asp.net 用gridview控件进行对数据的增删改时发生的异常(插入重复键等),在哪个事件中获取?
展开全部
发段代码给你参考下
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="geidviewEd.aspx.cs" Inherits="geidviewEd" %>
<!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>
<style type="text/css">
#GridView1
{
text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="387px"
DataKeyNames="id" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated"
onrowupdating="GridView1_RowUpdating">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="adminname" HeaderText="用户名" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class geidviewEd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
string SqlCon = @"server=IBM-THINK\MSSQL2008;DataBase=okdangao;uid=sa;pwd=";
string sql = "select * from admin";
DataSet ds = new DataSet();
SqlConnection SqlConstr = new SqlConnection(SqlCon);
SqlDataAdapter da = new SqlDataAdapter(sql,SqlConstr);
SqlConstr.Open();
da.Fill(ds);
SqlConstr.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string aId = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取datakeynames中隐藏的id值然后执行数据库操作
string adminname=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string sqlupdate = "update admin set adminname='"+adminname+"'where id="+aId+"";
string SqlCon = @"server=IBM-THINK\MSSQL2008;DataBase=okdangao;uid=sa;pwd=";
SqlConnection SqlConstr = new SqlConnection(SqlCon);
SqlCommand cmd = new SqlCommand(sqlupdate,SqlConstr);
SqlConstr.Open();
int fay = int.Parse(cmd.ExecuteNonQuery().ToString());
SqlConstr.Close();
if (fay > 0)
{
Response.Write("<script language='javascript'>alert('修改成功')</script>");
}
else
{
Response.Write("<script language='javascript'>alert('修改失败')</script>");
}
GridView1.EditIndex = -1;//最后取消编辑状态
BindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int tid =int.Parse( GridView1.DataKeys[e.RowIndex].Value.ToString());
//然后执行数据库操作
}
}
希望对你有用
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="geidviewEd.aspx.cs" Inherits="geidviewEd" %>
<!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>
<style type="text/css">
#GridView1
{
text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="387px"
DataKeyNames="id" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated"
onrowupdating="GridView1_RowUpdating">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="adminname" HeaderText="用户名" />
<asp:CommandField HeaderText="修改" ShowEditButton="True" />
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class geidviewEd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
protected void BindData()
{
string SqlCon = @"server=IBM-THINK\MSSQL2008;DataBase=okdangao;uid=sa;pwd=";
string sql = "select * from admin";
DataSet ds = new DataSet();
SqlConnection SqlConstr = new SqlConnection(SqlCon);
SqlDataAdapter da = new SqlDataAdapter(sql,SqlConstr);
SqlConstr.Open();
da.Fill(ds);
SqlConstr.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string aId = GridView1.DataKeys[e.RowIndex].Value.ToString();//获取datakeynames中隐藏的id值然后执行数据库操作
string adminname=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string sqlupdate = "update admin set adminname='"+adminname+"'where id="+aId+"";
string SqlCon = @"server=IBM-THINK\MSSQL2008;DataBase=okdangao;uid=sa;pwd=";
SqlConnection SqlConstr = new SqlConnection(SqlCon);
SqlCommand cmd = new SqlCommand(sqlupdate,SqlConstr);
SqlConstr.Open();
int fay = int.Parse(cmd.ExecuteNonQuery().ToString());
SqlConstr.Close();
if (fay > 0)
{
Response.Write("<script language='javascript'>alert('修改成功')</script>");
}
else
{
Response.Write("<script language='javascript'>alert('修改失败')</script>");
}
GridView1.EditIndex = -1;//最后取消编辑状态
BindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int tid =int.Parse( GridView1.DataKeys[e.RowIndex].Value.ToString());
//然后执行数据库操作
}
}
希望对你有用
展开全部
error事件中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把你要导入的表设置一个主键就可以了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询