在VS中文件代码:
.ASPX代码:
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="Guest" runat="server"
AutoGenerateColumns="False"
OnEditCommand="DataGrid_EditCommand"
OnUpdateCommand="DataGrid_UpdateCommand"
OnCancelCommand="DataGrid_CancelCommand"
AllowSorting="True" DataKeyField="id">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑" ItemStyle-Wrap="False">
</asp:EditCommandColumn>
<asp:BoundColumn DataField="id" SortExpression="id" ReadOnly="True" HeaderText="客户号">
</asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="id" DataTextField="name" SortExpression="name" HeaderText="姓名"
DataNavigateUrlFormatString="ShowPerson.aspx?id={0}">
</asp:HyperLinkColumn>
<asp:TemplateColumn HeaderText="密码">
<ItemTemplate>
<asp:Label Runat=server Text='<%# DataBinder.Eval(Container.DataItem,"password")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="password" Text='<%# DataBinder.Eval(Container.DataItem,"password")%>'/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="电子邮件">
<ItemTemplate>
<asp:Label Runat=server Text='<%# DataBinder.Eval(Container.DataItem,"email")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="email" Text='<%# DataBinder.Eval(Container.DataItem,"email")%>'/>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<span id="Message" runat="server" />
.CS代码:
SqlConnection myConn;
protected void Page_Load(object sender, System.EventArgs e)
{
myConn = new SqlConnection("server=(local);uid=sa;pwd=;database=student2");
if(!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
SqlDataAdapter da = new SqlDataAdapter("select * from GuestInfo",myConn);
DataSet ds = new DataSet();
da.Fill(ds);
Guest.DataSource = ds.Tables[0].DefaultView;
Guest.DataBind();
}
public void DataGrid_EditCommand(Object sender,DataGridCommandEventArgs e)
{
Guest.EditItemIndex = (int) e.Item.ItemIndex;
BindGrid();
}
public void DataGrid_CancelCommand(Object sender,DataGridCommandEventArgs e)
{
Guest.EditItemIndex = -1;
BindGrid();
}
public void DataGrid_UpdateCommand(Object sender,DataGridCommandEventArgs e)
{
string strSQL = "update GuestInfo set"+"email=@email,password=@password where id=@id";
SqlCommand myComm = new SqlCommand(strSQL,myConn);
myComm.Parameters.Add(new SqlParameter("@password",SqlDbType.VarChar,50));
myComm.Parameters.Add(new SqlParameter("@email",SqlDbType.VarChar,50));
myComm.Parameters.Add(new SqlParameter("@id",SqlDbType.Int));
myComm.Parameters["@password"].Value=
((TextBox)e.Item.FindControl("password")).Text;
myComm.Parameters["@email"].Value=
((TextBox)e.Item.FindControl("email")).Text;
myComm.Parameters["@id"].Value=
Guest.DataKeys[(int)e.Item.ItemIndex];
myComm.Connection.Open();
try
{
myComm.ExecuteNonQuery();
Message.InnerHtml = "<b>编辑成功!</b>";
Guest.EditItemIndex=-1;
}
catch(SqlException)
{
Message.InnerHtml ="<b>编辑失败!</b>";
Message.Style["color"]="red";
}
myComm.Connection.Close();
BindGrid();
}
编译运行都没错,但就是更新按钮时总是提示“编辑失败”,为什么?