当我执行“更新”OnUpdateCommand="DataGrid_UpdateCommand"的时候
我原本正常的数据库,结果却是所有的数据都丢失。网站名,网站网址, 网站介绍,网站评分 的数据全部丢失,而网站评分这一列全部变成我更新的数据
举例1 网站名1 网址1 网站介绍1 网站评分1
2 网站名2 网址2 网站介绍2 网站评分2当我编辑第二行,然后修改完,网站名和其他所有数据之后,点击“更新”,这时候结果就是,第1行和第2行,一直到n行的数据都是空白,只有网站评分不是空白,但是所有记录都变成了我刚才编辑的那一行的分数!!!错到哪里呢?请懂的朋友帮助一下我这个菜鸟。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><script language="C#" runat="Server">
SqlConnection MyConn;
public void Page_Load(Object src, EventArgs e)
{
//连接语句
string MyConnString = "server=microsof-a27fdb; database=test; user id=sa; password=sdgdsg243545fdgdfgssd43535gdfgsdgsdf2454p;";
MyConn = new SqlConnection(MyConnString);
MyConn.Open(); if (!Page.IsPostBack)
{
BindGrid();
}
}
ICollection CreateTable()
{
string strSel = "select * from 网站清单";
DataSet ds = new DataSet(); SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, "新表名"); return ds.Tables["新表名"].DefaultView;
}
public void BindGrid()
{
score.DataSource = CreateTable();
score.DataBind();
} //处理Edit命令
public void DataGrid_EditCommand(Object sender, DataGridCommandEventArgs e)
{
score.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
} //处理Cancel命令
public void DataGrid_CancelCommand(Object sender, DataGridCommandEventArgs e)
{
score.EditItemIndex = -1;
BindGrid();
} //处理Update命令
public void DataGrid_UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
//更新数据库中的信息
string web_name = e.Item.Cells[2].Text;
string web_address = e.Item.Cells[3].Text;
string web_jieshao = e.Item.Cells[4].Text;
int web_defen = Int32.Parse(((TextBox)e.Item.Cells[5].Controls[0]).Text); //更新数据库中的数据
string strUpdate = "Update 网站清单 Set 网站名='"+web_name +"',网站网址='"+web_address+ "',网站介绍='"+ web_jieshao +"',网站评分= "+web_defen ;
SqlCommand MyComm = new SqlCommand(strUpdate, MyConn);
MyComm.ExecuteNonQuery(); score.EditItemIndex = -1;
BindGrid(); }
</script><html>
<head>
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<div align="center">
<b>演示EditCommandColumn</b>
<asp:DataGrid ID="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False" OnEditCommand="DataGrid_EditCommand" OnUpdateCommand="DataGrid_UpdateCommand"
OnCancelCommand="DataGrid_CancelCommand">
<Columns>
<asp:EditCommandColumn HeaderText="操作区" EditText="编辑" UpdateText="更新" CancelText="取消"
ButtonType="PushButton" />
<asp:BoundColumn HeaderText="ID" DataField="网站id" ReadOnly="True" />
<asp:BoundColumn HeaderText="网站名称" DataField="网站名" />
<asp:BoundColumn HeaderText="网址" DataField="网站网址" />
<asp:BoundColumn HeaderText="介绍" DataField="网站介绍" />
<asp:BoundColumn HeaderText="评分" DataField="网站评分" />
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
我原本正常的数据库,结果却是所有的数据都丢失。网站名,网站网址, 网站介绍,网站评分 的数据全部丢失,而网站评分这一列全部变成我更新的数据
举例1 网站名1 网址1 网站介绍1 网站评分1
2 网站名2 网址2 网站介绍2 网站评分2当我编辑第二行,然后修改完,网站名和其他所有数据之后,点击“更新”,这时候结果就是,第1行和第2行,一直到n行的数据都是空白,只有网站评分不是空白,但是所有记录都变成了我刚才编辑的那一行的分数!!!错到哪里呢?请懂的朋友帮助一下我这个菜鸟。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><script language="C#" runat="Server">
SqlConnection MyConn;
public void Page_Load(Object src, EventArgs e)
{
//连接语句
string MyConnString = "server=microsof-a27fdb; database=test; user id=sa; password=sdgdsg243545fdgdfgssd43535gdfgsdgsdf2454p;";
MyConn = new SqlConnection(MyConnString);
MyConn.Open(); if (!Page.IsPostBack)
{
BindGrid();
}
}
ICollection CreateTable()
{
string strSel = "select * from 网站清单";
DataSet ds = new DataSet(); SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, "新表名"); return ds.Tables["新表名"].DefaultView;
}
public void BindGrid()
{
score.DataSource = CreateTable();
score.DataBind();
} //处理Edit命令
public void DataGrid_EditCommand(Object sender, DataGridCommandEventArgs e)
{
score.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
} //处理Cancel命令
public void DataGrid_CancelCommand(Object sender, DataGridCommandEventArgs e)
{
score.EditItemIndex = -1;
BindGrid();
} //处理Update命令
public void DataGrid_UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
//更新数据库中的信息
string web_name = e.Item.Cells[2].Text;
string web_address = e.Item.Cells[3].Text;
string web_jieshao = e.Item.Cells[4].Text;
int web_defen = Int32.Parse(((TextBox)e.Item.Cells[5].Controls[0]).Text); //更新数据库中的数据
string strUpdate = "Update 网站清单 Set 网站名='"+web_name +"',网站网址='"+web_address+ "',网站介绍='"+ web_jieshao +"',网站评分= "+web_defen ;
SqlCommand MyComm = new SqlCommand(strUpdate, MyConn);
MyComm.ExecuteNonQuery(); score.EditItemIndex = -1;
BindGrid(); }
</script><html>
<head>
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<div align="center">
<b>演示EditCommandColumn</b>
<asp:DataGrid ID="score" runat="server" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="False" OnEditCommand="DataGrid_EditCommand" OnUpdateCommand="DataGrid_UpdateCommand"
OnCancelCommand="DataGrid_CancelCommand">
<Columns>
<asp:EditCommandColumn HeaderText="操作区" EditText="编辑" UpdateText="更新" CancelText="取消"
ButtonType="PushButton" />
<asp:BoundColumn HeaderText="ID" DataField="网站id" ReadOnly="True" />
<asp:BoundColumn HeaderText="网站名称" DataField="网站名" />
<asp:BoundColumn HeaderText="网址" DataField="网站网址" />
<asp:BoundColumn HeaderText="介绍" DataField="网站介绍" />
<asp:BoundColumn HeaderText="评分" DataField="网站评分" />
</Columns>
</asp:DataGrid>
</div>
</form>
</body>
</html>
解决方案 »
- 应用程序中的服务器错误
- asp.net关于同一个页面传值问题
- 请教各位老大了,一个关于datagrid的问题!
- request 接受 xmlhttp post 过来的 requeststring 字段 ,数字显示正常 ,中文是一对乱码,怎么回事 ?
- 高手来看,excel生成问
- 一个布局的问题 (div)
- 使用asp.net 自带的用户管理数据库 如何使用membership 获得10位最新注册的用户信息?
- VS2010,调试一个项目,打开的网页时空白的,一直处于加载状态,什么情况这是?
- 请教,我如何得到这个数据源呢?
- 如何获得一个目录下的所有文件的文件名?拜托了,c#写的!!
- 关于ASP。NET占用内存量大,以及程序中用的变量释放内存的问题。
- =====Grid View 中怎么隐藏列?使用代码隐藏会和分页起冲突=====
没有更新条件
全部更新了,应该
string id=e.item.cells[0].text.trim();
string strUpdate = "Update 网站清单 Set 网站名='"+web_name +"',网站网址='"+web_address+ "',网站介绍='"+ web_jieshao +"',网站评分= "+web_defen +" where
id="+id;
id为数据库的主键
你的SQL语句怎么没有where?当然全部更新了!!!
Dim * As TextBox
* = e.Item.Cells(2).Controls(0或者其他,视你的DATAGRID而定),
再取*的Text
//更新数据库中的信息
int web_id = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);
string web_name = e.Item.Cells[2].Text;
string web_address = e.Item.Cells[3].Text;
string web_jieshao = e.Item.Cells[4].Text;
int web_defen = Int32.Parse(((TextBox)e.Item.Cells[5].Controls[0]).Text); //更新数据库中的数据
string strUpdate = "Update 网站清单 Set 网站名='"+web_name +"',网站网址='"+web_address+ "',网站介绍='"+ web_jieshao +"',网站评分="+web_defen +" where [网站id]="+web_id;
SqlCommand MyComm = new SqlCommand(strUpdate, MyConn);
MyComm.ExecuteNonQuery(); score.EditItemIndex = -1;
BindGrid();
执行更新报错!
指定的参数已超出有效值的范围。
参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index源错误:
行 52: {
行 53: //更新数据库中的信息
行 54: int web_id = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);
行 55: string web_name = e.Item.Cells[2].Text;
行 56: string web_address = e.Item.Cells[3].Text;
源文件: d:\xuexi100\model\DataGridEditCommandColumn.aspx 行: 54 堆栈跟踪:
[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index]
System.Web.UI.ControlCollection.get_Item(Int32 index) +75
ASP.model_datagrideditcommandcolumn_aspx.DataGrid_UpdateCommand(Object sender, DataGridCommandEventArgs e) in d:\xuexi100\model\DataGridEditCommandColumn.aspx:54
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e) +96
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e) +605
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e) +119
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +107
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +179
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3840
int web_id = Int32.Parse(((TextBox)e.Item.Cells[1].Controls[0]).Text);报错
“System.ArgumentOutOfRangeException”类型的异常在 System.Web.dll 中发生,但未在用户代码中进行处理其他信息: 指定的参数已超出有效值的范围。