On this tutorial i will explain how to show data from database on label using C# with foreach loop

Mostly developers using GridView to show data but sometimes i need to show all data on label show this is best example and also using foreach loop

Page : District.aspx.cs
public void GetTraining()
    {
        ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        SqlDataAdapter  Adp = new SqlDataAdapter("select DistName  from tbl_district", ConStr);
        DataTable Dt = new DataTable();
        Adp.Fill(Dt);
        if (Dt.Rows.Count > 0)
        {
            foreach (DataRow row in Dt.Rows)
            {
               
                lbl_training.Text = lbl_training.Text + "<br>"+row["DistName"].ToString();
            }
            
         
        }
   }
Page:- District.aspx
<marquee scrollamount="6" scrolldelay="20" height="310" behavior="scroll" direction="up" 
                                    onmouseover="this.stop();" onmouseout="this.start()" ;="">
                                
                                <asp:Label  ID="lbl_training" Style="font-size:16px; text-align:justify" runat="server"></asp:Label>

                                </marquee>

Output :-