Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > BOOK: Beginning ASP.NET Databases
|
BOOK: Beginning ASP.NET Databases Also see the forum ASP Databases for more general discussions of ASP database issues not directly related to these books.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 30th, 2005, 07:58 AM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default ch 7- Delete_Datagrid.aspx - The conflict occurred

Hi

I'm totally stumped by this one. I have done nothing to the code of ch07/Delete_Datagrid.aspx except change the connection.

The page comes up ok but when I try to delete something I keep on getting the following error::

DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_Order_Details_Products'. The conflict occurred in database 'Northwind', table 'Order Details', column 'ProductID'. The statement has been terminated.

Is this a database problem or a code problem.

The code is::
Code:
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>

<html>
  <head>
    <title>Updating Beverages</title>
  </head>
  <body>
    <form method="post" runat="server">
      <asp:DataGrid id="dgProducts" runat="server"
                    CellPadding="5" AutoGenerateColumns="False"
                    OnEditCommand="EditRecord"
                    OnCancelCommand="CancelEdit"
                    OnUpdateCommand="UpdateRecord"
                    OnDeleteCommand="DeleteRecord">
        <Columns>
          <asp:BoundColumn DataField="ProductID" ReadOnly="True"
                        Visible="False" />
          <asp:BoundColumn DataField="ProductName" ReadOnly="True"
                        HeaderText="Name" />
          <asp:BoundColumn DataField="UnitPrice" HeaderText="Price" />
          <asp:EditCommandColumn ButtonType="LinkButton"
                        UpdateText="Save" CancelText="Cancel"
                        EditText="Edit" />
          <asp:ButtonColumn Text="Delete" CommandName="Delete" />
        </Columns>
      </asp:DataGrid>
    </form>
  </body>
</html>

<script language="c#" runat="server">
private String strConnection = ConfigurationSettings.AppSettings["NWind"];
private String strSQLSelect = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID = 1";
private String ProductTableName = "ProductTable";
private SqlConnection objConnection;

private void Page_Load(object sender, System.EventArgs e)
{
  if (!IsPostBack)
  {
    String strSQL = "INSERT INTO Products (" + "ProductName, CategoryID, UnitPrice) " + "VALUES ('RolaBolaCola', 1, 15)";
    Connect();
    SqlCommand dbComm = new SqlCommand(strSQL, objConnection);
    dbComm.ExecuteNonQuery();
    Disconnect();
    LoadGrid();
  }
}

private void LoadGrid()
{
  Connect();
  SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
  DataSet ds = new DataSet();
  adapter.Fill(ds, ProductTableName);
  Disconnect();

  dgProducts.DataSource = ds.Tables[ProductTableName];
  dgProducts.DataBind();
}

private void Connect()
{
  if (objConnection == null)
    objConnection = new SqlConnection(strConnection);

  if (objConnection.State == ConnectionState.Closed)
    objConnection.Open();
}

private void Disconnect()
{
  objConnection.Close();
}

public void EditRecord(object sender, DataGridCommandEventArgs e)
{
  dgProducts.EditItemIndex = e.Item.ItemIndex;
  LoadGrid();
}

public void CancelEdit(object sender, DataGridCommandEventArgs e)
{
  dgProducts.EditItemIndex = -1;
  LoadGrid();
}

public void UpdateRecord(object sender, DataGridCommandEventArgs e)
{
  // Retrieve the field values in the edited row
  // make sure id is a int for security [int ProductID = Convert.ToInt32]
  int ProductID = Convert.ToInt32(e.Item.Cells[0].Text);
  TextBox PriceTextBox = (TextBox)e.Item.Cells[2].Controls[0];
  decimal Price = Convert.ToDecimal(PriceTextBox.Text);

  dgProducts.EditItemIndex = -1;
  UpdateProduct(ProductID, Price);

  DataSet ds = new DataSet();
  dgProducts.DataSource = ds.Tables[ProductTableName];
  dgProducts.DataBind();
}

private void UpdateProduct(int ProductID, decimal Price)
{
  // Create and load a DataSet with records from Northwind.Products table
  Connect();
  SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
  DataSet ds = new DataSet();
  adapter.Fill(ds, ProductTableName);
  Disconnect();

  // Modify the in-memory records in the DataSet
  DataTable tbl = ds.Tables[ProductTableName];
  tbl.PrimaryKey = new DataColumn[] {tbl.Columns["ProductID"]};
  DataRow row = tbl.Rows.Find(ProductID);
  row["UnitPrice"] = Price;

  // Reconnect the DataSet and update the database
  SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
  Connect();
  adapter.Update(ds, ProductTableName);
  Disconnect();
}

public void DeleteRecord(object sender, DataGridCommandEventArgs e)
{
  // Retrieve the ID of the product to be deleted
  int ProductID = Convert.ToInt32(e.Item.Cells[0].Text);

  dgProducts.EditItemIndex = -1;
  DeleteProduct(ProductID);

  // Display the remaining items in the DataGrid
  DataSet ds = new DataSet();
  dgProducts.DataSource = ds.Tables[ProductTableName];
  dgProducts.DataBind();
}

private void DeleteProduct(int ProductID)
{
  // Create and load a DataSet with records from Northwind's Products table
  Connect();
  SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, objConnection);
  DataSet ds = new DataSet();
  adapter.Fill(ds, ProductTableName);
  Disconnect();

  // Mark the product as Deleted in the DataSet
  DataTable tbl = ds.Tables[ProductTableName];
  tbl.PrimaryKey = new DataColumn[] {tbl.Columns["ProductID"]};
  DataRow row = tbl.Rows.Find(ProductID);
  row.Delete();

  // Reconnect the DataSet and delete the record from the database
  SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
  Connect();
  adapter.Update(ds, ProductTableName);
  Disconnect();
}
</script>
 
Old September 1st, 2005, 10:35 AM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

come on authors and experts... I can't be the only person this has happened to.

I know the problem is linked to either cascading deletes/ and, or permissions on sql server to do with FK's

cheers








Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception occurred. basie Classic ASP Basics 3 September 4th, 2008 03:02 AM
Ch 5 - Errors in all *Rss.aspx pages ewelling BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 September 6th, 2006 09:16 AM
Ch 13 Try it out file synchronize.aspx mahir BOOK: Beginning ASP.NET 1.0 9 February 23rd, 2005 05:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.