各位大哥好:
小弟学习中遇到如下错误:选中一行数据,点击删除按钮却数据无法删除。调试在 82行 cmd.ExecuteNonQuery ();语句出现异常:DELETE 语句与 REFERENCE 约束"FK_Order_Details_Products"冲突。该冲突发生于数据库"Northwind",表"dbo.Order Details", column 'ProductID'。
语句已终止。
后台代码如下using System;
using System.Configuration;
using System.Data;
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;
using System.IO;
using System.Data .SqlClient ;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page .IsPostBack )
{
bandexcel();
}
}
protected void bandexcel()
{
SqlConnection con=new SqlConnection (ConfigurationManager .ConnectionStrings ["dbexcel"].ConnectionString );
con.Open ();
string cmdtxt="select * from Products";
SqlDataAdapter sda=new SqlDataAdapter (cmdtxt ,con );
DataSet ds=new DataSet ();
sda.Fill (ds);
this.GridView1.DataSource =ds;
this.GridView1 .DataBind ();
con.Close ();
}
protected void btnout_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
} protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox cbox = (CheckBox)gr.FindControl("CheckBox1");
if (cbox.Checked == false)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
} }
protected void btndel_Click(object sender, EventArgs e)
{
btndel.Attributes.Add("onclick", "javasctript:return confirm('确认删除吗?')");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbexcel"].ConnectionString);
con.Open();
for(int i=0;i<=GridView1.Rows .Count -1;i++)
{
CheckBox check=(CheckBox )GridView1 .Rows [i].FindControl ("CheckBox1");
if(check.Checked ==true)
{
string sqlstr="delete from Products where ProductID='"+ GridView1 .DataKeys[i].Value+"'";
SqlCommand cmd=new SqlCommand (sqlstr,con);
cmd.ExecuteNonQuery ();
}
con .Close ();
bandexcel ();
}
}
protected void btncancel_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox check = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
check.Checked = false;
}
}
}
前台代码如下:
<%@ 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>
<style type="text/css">
.style1
{
width: 626px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 999px" >
<tr>
<td class="style1" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal" Width="995px" Height="186px" DataKeyNames ="ProductID"
onselectedindexchanged="CheckBox2_CheckedChanged">
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="商品ID号" />
<asp:BoundField DataField="ProductName" HeaderText="商品名称" />
<asp:BoundField DataField="UnitPrice" HeaderText="商品单价" />
<asp:BoundField DataField="SupplierID" HeaderText="商品数量" />
<asp:BoundField DataField="UnitPrice" HeaderText="商品总价" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt"
oncheckedchanged="CheckBox2_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView></td></tr>
<tr>
<td class="style1">
<asp:Button ID="btnout" runat="server" OnClick ="btnout_Click" Text="导出成EXCEL文件格式" />
<br />
<asp:Button ID="btndel" runat="server" onclick="btndel_Click" Text="删除" />
<asp:Button ID="btncancel" runat="server" onclick="btncancel_Click"
Text="取消删除" />
</td>
</tr>
</table>
</form>
</body>
</html>
问题是当选中一行数据时,无法删除选中数据。
小弟学习中遇到如下错误:选中一行数据,点击删除按钮却数据无法删除。调试在 82行 cmd.ExecuteNonQuery ();语句出现异常:DELETE 语句与 REFERENCE 约束"FK_Order_Details_Products"冲突。该冲突发生于数据库"Northwind",表"dbo.Order Details", column 'ProductID'。
语句已终止。
后台代码如下using System;
using System.Configuration;
using System.Data;
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;
using System.IO;
using System.Data .SqlClient ;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page .IsPostBack )
{
bandexcel();
}
}
protected void bandexcel()
{
SqlConnection con=new SqlConnection (ConfigurationManager .ConnectionStrings ["dbexcel"].ConnectionString );
con.Open ();
string cmdtxt="select * from Products";
SqlDataAdapter sda=new SqlDataAdapter (cmdtxt ,con );
DataSet ds=new DataSet ();
sda.Fill (ds);
this.GridView1.DataSource =ds;
this.GridView1 .DataBind ();
con.Close ();
}
protected void btnout_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
} protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox cbox = (CheckBox)gr.FindControl("CheckBox1");
if (cbox.Checked == false)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
} }
protected void btndel_Click(object sender, EventArgs e)
{
btndel.Attributes.Add("onclick", "javasctript:return confirm('确认删除吗?')");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbexcel"].ConnectionString);
con.Open();
for(int i=0;i<=GridView1.Rows .Count -1;i++)
{
CheckBox check=(CheckBox )GridView1 .Rows [i].FindControl ("CheckBox1");
if(check.Checked ==true)
{
string sqlstr="delete from Products where ProductID='"+ GridView1 .DataKeys[i].Value+"'";
SqlCommand cmd=new SqlCommand (sqlstr,con);
cmd.ExecuteNonQuery ();
}
con .Close ();
bandexcel ();
}
}
protected void btncancel_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox check = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
check.Checked = false;
}
}
}
前台代码如下:
<%@ 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>
<style type="text/css">
.style1
{
width: 626px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table style="width: 999px" >
<tr>
<td class="style1" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
CellPadding="4" GridLines="Horizontal" Width="995px" Height="186px" DataKeyNames ="ProductID"
onselectedindexchanged="CheckBox2_CheckedChanged">
<RowStyle BackColor="White" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="商品ID号" />
<asp:BoundField DataField="ProductName" HeaderText="商品名称" />
<asp:BoundField DataField="UnitPrice" HeaderText="商品单价" />
<asp:BoundField DataField="SupplierID" HeaderText="商品数量" />
<asp:BoundField DataField="UnitPrice" HeaderText="商品总价" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt"
oncheckedchanged="CheckBox2_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
</asp:GridView></td></tr>
<tr>
<td class="style1">
<asp:Button ID="btnout" runat="server" OnClick ="btnout_Click" Text="导出成EXCEL文件格式" />
<br />
<asp:Button ID="btndel" runat="server" onclick="btndel_Click" Text="删除" />
<asp:Button ID="btncancel" runat="server" onclick="btncancel_Click"
Text="取消删除" />
</td>
</tr>
</table>
</form>
</body>
</html>
问题是当选中一行数据时,无法删除选中数据。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货