[英]Databind to a Repeater
I am having a really hard time wrapping my head around the use of datatables.我真的很难理解数据表的使用。 There for i been trying to use them more active, now this is where my issue surface.
因为我一直在尝试更积极地使用它们,现在这就是我的问题浮出水面的地方。
This code returns no errors but no data is bound to the repeater:此代码不返回错误,但没有数据绑定到转发器:
Page:页:
<asp:Repeater ID="RepeaterBrand" runat="server">
<ItemTemplate>
<p><%# Eval("products_name")%></p>
</ItemTemplate>
</asp:Repeater>
Codebehind:代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["brand"] != null && !IsPostBack)
{
var Brand = (Request.QueryString["brand"]);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Table_Products WHERE products_brand = @Pr_brand", conn);
cmd.Connection = conn;
cmd.Parameters.Add("@Pr_brand", System.Data.SqlDbType.VarChar).Value = Brand;
DataTable dt = new DataTable();
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows && reader.Read())
{
dt.Load(reader);
RepeaterBrand.DataSource = dt;
RepeaterBrand.DataBind();
}
conn.Close();
}
}
I'm quite new to C# so i might need some visual aid to get this.我对 C# 很陌生,所以我可能需要一些视觉帮助来获得它。
Thank you in advance.先感谢您。
I hope this might help anyone this is what i ended up with after JaydipJ showed me the syntax.我希望这可以帮助任何人这是我在 JaydipJ 向我展示语法后得到的结果。
if (Request.QueryString["brand"] != null && !IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
var Brand = (Request.QueryString["brand"]);
cmd.CommandText = "SELECT * FROM Table_Products WHERE products_brand = @Pr_brand";
cmd.Parameters.Add("@Pr_brand", System.Data.SqlDbType.VarChar).Value = Brand;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
RepeaterBrand.DataSource = dt;
RepeaterBrand.DataBind();
Don't read data from SQLDataReader.不要从 SQLDataReader 读取数据。 use it as it is to fill DataTable
按原样使用它来填充 DataTable
if (reader.HasRows )
{
dt.Load(reader);
RepeaterBrand.DataSource = dt;
RepeaterBrand.DataBind();
}
Try This Code It's Working.试试这个代码它正在工作。
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select Mst_Emp_Login.Emp_Uname from Mst_Emp_Login", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
ds.Dispose();adp.Dispose();
cmd.Dispose();
con.Close();con.Dispose();
}
catch(Exception err)
{
throw err;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.