How to bind a label to a dataset with C#

Discussion in 'ASP .Net' started by bthumber, Jan 20, 2010.

  1. bthumber

    bthumber Guest

    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, Jan 20, 2010
    #1
    1. Advertising

  2. bthumber

    James Irvine Guest

    bthumber wrote:
    > 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 Irvine, Jan 20, 2010
    #2
    1. Advertising

  3. bthumber

    bthumber Guest

    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" wrote:

    > bthumber wrote:
    > > 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, Jan 20, 2010
    #3
  4. bthumber

    James Irvine Guest

    bthumber wrote:
    > 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...
    James Irvine, Jan 20, 2010
    #4
  5. bthumber

    bthumber Guest

    Thanks!

    "James Irvine" wrote:

    > bthumber wrote:
    > > 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...
    > .
    >
    bthumber, Jan 21, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,591
    mr2_93
    Oct 2, 2005
  2. =?Utf-8?B?bWltbw==?=

    Bind a Label to an object Datasource control

    =?Utf-8?B?bWltbw==?=, Jan 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,922
    =?Utf-8?B?bWltbw==?=
    Jan 3, 2006
  3. Miguel Dias Moura
    Replies:
    2
    Views:
    9,285
    Yamini
    Apr 14, 2011
  4. Replies:
    0
    Views:
    589
  5. Knut Krueger
    Replies:
    8
    Views:
    263
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page