-1

I am new to programming and please tell me how to get a column value retrieved from MS Sql database to a label. I hav got the data from sql as follows

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopsConnectionStringM"].ConnectionString);
 con.Open();
 sda = new SqlDataAdapter("Select UserName,Email,ShopCountry,PIN,ShopName,ShopCity,ShopDistrict,ShopState, Location,ShopBoard from Shops_Table where UserName= '" + HiddenField1.Value + "'", con);
 DataTable dt = new DataTable();
 sda.Fill(dt);

now i have an asp .net label

<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>

How do i get the value of column "Email" to Label5 ?

is it possible to do like this?

<asp:Label ID="Label5" runat="server" Text='<%#Eval("Email")%>'></asp:Label>

I dont want to use code behind or c#.

Please help.

hardkoded
  • 18,915
  • 3
  • 52
  • 64
Mansoor
  • 31
  • 1
  • 8
  • For your own safety use SQL parameters, don't concatenate strings – hardkoded Dec 19 '17 at 14:49
  • ok , can you give me a solution ?@kblok – Mansoor Dec 19 '17 at 14:50
  • *"I dont want to use code behind or c#."* - Then ASP.NET with C# probably wasn't the best choice of technologies. Why exactly don't you want to simply set the value of the label in your code? `Label5.Text = someValue;` Or create a property in your page's class and bind the value to that? Is the `Label` part of some larger data-bound control and you want to bind the `DataTable` to that control? – David Dec 19 '17 at 14:50
  • @Mansoor: More information about SQL parameters in ADO.NET code can be found here: https://stackoverflow.com/q/7505808/328193 – David Dec 19 '17 at 14:56
  • This is just for learning purpose – Mansoor Dec 19 '17 at 15:16
  • @Mansoor: Then that sounds like an *ideal* opportunity to, well, learn things. – David Dec 19 '17 at 15:49

1 Answers1

1

There are many ways to implement this. One could be using a FormView:

Markup

<asp:FormView ID="formView" runat="server">
    <ItemTemplate>
        <span><%# Eval("Email") %></span>
    </ItemTemplate>
</asp:FormView>

Code Beside

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopsConnectionStringM"].ConnectionString);
con.Open();
sda = new SqlDataAdapter("Select UserName,Email,ShopCountry,PIN,ShopName,ShopCity,ShopDistrict,ShopState, Location,ShopBoard from Shops_Table where UserName= '" + HiddenField1.Value + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
formView.DataSource = dt;
formView.DataBind();

Another way could be using the Page DataBind, e.g.:

Markup

<asp:Label ID="Label5" runat="server" Text='<%# Email %>'></asp:Label>

Code Beside

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ShopsConnectionStringM"].ConnectionString);
    con.Open();
sda = new SqlDataAdapter("Select UserName,Email,ShopCountry,PIN,ShopName,ShopCity,ShopDistrict,ShopState, Location,ShopBoard from Shops_Table where UserName= '" + HiddenField1.Value + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);

Email = dt.Rows[0].Field<string>("Email);
DataBind(); //This is the Page data bind

You will need to declare Email as a property.

protected string Email { get; set; }
hardkoded
  • 18,915
  • 3
  • 52
  • 64