可以换成this.sqlDeleteCommand1.Parameters["Original_question_ID"].Value=d_Assign.selectindex就可以了。 如果还不好使,可以检查一下InitializeComponent()函数里面是不是没有添加d_Assign_DeleteCommand的代表,应该是this.d_Assign.DeleteCommand+= new System.EventHandler(this.d_Assign_DeleteCommand);
if(!IsPostBack)
{
//绑定数据
}
只写了fill的那句
难道一定要写代码吗.
写在哪里?
sqlConnection1.ConnectionString = "user id=sa;data source=10.0.0.15;persist security info=True;initial catalog=202PEM;password=gaoshan"; SqlDataAdapter da=new SqlDataAdapter("select * from test",sqlConnection1);
SqlCommandBuilder myBld = new SqlCommandBuilder (da);
da.Update (ds);
{
//绑定数据
}
1 我是用vs.net界面设计的。自己只写了两行代码
private void Page_Load(object sender, System.EventArgs e)
{
this.sqlDataAdapter1.Fill(this.dataSet21);
this.DataGrid1.DataBind();
}
删除用的是一个删除按钮列
2 看过数据库,数据根本没删掉
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this.sqlDataAdapter1.Fill(this.dataSet21);
this.DataGrid1.DataBind();
}
}
{
//用(孤寂无边)的方法删除相应行,此略
this.sqlDataAdapter1.Update(this.dataSet21);
this.DataGrid1.DataBind();
}
{
if(!IsPostBack)
{
this.sqlDataAdapter1.Fill(this.dataSet21);
this.DataGrid1.DataBind();
}
}
我已经加上了if(!IsPostBack)还是不行。
DeleteCommand当然有了。 //
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = @"DELETE FROM dbo.T_Question WHERE (question_ID = @Original_question_ID) AND (answer = @Original_answer OR @Original_answer IS NULL AND answer IS NULL) AND (question = @Original_question OR @Original_question IS NULL AND question IS NULL) AND (username = @Original_username OR @Original_username IS NULL AND username IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_question_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "question_ID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_answer", System.Data.SqlDbType.NVarChar, 255, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "answer", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_question", System.Data.SqlDbType.NVarChar, 255, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "question", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_username", System.Data.SqlDbType.NVarChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "username", System.Data.DataRowVersion.Original, null));
以上是窗体设计器生成的代码
这是用窗体设计器生成的删除按钮列,他应该会自动去调用delete。
难道没人用过 删除按钮列 吗????
我以前是可以的。很简单的。不知道怎么不行了
{
}
把你的这个事件处理里面的代码贴出来看看就知道了
private void d_Assign_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int keyID=Convert.ToInt32(d_Assign.DataKeys[(int)e.Item.ItemIndex].ToString());
DataRow dr=ds.Tables[0].Find(keyID);
dr.Delete();
ds.AccepChanges();
da.Update(ds);
BindAssignGrid();
}
我用this.sqlDeleteCommand1.ExecuteNonQuery();试试
提示我异常详细信息: System.Data.SqlClient.SqlException: 被准备语句 '(@Original_question_ID int,@Original_answer nvarchar(255),@Origi' 需要参数 @Original_question_ID,但未提供该参数。
怎么设置参数阿
this.sqlDeleteCommand1.Parameters["Original_question_ID"].Value=2;试一下异常详细信息: System.IndexOutOfRangeException: 此 SqlParameterCollection 中未包含带有 ParameterName“Original_question_ID”的 SqlParameter。
this.sqlDeleteCommand1.Parameters["Original_question_ID"].Value=2;
就不行。
而且这里的2怎么换成指定的行阿。
如果还不好使,可以检查一下InitializeComponent()函数里面是不是没有添加d_Assign_DeleteCommand的代表,应该是this.d_Assign.DeleteCommand+= new System.EventHandler(this.d_Assign_DeleteCommand);
private void d_Assign_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int keyID=Convert.ToInt32(d_Assign.DataKeys[(int)e.Item.ItemIndex].ToString());
SqlConnection conntemp;
conntemp2=new SqlConnection(connStr);
SqlCommand cmdtemp;
conntemp.Open();
selectCmd="delete ASN_HEAD_WRK where keyID='"+keyID+"'";
cmdtemp.ExecuteNonQuery();
conntemp.Close();BindAssignGrid(); }
int keyID=Convert.ToInt32(this.DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString());出错:异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index怎么办啊,还有你们说的BindAssignGrid是不是自己写的方法,我这里没有啊
是0怎么回事啊?????
为什么我用 hhqwolf(ukyo)的方法private void d_Assign_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int keyID=Convert.ToInt32(d_Assign.DataKeys[(int) e.Item.ItemIndex].ToString());
DataRow dr=ds.Tables[0].Find(keyID);
dr.Delete();
ds.AccepChanges();
da.Update(ds);
BindAssignGrid();
}
问什么dr.Delete();这行出错:
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
何不用普通的delete语法试试呢