Help Me to Combine Label id, text, and Database

Discussion in 'C#' started by hungryfriend, Jun 24, 2009.

  1. #1
    please help me to combine label id, text and database content


    first, i got 3 data in my database (tabel name: "mydata", content: data001, data002, data003) << [succeed]

    then, in my Asp.net webpage, i put 3 Label (ID: out1, out2, out3) << [succeed]


    in my .cs file, i insert

    connect the database, then the myquery(select * from mydata) << [succeed]

    SqlDataReader reader = myquery.ExecuteReader(); << [succeed]


    while(reader.Read())

    {

    for(int a=0;a<4;a++)

    {

    "out" + a.ToString() + ".Text" = (string)reader["mydata"]; << [FAILED]

    error message: "The left-hand side of an assignment must be a variable, property or indexer"

    i wanna automated the label id (out1,out2,out3) increment using loop int a, then the text also automated, and a the webpage, Label named out1, out2, out3, the text will change into data001, data002, data003.

    }

    }
     
    hungryfriend, Jun 24, 2009 IP
  2. dopanel.com

    dopanel.com Peon

    Messages:
    93
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    ((Label)this.FindControl("out" + a.ToString())).Text = (string)reader["mydata"];
    
    Code (markup):
     
    dopanel.com, Jun 24, 2009 IP
  3. hungryfriend

    hungryfriend Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thx, it work... now my label can be automated... but, there's lil' problem...

    when i use
    = (string)reader["mydata"]; << the record shown just the last record from the database

    then i using array to collect each database data... with hope that they will be like this...
    out1.Text = first database data
    out2.Text = second database data
    etc...

    but still... all Label.Text shown only the last database data... where i did wrong? thx b4... ^ ^
     
    hungryfriend, Jun 25, 2009 IP
  4. dopanel.com

    dopanel.com Peon

    Messages:
    93
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    ((Label)this.FindControl("out" + a.ToString())).Text = (string)reader[a];
    
    Code (markup):
     
    dopanel.com, Jun 26, 2009 IP