How to bind a label to a dataset with C#

  • Thread starter Thread starter bthumber
  • Start date Start date
B

bthumber

I have a project class. The method GetRecordCover returns a dataset:

public DataTable GetRecordCover(string id)
{
string str = “SELECT * FROM RecordCover WHERE=’†+ “id†+ “’â€;

SqlConnection cn = new SqlConnection(_connectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, cn);

DataSet ds = new DataSet();
da.Fill(ds, “RecordCoverâ€);

// Get data table
DataTable dt = ds.Tables[“RecordCoverâ€];

Return ds.Tables[0];

//ds.Tables[0].Rows[0][0];
//ds.Tables[0].Rows[0][1];
//ds.Tables[0].Rows[0][2];
}

In the web form's code behind I want:

RecordCover rc = new RecordCover();

label1.text = how do populate my label with ds.Tables[0].Rows[0][0];
label2.text = how do populate my label with ds.Tables[0].Rows[0][1];
label3.text = how do populate my label with ds.Tables[0].Rows[0][2]; ???
 
bthumber said:
I have a project class. The method GetRecordCover returns a dataset:

public DataTable GetRecordCover(string id)
{
string str = “SELECT * FROM RecordCover WHERE=’†+ “id†+ “’â€;

SqlConnection cn = new SqlConnection(_connectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, cn);

DataSet ds = new DataSet();
da.Fill(ds, “RecordCoverâ€);

// Get data table
DataTable dt = ds.Tables[“RecordCoverâ€];

Return ds.Tables[0];

//ds.Tables[0].Rows[0][0];
//ds.Tables[0].Rows[0][1];
//ds.Tables[0].Rows[0][2];
}

In the web form's code behind I want:

RecordCover rc = new RecordCover();

label1.text = how do populate my label with ds.Tables[0].Rows[0][0];
label2.text = how do populate my label with ds.Tables[0].Rows[0][1];
label3.text = how do populate my label with ds.Tables[0].Rows[0][2]; ???

label3.Text = dt.Rows[0][1].ToString();

btw, you can name your columns, making you code more readable and less
breakable in case a column is added to the source table. Like this:


Label_addr4.Text = dtEvents.Rows[0]["addr4"].ToString();
 
James...remember the label is not in the project so don't I need an instance
of the class and make a call to that method? i.e.

RecordCover rc = new RecordCover();

then rc.GetRecordCover("id");



James Irvine said:
bthumber said:
I have a project class. The method GetRecordCover returns a dataset:

public DataTable GetRecordCover(string id)
{
string str = “SELECT * FROM RecordCover WHERE=’†+ “id†+ “’â€;

SqlConnection cn = new SqlConnection(_connectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, cn);

DataSet ds = new DataSet();
da.Fill(ds, “RecordCoverâ€);

// Get data table
DataTable dt = ds.Tables[“RecordCoverâ€];

Return ds.Tables[0];

//ds.Tables[0].Rows[0][0];
//ds.Tables[0].Rows[0][1];
//ds.Tables[0].Rows[0][2];
}

In the web form's code behind I want:

RecordCover rc = new RecordCover();

label1.text = how do populate my label with ds.Tables[0].Rows[0][0];
label2.text = how do populate my label with ds.Tables[0].Rows[0][1];
label3.text = how do populate my label with ds.Tables[0].Rows[0][2]; ???

label3.Text = dt.Rows[0][1].ToString();

btw, you can name your columns, making you code more readable and less
breakable in case a column is added to the source table. Like this:


Label_addr4.Text = dtEvents.Rows[0]["addr4"].ToString();





.
 
bthumber said:
James...remember the label is not in the project so don't I need an instance
of the class and make a call to that method? i.e.

RecordCover rc = new RecordCover();

then rc.GetRecordCover("id");


yes. Here's an example:

protected void getEvents()
{
getEvents cx = new getEvents();
DataTable dtEvents = new DataTable();

dtEvents = cx.Get_Events(); // get all events


if (dtEvents.Rows.Count > 0)
{

// 1st event:
Label_Headline.Text =
dtEvents.Rows[0]["headline"].ToString();

}

etc...
 
Thanks!

James Irvine said:
bthumber said:
James...remember the label is not in the project so don't I need an instance
of the class and make a call to that method? i.e.

RecordCover rc = new RecordCover();

then rc.GetRecordCover("id");


yes. Here's an example:

protected void getEvents()
{
getEvents cx = new getEvents();
DataTable dtEvents = new DataTable();

dtEvents = cx.Get_Events(); // get all events


if (dtEvents.Rows.Count > 0)
{

// 1st event:
Label_Headline.Text =
dtEvents.Rows[0]["headline"].ToString();

}

etc...
.
 
Back
Top