Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Get multiple data from table and show in different label boxes

Get multiple data from table and show in different label boxes

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scothykonma
    wrote on last edited by
    #1

    Hi All, I have table with 3 fields id , status and Name... I have 3 or more label boxes in my header. I getting data like, "select id, Name from table1 where status = 1" My data are, id Name 1001 AAA 1002 CCC 1003 SSS My code is

    Header head = new Header();
    DataSet obj = new DataSet();
    obj = head.getdata();
    if (obj.Tables[0].Rows.Count < 0)
    {
    int i;
    for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++)
    {
    string text1 = obj.Tables[0].Rows[i]["id"].ToString();
    string text2 = obj.Tables[0].Rows[i]["Name"].ToString();
    lblMessage.Text = text1 + " " + text2;
    }
    }

    It shows only the last data...I know i have to assign values to lblmessage1.text and lblmessage2.text and so on.. But i do not know how to fix it... I am fresher and new to sql....Give some samples Thanks in Advance

    S U 2 Replies Last reply
    0
    • S scothykonma

      Hi All, I have table with 3 fields id , status and Name... I have 3 or more label boxes in my header. I getting data like, "select id, Name from table1 where status = 1" My data are, id Name 1001 AAA 1002 CCC 1003 SSS My code is

      Header head = new Header();
      DataSet obj = new DataSet();
      obj = head.getdata();
      if (obj.Tables[0].Rows.Count < 0)
      {
      int i;
      for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++)
      {
      string text1 = obj.Tables[0].Rows[i]["id"].ToString();
      string text2 = obj.Tables[0].Rows[i]["Name"].ToString();
      lblMessage.Text = text1 + " " + text2;
      }
      }

      It shows only the last data...I know i have to assign values to lblmessage1.text and lblmessage2.text and so on.. But i do not know how to fix it... I am fresher and new to sql....Give some samples Thanks in Advance

      S Offline
      S Offline
      saanj
      wrote on last edited by
      #2

      Hi, You have told that you are using three or more label controls to display three rows picked from the database tables. As far as the code that you have posted, you have started a for loop and it binds the value with the lblMessage control. So it is pretty clear that the value which is getting bound to the lblMessage control, is getting overwritten each time the for loop runs. So at the end of the loop execution, the label is showing the last result only. :( Suppose, you have three controls like lblMessage1, lblMessage2 and lblMessage3. To achieve the functionality referred by you, just try the following code: for (i = 0; i<= obj.Tables[0].Rows.Count - 1; i++) { if(i==0) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage1.Text = text1 + " " + text2; } if(i==1) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage2.Text = text1 + " " + text2; } else { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage3.Text = text1 + " " + text2; } } All the best. :)

      Either you love IT or leave IT...

      S 1 Reply Last reply
      0
      • S saanj

        Hi, You have told that you are using three or more label controls to display three rows picked from the database tables. As far as the code that you have posted, you have started a for loop and it binds the value with the lblMessage control. So it is pretty clear that the value which is getting bound to the lblMessage control, is getting overwritten each time the for loop runs. So at the end of the loop execution, the label is showing the last result only. :( Suppose, you have three controls like lblMessage1, lblMessage2 and lblMessage3. To achieve the functionality referred by you, just try the following code: for (i = 0; i<= obj.Tables[0].Rows.Count - 1; i++) { if(i==0) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage1.Text = text1 + " " + text2; } if(i==1) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage2.Text = text1 + " " + text2; } else { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage3.Text = text1 + " " + text2; } } All the best. :)

        Either you love IT or leave IT...

        S Offline
        S Offline
        scothykonma
        wrote on last edited by
        #3

        Thanks for your reply.. I case i have mulitple records in my table and may use more label box in my page.. How can I change my code?

        S 1 Reply Last reply
        0
        • S scothykonma

          Thanks for your reply.. I case i have mulitple records in my table and may use more label box in my page.. How can I change my code?

          S Offline
          S Offline
          saanj
          wrote on last edited by
          #4

          Hi, Did you get the logic in the code that I wrote? If you got the logic then you should have got your answer. :) Regards Saanj

          Either you love IT or leave IT...

          1 Reply Last reply
          0
          • S scothykonma

            Hi All, I have table with 3 fields id , status and Name... I have 3 or more label boxes in my header. I getting data like, "select id, Name from table1 where status = 1" My data are, id Name 1001 AAA 1002 CCC 1003 SSS My code is

            Header head = new Header();
            DataSet obj = new DataSet();
            obj = head.getdata();
            if (obj.Tables[0].Rows.Count < 0)
            {
            int i;
            for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++)
            {
            string text1 = obj.Tables[0].Rows[i]["id"].ToString();
            string text2 = obj.Tables[0].Rows[i]["Name"].ToString();
            lblMessage.Text = text1 + " " + text2;
            }
            }

            It shows only the last data...I know i have to assign values to lblmessage1.text and lblmessage2.text and so on.. But i do not know how to fix it... I am fresher and new to sql....Give some samples Thanks in Advance

            U Offline
            U Offline
            Uma J
            wrote on last edited by
            #5

            Hi, This is simple just include a plus(+)... Look at the code in which i did some alteration. Header head = new Header(); DataSet obj = new DataSet(); obj = head.getdata(); if (obj.Tables[0].Rows.Count < 0) { int i; for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); // Just include a plus(+) lblMessage.Text += text1 + " " + text2; } }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups