更新时间:2023-02-03 15:57:22
您只需要执行
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.