locked
I want to bind labels from table data. RRS feed

  • Question

  • User420834038 posted

    I want to bind labels from table data. Like i have five label and those 5 labels should bind from data which is save in table.

    Table Name = tbl_data

    ID Time
    1 07:00 AM to 16:00 PM
    2 11:00 AM to 20:00 PM
    3 13:00 PM to 22:00 PM
    4 19:00 PM to 04:00 AM
    5 22:00 PM to 07:00 AM

    and i want to bind all this record in Five different labels like label1, label2, label3, label4, label5

    Kindly help.

    Thanks.

    Friday, May 29, 2020 6:13 AM

All replies

  • User-939850651 posted

    Hi, priyanka.talekar

    You could traverse the query data, dynamically create a label and bind the value.

    I create a demo and achieved bind in button click event, you also can do it at other event.

    More details, you could refer to the below codes:

    FiveLabels.aspx

    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button runat="server" Text="BindToLabel" OnClick="Bind_Click"/>
    <br /> </div> </form> </body>

    FiveLabels.aspx.cs

    protected void Bind_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                String conStr = "data source=.; database=TestDB; integrated security=SSPI";
                using (SqlConnection conn = new SqlConnection(conStr)) {
                    String query = "Select time from test";
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(query, conn)) {
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        for (int i = 0; i < dt.Rows.Count; i++) {
                            Label label = new Label();
                            String LabelID = String.Concat("id", (i + 1).ToString());
                            label.ID = LabelID;
                            label.Text = dt.Rows[i].ItemArray[0].ToString();
                            label.Width = 100;
                            Page.Controls.Add(label);
                            
                            label = new Label();
                            LabelID = String.Concat("label", (i+1).ToString());
                            label.ID = LabelID;
                            label.Text = dt.Rows[i].ItemArray[1].ToString();
    
                            Page.Controls.Add(label);
                            Literal literal = new Literal();
                            literal.Text = "</br>";
                            Page.Controls.Add(literal); 
                       }
                    }
                }
            }
    

    Result:

    When I click the button and its display 

    Hope this can help you.

    Best regards,

    Xudong Peng

    Friday, May 29, 2020 8:48 AM
  • User420834038 posted

    Hi XuDong Peng,

    actually this would work, but i want all the values above the textbox. like 

    Design: on aspx page

    Label1 Label2 Label3 Label4 Label5
    Textbox1 Textbox2 Textbox3 Textbox4 Textbox5

    Design : this output i was expecting

    07:00 AM to 16:00 PM 11:00 AM to 20:00 PM 13:00 PM to 22:00 PM 19:00 PM to 04:00 AM 22:00 PM to 07:00 AM
    Textbox1 Textbox2 Textbox3 Textbox4 Textbox5

    like this i want to bind all values in label above the textbox. Help me accordingly.

    Thanks

    Friday, May 29, 2020 9:04 AM
  • User-939850651 posted

    Hi, priyanka.talekar

    I create a demo, This may meet your needs?

    Please refer to below codes:

    aspx.cs

    protected void Bind_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                String conStr = "data source=.; database=TestDB; integrated security=SSPI";
                using (SqlConnection conn = new SqlConnection(conStr)) {
                    String query = "Select * from test";
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(query, conn)) {
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        Page.Form.Controls.Add(new Literal { Text = "</br>"});
                        for (int i = 0; i < dt.Rows.Count; i++) {
                            Label label = new Label();
                           
                            String LabelID = String.Concat("label", (i+1).ToString());
                            label.ID = LabelID;
                            label.Text = dt.Rows[i].ItemArray[1].ToString();
                            label.Style.Add("text-align", "center");
                            label.Width = 180;
                            Page.Form.Controls.Add(label);
                        }
                        Literal literal = new Literal();
                        literal.Text = "</br>";
                        Page.Form.Controls.Add(literal);
                        for (int i = 0; i < dt.Rows.Count; i++) {
                            TextBox box = new TextBox();
                            box.AutoPostBack = true;
                            box.Attributes.Add("runat", "server");
                            box.ID = String.Concat("TextBox", (i + 1).ToString());
                            box.Style.Add("width", "172px");
                            box.Text = "TextBox" + (i + 1).ToString();
                            Page.Form.Controls.Add(box);
                        }
                    }
                }
            }

    Result:

    Best regards,

    Xudong Peng

    Friday, May 29, 2020 10:40 AM
  • User420834038 posted

    Hi, XuDong Peng,

    Thanks for your response, but dont want it like this i have already taken the label controls from toolbox in aspx page.

    this will be my .aspx (design) page

    <div>Shift1 -
    <asp:Label runat="server" ID="lbl1"></asp:Label>
    <asp:TextBox runat="server" ID="txtlbl1"></asp:TextBox>

    Shift2 -
    <asp:Label runat="server" ID="Label1"></asp:Label>
    <asp:DropDownList runat="server" ID="TextBox1"></asp:DropDownList>

    Shift3 -
    <asp:Label runat="server" ID="Label2"></asp:Label>
    <asp:DropDownList runat="server" ID="TextBox2"></asp:DropDownList>

    Shift4 -
    <asp:Label runat="server" ID="Label3"></asp:Label>
    <asp:TextBox runat="server" ID="TextBox3"></asp:TextBox>

    Shift5 -
    <asp:Label runat="server" ID="Label4"></asp:Label>
    <asp:TextBox runat="server" ID="TextBox4"></asp:TextBox>
    </div>

    all these 5 label controls i want to bind with the data i share with you (tbl_data). In that table there are 5 shift times and i need to bind label controls with those shift times. Means i want to read those values to label controls from that table

    Kindly help. Thanks.

    Friday, May 29, 2020 12:34 PM
  • User-939850651 posted

    Hi, Priyanka.talekar

    For already designed pages, you can use the FindControl method to get the control and operate it.

    More details, you could refer to the below codes:

    FiveLabels.aspx
    
    <head runat="server">
        <title></title>
        <link href="Content/bootstrap.css" rel="stylesheet" />
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <br />
                <asp:Button runat="server" Text="BindToLabel" OnClick="Bind_Click" />
                <br /><br />
                <div>
                    <div class="col-md-2">
                        Shift1 -
                    <asp:Label runat="server" ID="Label1"></asp:Label><br />
                        <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
                    </div>
                    <div class="col-md-2">
                        Shift2 -
                    <asp:Label runat="server" ID="Label2"></asp:Label><br />
                        <asp:DropDownList runat="server" ID="TextBox2">
                            <asp:ListItem Text="TextBox2"></asp:ListItem>
                        </asp:DropDownList>
                    </div>
                    <div class="col-md-2">
                        Shift3 -
                    <asp:Label runat="server" ID="Label3"></asp:Label><br />
                        <asp:DropDownList runat="server" ID="TextBox3">
                            <asp:ListItem Text="TextBox3"></asp:ListItem>
                        </asp:DropDownList>
                    </div>
                    <div class="col-md-2">
                        Shift4 -
                    <asp:Label runat="server" ID="Label4"></asp:Label><br />
                        <asp:TextBox runat="server" ID="TextBox4"></asp:TextBox>
                    </div>
                    <div class="col-md-2">
                        Shift5 -
                    <asp:Label runat="server" ID="Label5"></asp:Label><br />
                        <asp:TextBox runat="server" ID="TextBox5"></asp:TextBox>
                    </div>
                </div>
                <br />
            </div>
        </form>
    </body>
    
    FiveLabels.aspx.cs
    
    protected void Bind_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                String conStr = "data source=.; database=TestDB; integrated security=SSPI";
                using (SqlConnection conn = new SqlConnection(conStr)) {
                    String query = "Select time from test";
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(query, conn)) {
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                        Page.Form.Controls.Add(new Literal { Text = "</br>"});
                        for (int i = 0; i < dt.Rows.Count; i++) {
                            //get Label control with control ID, and set value
                            String LabelID = String.Concat("Label",(i+1).ToString());
                            Label label = (Label)Page.FindControl(LabelID);
                            label.Text = dt.Rows[i].ItemArray[0].ToString();
    
                            String TextBoxID = String.Concat("TextBox", (i + 1).ToString());
                            if (i != 1 && i != 2) { 
                            TextBox box = (TextBox)Page.FindControl(TextBoxID);
                            box.Text = "TextBox" + (i + 1).ToString();
                            }
                        }
                   }
                }
            }
    

    Result:

    Best regards,

    Xudong Peng

    Monday, June 1, 2020 2:30 AM
  • User-2054057000 posted

    You can simply use ADO.NET to read data from database and show it to the user. Kindly refer - Reading Records using ADO.NET

    Monday, June 1, 2020 9:21 AM