Wednesday, March 28, 2012

How to show data in a Label item?

SqlCeConnection cn = new SqlCeConnection("Data Source=\\Program Files\\test\\tel.sdf");
try
{
SqlCeCommand cmd = cn.CreateCommand();

cmd.CommandText = "SELECT * from phone where PhoneNo = '0912312345'";

SqlCeDataAdapter adp = new SqlCeDataAdapter(cmd);

DataSet ds = new DataSet();

adp.Fill(ds);
this.dataGrid1.DataSource = ds.Tables[0];

label1.Text =Convert.ToString(?);

label2.Text =Convert.ToString(?);

}

finally
{
if (cn.State != ConnectionState.Closed)
{
cn.Close();
}
}

I can show the result in the DataGrid1

however, I want the label1 to show the Name,

and label2 to show the phoneNo.

how to do in here,

label1.Text =Convert.ToString(?);

label2.Text =Convert.ToString(?);

thanks

DataSet is a collection of DataTables and DataTable is a collection of rows and columns. You can access data in the table directly as you would do it with array:

label1.Text = ds.Tables[0].Rows[row_index_here][colum_index_or_name_here].ToString();

Alternatively you can bind your label so it would be updated automatically as you change current row in the grid:

label1.DataBindings.Add("Text", ds.Tables[0], "ColumnNameHere");

sql

No comments:

Post a Comment