| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 672 人关注过本帖
标题:[求助]为何我的代码DataGrid更新失败?
取消只看楼主 加入收藏
ckl_20147
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-5-15
收藏
 问题点数:0 回复次数:2 
[求助]为何我的代码DataGrid更新失败?

在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();
}

编译运行都没错,但就是更新按钮时总是提示“编辑失败”,为什么?

搜索更多相关主题的帖子: DataGrid 代码 失败 
2006-10-11 18:02
ckl_20147
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-5-15
收藏
得分:0 

还是有错误,
错误信息:System.Data.SqlClient.SqlException: 第 1 行: 'setemail' 附近有语法错误。
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at applic.DataGridEditColumn2.DataGrid_UpdateCommand(Object sender, DataGridCommandEventArgs e) in e:\aspweb\applic\datagrideditcolumn2.aspx.cs:line 69
望赐教!

[此贴子已经被作者于2006-10-11 22:28:11编辑过]

2006-10-11 22:27
ckl_20147
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-5-15
收藏
得分:0 

可以了,多谢mylover624的提示。Thx!
string strSQL = "update GuestInfo set"+"email=@email,password=@password where id=@id";
加个空格就OK了
string strSQL = "update GuestInfo set"+" email=@email,password=@password where id=@id";

2006-10-11 22:32
快速回复:[求助]为何我的代码DataGrid更新失败?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029692 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved