How to bind a label to a dataset with C#

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]; ???
 
J

James Irvine

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();
 
B

bthumber

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();





.
 
J

James Irvine

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...
 
B

bthumber

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...
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top