<asp:DataGrid id="Dgd_student" runat="server" PageSize="15" AllowPaging="True" OnPageIndexChanged="DataGrid_Page" OnDeleteCommand="DataGrid_delete" DataKeyField="Student_id">
<PagerStyle mode="NumericPages"></PagerStyle>
<Columns>
<asp:ButtonColumn Text="Del" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid> public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string strconn="Server=localhost;uid=sa;pwd=;database=cms";
SqlConnection cn = new SqlConnection(strconn);
string strsql="delete from student where Student_id=@Student_id";
SqlCommand cm = new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
cm.Parameters["@Student_id"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="Delete OK!";
}
catch(SqlException)
{
Lbl_note.Text="Failed!";
}
cm.Connection.Close();
Bindgrid();
}为什么不能删除一行数据,页面只是刷新以下?
<PagerStyle mode="NumericPages"></PagerStyle>
<Columns>
<asp:ButtonColumn Text="Del" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid> public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string strconn="Server=localhost;uid=sa;pwd=;database=cms";
SqlConnection cn = new SqlConnection(strconn);
string strsql="delete from student where Student_id=@Student_id";
SqlCommand cm = new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
cm.Parameters["@Student_id"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="Delete OK!";
}
catch(SqlException)
{
Lbl_note.Text="Failed!";
}
cm.Connection.Close();
Bindgrid();
}为什么不能删除一行数据,页面只是刷新以下?
解决方案 »
- 奇怪问题“找不到可接受类型”
- 关于install shield 2008 打包windows服务的问题,不能完全卸载服务
- 【求助】从网站自动下载数据、批量下载
- c# 使用ADOX创建access数据库出了点问题
- 多项目 交叉引用问题
- Enterprise Library 2.0中的Caching Application Block怎么使用缓存依赖
- 新手问问题:关于ComboBox!
- Remoting中,如何踢掉客户端的事件的预定?
- 请问如何获得dns服务器的地址和名称? 在。net下 第一次问问题请支持一下
- ListView难题,高手请赐教
- 有关comboBox Undo如何实现
- 关于guid的问题!?急啊!
Dgd_student.DataKeys[(int)e.Item.ItemIndex]的值是什么,
建议你用e.Item.cell[0].text //索引 表示Student_id的那列
cm.Connection.Close();
Bindgrid();视图应该是先databind(),然后在关闭cm.Connection.Close();