我是编程新手,请告诉我如何从MS Sql数据库中检索到的列值到标签。我从sql获得了如下数据
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);
现在我有一个asp .net标签
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
我如何获得列的价值&#34;电子邮件&#34;到Label5?
可以这样做吗?
<asp:Label ID="Label5" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
我不想使用后面的代码或c#。
请帮忙。
答案 0 :(得分:1)
有很多方法可以实现这一点。可以使用FormView:
标记
<asp:FormView ID="formView" runat="server">
<ItemTemplate>
<span><%# Eval("Email") %></span>
</ItemTemplate>
</asp:FormView>
旁边的代码
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();
另一种方法可能是使用Page DataBind,例如:
标记
<asp:Label ID="Label5" runat="server" Text='<%# Email %>'></asp:Label>
旁边的代码
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
您需要将电子邮件声明为属性。
protected string Email { get; set; }