主题:数据为什么修改不成功呢????
我做了一个学生信息的删除修改 可是就是编辑删除失败 哪位大虾可以帮小妹指点一下?????[em18]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
SqlConnection Conn;
protected void Page_Load(Object Src, EventArgs E ) {
Conn=new SqlConnection("server=127.0.0.1;database=jiaoshi;uid=sa;pwd='1'");
Conn.Open();
if (!IsPostBack) BindGrid();
}
void DataGrid_Edit(Object sender,DataGridCommandEventArgs E){
dg.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
void DataGrid_Cancel(Object sender,DataGridCommandEventArgs E){
dg.EditItemIndex = -1;
BindGrid();
}
void DataGrid_Update(Object sender,DataGridCommandEventArgs E){
String strSQL = "UPDATE stu_inf SET "+
"姓名='"+((TextBox)E.Item.Cells[3].Controls[0]).Text
+"',联系方式="+((TextBox)E.Item.Cells[4].Controls[0]).Text
+" where 学号="+dg.DataKeys[(int)E.Item.ItemIndex];
SqlCommand cm = new SqlCommand(strSQL,Conn);
try {
cm.ExecuteNonQuery();
Message.InnerHtml="<b>编辑成功</b>";
// 退出编辑状态
dg.EditItemIndex=-1;
}
catch (SqlException) {
Message.InnerHtml="编辑失败";
Message.Style["color"]="red";
}
cm.Connection.Close();
// 更新DataGrid
BindGrid();
}
// 处理删除事件
public void DataGrid_Delete(Object sender,DataGridCommandEventArgs E){
String strSQL="DELETE FROM stu_inf WHERE 学号="
+ dg.DataKeys[(int)E.Item.ItemIndex];
SqlCommand cm = new SqlCommand(strSQL,Conn);
try {
cm.ExecuteNonQuery();
Message.InnerHtml = "<b>删除成功</b>";
}
catch (SqlException){
Message.InnerHtml = "<b>删除失败</b>";
Message.Style["color"]="red";
}
Conn.Close();
BindGrid();
}
public void BindGrid() {
SqlDataAdapter da=new SqlDataAdapter("select * from stu_inf",Conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource=ds;
dg.DataBind();
}
</script>
<style type="text/css">
<!--
.STYLE1 {
font-size: 12px;
font-weight: bold;
}
body {
background-color: #aaaaCC;
}
-->
</style>
<body style="font: 10pt verdana">
<form runat="server">
<asp:DataGrid id="dg" runat="server"
Width="400" BackColor="#ccccff"
BorderColor="black" ShowFooter="false"
CellPadding=3 CellSpacing="0"
Font-Name="Verdana" Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="DataGrid_Edit"
OnCancelCommand="DataGrid_Cancel"
OnUpdateCommand="DataGrid_Update"
OnDeleteCommand="DataGrid_Delete"
DataKeyField="学号"
AutoGenerateColumns="false"
MaintainState="false">
<Columns>
<asp:EditCommandColumn EditText="编辑" CancelText="取消"
UpdateText="更新" ItemStyle-Wrap="false"/>
<asp:ButtonColumn Text="删除" CommandName="Delete" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="学号" DataField="学号" ReadOnly="True"/>
<asp:BoundColumn HeaderText="姓名" DataField="姓名" />
<asp:BoundColumn HeaderText="联系方式" DataField="联系方式" />
</Columns>
</asp:DataGrid>
<span id="Message" MaintainState="false"
style="font: arial 11pt;" runat="server"/>
</form>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
SqlConnection Conn;
protected void Page_Load(Object Src, EventArgs E ) {
Conn=new SqlConnection("server=127.0.0.1;database=jiaoshi;uid=sa;pwd='1'");
Conn.Open();
if (!IsPostBack) BindGrid();
}
void DataGrid_Edit(Object sender,DataGridCommandEventArgs E){
dg.EditItemIndex = (int)E.Item.ItemIndex;
BindGrid();
}
void DataGrid_Cancel(Object sender,DataGridCommandEventArgs E){
dg.EditItemIndex = -1;
BindGrid();
}
void DataGrid_Update(Object sender,DataGridCommandEventArgs E){
String strSQL = "UPDATE stu_inf SET "+
"姓名='"+((TextBox)E.Item.Cells[3].Controls[0]).Text
+"',联系方式="+((TextBox)E.Item.Cells[4].Controls[0]).Text
+" where 学号="+dg.DataKeys[(int)E.Item.ItemIndex];
SqlCommand cm = new SqlCommand(strSQL,Conn);
try {
cm.ExecuteNonQuery();
Message.InnerHtml="<b>编辑成功</b>";
// 退出编辑状态
dg.EditItemIndex=-1;
}
catch (SqlException) {
Message.InnerHtml="编辑失败";
Message.Style["color"]="red";
}
cm.Connection.Close();
// 更新DataGrid
BindGrid();
}
// 处理删除事件
public void DataGrid_Delete(Object sender,DataGridCommandEventArgs E){
String strSQL="DELETE FROM stu_inf WHERE 学号="
+ dg.DataKeys[(int)E.Item.ItemIndex];
SqlCommand cm = new SqlCommand(strSQL,Conn);
try {
cm.ExecuteNonQuery();
Message.InnerHtml = "<b>删除成功</b>";
}
catch (SqlException){
Message.InnerHtml = "<b>删除失败</b>";
Message.Style["color"]="red";
}
Conn.Close();
BindGrid();
}
public void BindGrid() {
SqlDataAdapter da=new SqlDataAdapter("select * from stu_inf",Conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource=ds;
dg.DataBind();
}
</script>
<style type="text/css">
<!--
.STYLE1 {
font-size: 12px;
font-weight: bold;
}
body {
background-color: #aaaaCC;
}
-->
</style>
<body style="font: 10pt verdana">
<form runat="server">
<asp:DataGrid id="dg" runat="server"
Width="400" BackColor="#ccccff"
BorderColor="black" ShowFooter="false"
CellPadding=3 CellSpacing="0"
Font-Name="Verdana" Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="DataGrid_Edit"
OnCancelCommand="DataGrid_Cancel"
OnUpdateCommand="DataGrid_Update"
OnDeleteCommand="DataGrid_Delete"
DataKeyField="学号"
AutoGenerateColumns="false"
MaintainState="false">
<Columns>
<asp:EditCommandColumn EditText="编辑" CancelText="取消"
UpdateText="更新" ItemStyle-Wrap="false"/>
<asp:ButtonColumn Text="删除" CommandName="Delete" ItemStyle-Wrap="false"/>
<asp:BoundColumn HeaderText="学号" DataField="学号" ReadOnly="True"/>
<asp:BoundColumn HeaderText="姓名" DataField="姓名" />
<asp:BoundColumn HeaderText="联系方式" DataField="联系方式" />
</Columns>
</asp:DataGrid>
<span id="Message" MaintainState="false"
style="font: arial 11pt;" runat="server"/>
</form>