ASP.NET中RowDeleting删除行的后台代码?
我的后台代码为protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse){SqlC...
我的后台代码为
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection("Server=(local);database=图书馆管理系统;uid=sa;pwd=1111");
string strSql = "delete from 图书信息表 where 图书编号= '" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script language='javascript'>alert('删除成功!')</script>");
Bind();
}
可是提示一个错误!
请问是哪出错了? 展开
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection("Server=(local);database=图书馆管理系统;uid=sa;pwd=1111");
string strSql = "delete from 图书信息表 where 图书编号= '" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script language='javascript'>alert('删除成功!')</script>");
Bind();
}
可是提示一个错误!
请问是哪出错了? 展开
3个回答
2011-07-06
展开全部
<html xmlns="" >
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 72%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<br />
<br />
<br />
<br />
<br />
<br />
<div align=center>
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td colspan="2" align=center style="background-image:url(../img/titlebg.gif)">
友情链接管理</td>
</tr>
<tr>
<td align=right width=200>
网站名称:</td>
<td align=left>
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="网址名称不能为空"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right>
网站地址:</td>
<td align=left>
<asp:TextBox ID="TextBox2" runat="server"
Width="300px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="网址格式不正确"
ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="重置" onclick="Button2_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="726px" CellPadding="4" ForeColor="#333333" GridLines="None"
DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="WName" HeaderText="网站名称" />
<asp:BoundField DataField="WAddress" HeaderText="网站地址" />
<asp:BoundField DataField="time" HeaderText="加入时间" />
<asp:CommandField ShowEditButton="True" HeaderText="更改"
CausesValidation="False" />
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="shan"
CommandArgument='<%# Eval("id") %>' CausesValidation="False">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
namespace xzbags.Admin
{
public partial class FrientsManaber : System.Web.UI.Page
{
User uu = new User();
UsersModel um = new UsersModel();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定
public void bind()
{
DataTable dt = uu.Friends();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.ToString();
string address = TextBox2.Text.ToString();
DateTime time = Convert.ToDateTime(System.DateTime.Now.ToString("yy-MM-dd hh:mm:ss"));
um.WName = name;
um.Waddress = address;
um.time = time;
if (uu.AddFriends(um) > 0)
{
bind();
TextBox1.Text = "";
TextBox2.Text = "";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument.ToString());
if (e.CommandName == "shan")
{
um.id = id;
if (uu.deletefriends(um, id) > 0)
{
bind();
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string time = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
um.id = id;
um.WName = name;
um.Waddress =address;
if (uu.updatefriends(um, id) > 0)
{
GridView1.EditIndex = -1;
bind();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
}
}
调用的类里的数据库语句。。你创建个类,自己写增删改语句吧。。
另外,团IDC网上有许多产品团购,便宜有口碑
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.style1
{
width: 72%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<br />
<br />
<br />
<br />
<br />
<br />
<div align=center>
<table cellpadding="0" cellspacing="0" class="style1">
<tr>
<td colspan="2" align=center style="background-image:url(../img/titlebg.gif)">
友情链接管理</td>
</tr>
<tr>
<td align=right width=200>
网站名称:</td>
<td align=left>
<asp:TextBox ID="TextBox1" runat="server" Width="300px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="网址名称不能为空"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right>
网站地址:</td>
<td align=left>
<asp:TextBox ID="TextBox2" runat="server"
Width="300px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="网址格式不正确"
ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="重置" onclick="Button2_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="726px" CellPadding="4" ForeColor="#333333" GridLines="None"
DataKeyNames="id" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="WName" HeaderText="网站名称" />
<asp:BoundField DataField="WAddress" HeaderText="网站地址" />
<asp:BoundField DataField="time" HeaderText="加入时间" />
<asp:CommandField ShowEditButton="True" HeaderText="更改"
CausesValidation="False" />
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="shan"
CommandArgument='<%# Eval("id") %>' CausesValidation="False">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
namespace xzbags.Admin
{
public partial class FrientsManaber : System.Web.UI.Page
{
User uu = new User();
UsersModel um = new UsersModel();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
//绑定
public void bind()
{
DataTable dt = uu.Friends();
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.ToString();
string address = TextBox2.Text.ToString();
DateTime time = Convert.ToDateTime(System.DateTime.Now.ToString("yy-MM-dd hh:mm:ss"));
um.WName = name;
um.Waddress = address;
um.time = time;
if (uu.AddFriends(um) > 0)
{
bind();
TextBox1.Text = "";
TextBox2.Text = "";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument.ToString());
if (e.CommandName == "shan")
{
um.id = id;
if (uu.deletefriends(um, id) > 0)
{
bind();
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string time = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
um.id = id;
um.WName = name;
um.Waddress =address;
if (uu.updatefriends(um, id) > 0)
{
GridView1.EditIndex = -1;
bind();
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
}
}
调用的类里的数据库语句。。你创建个类,自己写增删改语句吧。。
另外,团IDC网上有许多产品团购,便宜有口碑
展开全部
语句看上去没什么错,如果你的图书编号是整型的话,建议把单引号去掉!除此之外,请确保你的连接能够连接上数据,最好把错误信息放上来!
---看到你的图片了
索引超过,你设置了gridView的DataKeys没有
---看到你的图片了
索引超过,你设置了gridView的DataKeys没有
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
调试运行一下嘛。
看一看 GridView1.DataKeys[e.RowIndex].Value
到底值是什么。
如果报错,就看看 GridView1.DataKeys[e.RowIndex-1].Value
看一看 GridView1.DataKeys[e.RowIndex].Value
到底值是什么。
如果报错,就看看 GridView1.DataKeys[e.RowIndex-1].Value
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询