//这为aspx文件
<%@ Page language="c#" Codebehind="update.aspx.cs" AutoEventWireup="false" Inherits="seven.update" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>update</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 144px"
runat="server" Height="296px" Width="528px">
<Columns>
<asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="更新" />
</Columns>
</asp:datagrid><asp:label id="lblMessage" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 480px"
runat="server" Height="32px" Width="464px"></asp:label></FONT></form>
</body>
</HTML>
//这为cs文件。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace seven
{
/// <summary>
/// update 的摘要说明。
/// </summary>
public class update : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label lblMessage;
protected SqlConnection mySqlCon;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Write("<center><u><b>更新数据</b></u></center>");
mySqlCon = new SqlConnection("uid = sa; pwd = sa; initial catalog = pubs;data source =.");
if(!this.IsPostBack) BindGrid();
}
void BindGrid()
{
SqlDataAdapter SqlCom = new SqlDataAdapter("select * from publishers",mySqlCon);
DataSet myds = new DataSet();
SqlCom.Fill(myds,"publishers");
DataGrid1.DataSource = myds.Tables["publishers"].DefaultView;
DataGrid1.DataBind();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.EditCommand += new DataGridCommandEventHandler(this.dgMyGrid_Edit);
this.DataGrid1.CancelCommand += new DataGridCommandEventHandler(this.dgMyGrid_Cancel);
this.DataGrid1.UpdateCommand += new DataGridCommandEventHandler(this.dgMyGrid_Update);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void dgMyGrid_Edit(object sender,DataGridCommandEventArgs e)
{
DataGrid1.Columns[0].HeaderText = "编辑";
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex; //表示该可以编辑.
BindGrid();
}
private void dgMyGrid_Cancel(object sender,DataGridCommandEventArgs e)
{
DataGrid1.Columns[0].HeaderText = "取消";
DataGrid1.EditItemIndex = -1;
BindGrid();
}
private void dgMyGrid_Update(object render,DataGridCommandEventArgs e)
{
DataGrid1.Columns[0].HeaderText = "更新";
string updateCmd = "update publishers set pub_id = @pubid, pub_name = @pubname, city = @city, state = @state, country = @country where pub_id = @pubid";
SqlCommand mySqlCmd = new SqlCommand(updateCmd,mySqlCon); mySqlCmd.Parameters.Add(new SqlParameter("@pubid",SqlDbType.Char,4));
mySqlCmd.Parameters.Add(new SqlParameter("@pubname",SqlDbType.VarChar,40));
mySqlCmd.Parameters.Add(new SqlParameter("@city",SqlDbType.VarChar,20));
mySqlCmd.Parameters.Add(new SqlParameter("@state",SqlDbType.Char,2));
mySqlCmd.Parameters.Add(new SqlParameter("@country",SqlDbType.VarChar,30)); mySqlCmd.Parameters["@pubid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex]; //不可编辑。
mySqlCmd.Parameters["@pubname"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
mySqlCmd.Parameters["@city"].Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
mySqlCmd.Parameters["@state"].Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text;
mySqlCmd.Parameters["@country"].Value = ((TextBox)e.Item.Cells[5].Controls[0]).Text; mySqlCmd.Connection.Open();
try
{
mySqlCmd.ExecuteNonQuery();
lblMessage.Text = "<b>已更新记录</b><br/>";
this.DataGrid1.EditItemIndex = -1;
}
catch(SqlException exc)
{
if(exc.Number == 2627)
lblMessage.Text = "错误,已存在一休具有相同主键的记录!";
else
lblMessage.Text = exc.ToString() + "错误:无法更新记录,请确保正确填写字段!";
}
mySqlCmd.Connection.Close();
BindGrid();
}
}
}为什么更新的时候就错呢?
求教!!!!!!!!!!

解决方案 »

  1.   

    mySqlCmd.Parameters["@pubid"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
    ======
    pub_id是自动编号吗?如果是,是不能编辑的,如果不是估计是这里错误mySqlCmd.Parameters["@pubid"].Value = DataGrid1.DataKeys[e.Item.ItemIndex];这样看看。
      

  2.   

    <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 208px; POSITION: absolute; TOP: 144px"
    runat="server" Height="296px" Width="528px">设置一下datagrid 的主键.....
    datakey
      

  3.   

    我知道啦
    DataGrid1.DataKeysField = "pub_id";