急急急。。。高分求解。。。。。。。。。在一个页面中做增删除改查,用GridView显示查询的数据 现在做好了增加查询,还有删除和更新没做,各位高手帮帮忙,谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就是对一个表test(id,title,content)进行增删改查 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataAccess dataAccess = new DataAccess(); string connectionString = dataAccess.DBConnString; SqlConnection cn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("delete from admin where id=@id", cn); cmd.Parameters.Add("@id", SqlDbType.Int).Value = GridView1.DataKeys[e.RowIndex].Value.ToString(); cn.Open(); try { cmd.ExecuteNonQuery(); Response.Write("<script language=javascript>window.alert('删除成功!');window.location.href=('Member.aspx');</script>"); } catch (SqlException) { Response.Write("<script language=javascript>window.alert('删除失败!');location='javascript:history.go(-1)';</script>"); } GridView1.DataKeyNames[0] = "visualize_id"; GridView1.EditIndex = -1; 绑定代码的一个方法(); protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { DataAccess dataAccess = new DataAccess(); string connectionString = dataAccess.DBConnString; SqlConnection cn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("update admin set UserName=@UserName,PassWord=@PassWord where id=@id", cn); //cmd.CommandType = CommandType.StoredProcedure; GridViewRow myrow = GridView1.Rows[e.RowIndex]; cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = ((TextBox)myrow.Cells[1].Controls[0]).Text; cmd.Parameters.Add("@PassWord", SqlDbType.VarChar).Value = ((TextBox)myrow.Cells[2].Controls[0]).Text; cmd.Parameters.Add("@id", SqlDbType.Int).Value = gvCategoryList.DataKeys[e.RowIndex].Values[0].ToString(); cn.Open(); try { cmd.ExecuteNonQuery(); Response.Write("<script language=javascript>window.alert('更新成功!');window.location.href=('Member.aspx');</script>"); } catch (SqlException) { Response.Write("<script language=javascript>window.alert('更新失败!');location='javascript:history.go(-1)';</script>"); } GridView1.DataKeyNames[0] = "id"; cn.Close(); GridView1.EditIndex = -1; 绑定代码的一个方法(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { SqlConnection conn = CreatConnection(); string sqlstr = "Update Goods Set Goods_Name='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Goods_Price='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Goods_Type='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Goods_Count='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Re='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' Where Goods_ID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; SqlCommand cmd = new SqlCommand(sqlstr, conn); cmd.ExecuteNonQuery(); conn.Close(); GridView1.EditIndex = -1; GridView1.DataBind(); } GridView1.DataKeys[e.RowIndex].Value.ToString();里 e.RowIndex没这个属性 你用的是GridView的控件吗?而且一定是RowDeleting事件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <table cellpadding="0" cellspacing="0" style="height: 55px"> <tr> <td align="right" style="width: 76px; height: 37px"> 标题:</td> <td style="height: 37px"> <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td> <td align="right" style="width: 73px; height: 37px">内容:</td> <td style="height: 37px"><asp:TextBox ID="txtContent" runat="server"></asp:TextBox> <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label></td> </tr> <tr> <td colspan="4"> <asp:GridView ID="GridView_test" runat="server" AutoGenerateColumns="False" Width="743px" OnRowDataBound="GridView_test_RowDataBound" OnRowDeleted="GridView_test_RowDeleted"> <Columns> <asp:TemplateField HeaderText="选择"> <ItemTemplate><div style="width: 100%; height: 18px;text-align:center;"> <asp:CheckBox ID="CheckBox_Edit" runat="server" Text='<%# Eval("ID") %>' Font-Size="0pt" ForeColor="white"/></div> </ItemTemplate> <ControlStyle Width="40px" /> <ItemStyle Width="40px" /> <HeaderStyle Width="40px" /> </asp:TemplateField> <asp:BoundField DataField="ID" HeaderText="编号" > <ItemStyle Width="50px" /> </asp:BoundField> <asp:BoundField DataField="title" HeaderText="标题" > <ItemStyle Width="100px" /> </asp:BoundField> <asp:BoundField DataField="contents" HeaderText="内容" > <ItemStyle Width="100px" /> </asp:BoundField> <asp:TemplateField HeaderText="更新"> <ItemTemplate><div style="width: 100%; height: 18px;text-align:center;"> <asp:HyperLink ID="HyperLink_Update" runat="server" Text="" NavigateUrl="~/Default.aspx">更新</asp:HyperLink></div> </ItemTemplate> <ControlStyle Width="40px" /> <ItemStyle Width="40px" /> <HeaderStyle Width="40px" /> </asp:TemplateField> <asp:TemplateField HeaderText="删除"> <ItemTemplate><div style="width: 100%; height: 18px;text-align:center;"> <asp:LinkButton ID="LinkButton_Del" runat="server" Text="删除" OnClick="LinkButton_Del_Click" OnClientClick="return confirm('确定要删除吗?')"/> </div> </ItemTemplate> <ControlStyle Width="40px" /> <ItemStyle Width="40px" /> <HeaderStyle Width="40px" /> </asp:TemplateField> </Columns> </asp:GridView></td> </tr> <tr> <td colspan="4" style="height: 54px" align="right"> <asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" /> <asp:Button ID="btnUpdate" runat="server" Text="更新" OnClick="btnUpdate_Click" /> <asp:Button ID="btnSelect" runat="server" Text="查询" OnClick="btnSelect_Click" /> <asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" /> </td> </tr> </table> </form></body></html> 你的更新和删除前台用的是 <asp:TemplateField HeaderText="更新"> <asp:TemplateField HeaderText="删除"> 这两个吗 <asp:CommandField ShowDeleteButton="True" HeaderText="删除" > <ItemStyle HorizontalAlign="Left" Width="30px" /> </asp:CommandField> <asp:CommandField ShowEditButton="True" HeaderText="更改" /> 其实GridView是一个很实用的控件,你如果真的是工作很多年了话,应该对GridView很熟悉才对的 以前是用VS03,最近才用05的,虽然和DataGrid相似,但是比DataGrid功能大多了 还有点问题cmd.Parameters.Add("@delid", SqlDbType.Int).Value = GridView_test.DataKeys[e.RowIndex].Value.ToString();delid是个INT型的 索引超出范围。必须为非负值并小于集合大小。参数名: index 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index源错误: 行 171: string delstr = "delete from test where id = @delid";行 172: SqlCommand cmd = new SqlCommand(delstr, con);行 173: cmd.Parameters.Add("@delid", SqlDbType.Int).Value = GridView_test.DataKeys[e.RowIndex].Value.ToString();行 174:行 175: try 你们两个在干嘛?你一句我一句的索引超出范围。必须为非负值并小于集合大小。 指定数据源的时候是否设置了主键GridView1.DataKeyNames = new string[] { "ID" }; 大家进来帮忙看看 gridview 行ONCLICK事件 ASP.NET FTP问题 关于网站登陆的时候,Session失效的问题???(紧急!!!解决给分!!!) 发送邮件 关于form验证的问题 高手帮我看一个SQl语句的小问题 初学者的疑问 怎么用OracleDataAdapter执行Oracle动态存储过程生成DataSset?请高手指教 如何对水晶报表的上一页/下一页事件进行编码?急... 如何实现站内自动发送短消息, asp.net +access 着急啊! C# 数据结构 视频
{
DataAccess dataAccess = new DataAccess();
string connectionString = dataAccess.DBConnString; SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("delete from admin where id=@id", cn); cmd.Parameters.Add("@id", SqlDbType.Int).Value = GridView1.DataKeys[e.RowIndex].Value.ToString();
cn.Open(); try
{
cmd.ExecuteNonQuery();
Response.Write("<script language=javascript>window.alert('删除成功!');window.location.href=('Member.aspx');</script>");
}
catch (SqlException)
{
Response.Write("<script language=javascript>window.alert('删除失败!');location='javascript:history.go(-1)';</script>");
} GridView1.DataKeyNames[0] = "visualize_id";
GridView1.EditIndex = -1;
绑定代码的一个方法();
{ DataAccess dataAccess = new DataAccess();
string connectionString = dataAccess.DBConnString; SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("update admin set UserName=@UserName,PassWord=@PassWord where id=@id", cn); //cmd.CommandType = CommandType.StoredProcedure; GridViewRow myrow = GridView1.Rows[e.RowIndex]; cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = ((TextBox)myrow.Cells[1].Controls[0]).Text;
cmd.Parameters.Add("@PassWord", SqlDbType.VarChar).Value = ((TextBox)myrow.Cells[2].Controls[0]).Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = gvCategoryList.DataKeys[e.RowIndex].Values[0].ToString();
cn.Open();
try
{
cmd.ExecuteNonQuery();
Response.Write("<script language=javascript>window.alert('更新成功!');window.location.href=('Member.aspx');</script>");
}
catch (SqlException)
{
Response.Write("<script language=javascript>window.alert('更新失败!');location='javascript:history.go(-1)';</script>");
} GridView1.DataKeyNames[0] = "id"; cn.Close();
GridView1.EditIndex = -1;
绑定代码的一个方法(); }
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = CreatConnection();
string sqlstr = "Update Goods Set Goods_Name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',Goods_Price='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',Goods_Type='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',Goods_Count='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Re='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "' Where Goods_ID='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(sqlstr, conn); cmd.ExecuteNonQuery();
conn.Close();
GridView1.EditIndex = -1;
GridView1.DataBind();
}
GridView1.DataKeys[e.RowIndex].Value.ToString();里 e.RowIndex没这个属性
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<table cellpadding="0" cellspacing="0" style="height: 55px">
<tr>
<td align="right" style="width: 76px; height: 37px"> 标题:</td>
<td style="height: 37px"> <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox></td>
<td align="right" style="width: 73px; height: 37px">内容:</td>
<td style="height: 37px"><asp:TextBox ID="txtContent" runat="server"></asp:TextBox>
<asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label></td>
</tr>
<tr>
<td colspan="4"> <asp:GridView ID="GridView_test" runat="server" AutoGenerateColumns="False"
Width="743px" OnRowDataBound="GridView_test_RowDataBound" OnRowDeleted="GridView_test_RowDeleted">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate><div style="width: 100%; height: 18px;text-align:center;">
<asp:CheckBox ID="CheckBox_Edit" runat="server" Text='<%# Eval("ID") %>' Font-Size="0pt" ForeColor="white"/></div>
</ItemTemplate>
<ControlStyle Width="40px" />
<ItemStyle Width="40px" />
<HeaderStyle Width="40px" />
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="编号" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="title" HeaderText="标题" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="contents" HeaderText="内容" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:TemplateField HeaderText="更新">
<ItemTemplate><div style="width: 100%; height: 18px;text-align:center;">
<asp:HyperLink ID="HyperLink_Update" runat="server" Text="" NavigateUrl="~/Default.aspx">更新</asp:HyperLink></div>
</ItemTemplate>
<ControlStyle Width="40px" />
<ItemStyle Width="40px" />
<HeaderStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate><div style="width: 100%; height: 18px;text-align:center;">
<asp:LinkButton ID="LinkButton_Del" runat="server" Text="删除" OnClick="LinkButton_Del_Click" OnClientClick="return confirm('确定要删除吗?')"/>
</div>
</ItemTemplate>
<ControlStyle Width="40px" />
<ItemStyle Width="40px" />
<HeaderStyle Width="40px" />
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
<tr>
<td colspan="4" style="height: 54px" align="right">
<asp:Button ID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="更新" OnClick="btnUpdate_Click" />
<asp:Button ID="btnSelect" runat="server" Text="查询" OnClick="btnSelect_Click" />
<asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
<asp:TemplateField HeaderText="删除"> 这两个吗
<ItemStyle HorizontalAlign="Left" Width="30px" />
</asp:CommandField>
<asp:CommandField ShowEditButton="True" HeaderText="更改" />
cmd.Parameters.Add("@delid", SqlDbType.Int).Value = GridView_test.DataKeys[e.RowIndex].Value.ToString();delid是个INT型的
参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index源错误:
行 171: string delstr = "delete from test where id = @delid";
行 172: SqlCommand cmd = new SqlCommand(delstr, con);
行 173: cmd.Parameters.Add("@delid", SqlDbType.Int).Value = GridView_test.DataKeys[e.RowIndex].Value.ToString();
行 174:
行 175: try
GridView1.DataKeyNames = new string[] { "ID" };