对ASP.NET中继器控件的怀疑 [英] Doubt in asp.net repeater control
本文介绍了对ASP.NET中继器控件的怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的按钮点击事件代码
protected void ButtonShowVideo_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmd1 = new SqlCommand(); conn.Open(); cmd1.Connection = conn; cmd1.CommandType = CommandType.StoredProcedure; cmd1.CommandText = "Show_video16"; cmd1.Parameters.Add("@ID", SqlDbType.Int).Value = TextBox1.Text; SqlDataAdapter da = new SqlDataAdapter(cmd1); //cmd1.Parameters.Clear(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); da.Fill(dt); Repeater1.DataSource = cmd1.ExecuteReader(); Repeater1.DataBind(); conn.Close(); }
存储过程为:
CREATE PROCEDURE [dbo].[show_video16] ( @ID int ) AS declare @Video varbinary(max), @Video_Name nvarchar(50) BEGIN select @Video=Video,@Video_Name =Video_Name from Videos where ID = @ID END
我想在按钮单击事件期间将数据绑定到转发器控件中
,但不起作用,
解决方案
您只需要执行
Repeater1.DataSource = dt;
我不认为您需要执行''Repeater1.DataSource = cmd1.ExecuteReader();''
试试这个简单的方法.
绝对检查这些行...
DataSet ds = new DataSet(); DataTable dt = new DataTable(); da.Fill(dt); Repeater1.DataSource = cmd1.ExecuteReader();
...应该更像这样...
DataSet ds = new DataSet(); da.Fill(ds); if (ds.Tables.Count > 0) { DataTable dt = ds.Tables[0]; Repeater1.DataSource = dt; Repeater1.DataBind(); }
您不应该绑定到IDataReader(ExecuteReader的结果).而是填充您的DataSet,从结果中获取一个DataTable并绑定到该
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmd1 = new SqlCommand(); DataTable dt = new DataTable(); cmd1.CommandType = CommandType.StoredProcedure; cmd1.CommandText = "Show_video16"; cmd1.Parameters.Add("@ID", SqlDbType.Int).Value =convert.toInt32(TextBox1.Text); SqlDataAdapter da = new SqlDataAdapter(); da.selectedCommand=cmd1; da.selectedCommand.connection=conn; da.Fill(dt); <big>Repeater1.DataSource = dt;</big> Repeater1.DataBind();
我对您的代码做了一些修改.尝试使用此代码.
here is my button click event code
protected void ButtonShowVideo_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmd1 = new SqlCommand(); conn.Open(); cmd1.Connection = conn; cmd1.CommandType = CommandType.StoredProcedure; cmd1.CommandText = "Show_video16"; cmd1.Parameters.Add("@ID", SqlDbType.Int).Value = TextBox1.Text; SqlDataAdapter da = new SqlDataAdapter(cmd1); //cmd1.Parameters.Clear(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); da.Fill(dt); Repeater1.DataSource = cmd1.ExecuteReader(); Repeater1.DataBind(); conn.Close(); }
The Stored Procedure is:
CREATE PROCEDURE [dbo].[show_video16] ( @ID int ) AS declare @Video varbinary(max), @Video_Name nvarchar(50) BEGIN select @Video=Video,@Video_Name =Video_Name from Videos where ID = @ID END
I want to bind the data in repeater control during button click event
,but its not working,解决方案You just need to do
Repeater1.DataSource = dt;
I don''t think you need to do ''Repeater1.DataSource = cmd1.ExecuteReader();''
try this simple.
Definitely check over these lines...
DataSet ds = new DataSet(); DataTable dt = new DataTable(); da.Fill(dt); Repeater1.DataSource = cmd1.ExecuteReader();
...which should be more like this...
DataSet ds = new DataSet(); da.Fill(ds); if (ds.Tables.Count > 0) { DataTable dt = ds.Tables[0]; Repeater1.DataSource = dt; Repeater1.DataBind(); }
You shouldn''t be binding to a IDataReader (result of ExecuteReader). Instead, fill your DataSet, get a DataTable from the results and bind to that
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmd1 = new SqlCommand(); DataTable dt = new DataTable(); cmd1.CommandType = CommandType.StoredProcedure; cmd1.CommandText = "Show_video16"; cmd1.Parameters.Add("@ID", SqlDbType.Int).Value =convert.toInt32(TextBox1.Text); SqlDataAdapter da = new SqlDataAdapter(); da.selectedCommand=cmd1; da.selectedCommand.connection=conn; da.Fill(dt); <big>Repeater1.DataSource = dt;</big> Repeater1.DataBind();
I modified your code a little.Try With this.
这篇关于对ASP.NET中继器控件的怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文