Click here to Skip to main content
15,885,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to get an ObjectDataSource working with a GridView.

So far I have got the Update command working fine, but the Delete command will not pass the correct OrderID, which is the Primary Key for the Order Table.

Here is the code for the update and delete in the HTML objectDataSource

My Data Source is an AccessDataBase so I am using OLEDB

C#
<asp:ObjectDataSource ID="odsOrderDetail" runat="server" TypeName="TrackerDotNet.App_Code.OrderDetailDAL" 
    EnablePaging="False" SelectMethod="LoadOrderDetailData"
    UpdateMethod="UpdateOrderDetails" 
    StartRowIndexParameterName="StartRowIndex" 
    MaximumRowsParameterName="MaximumRows" 
    OldValuesParameterFormatString="original_{0}" 
    DeleteMethod="DeleteOrderDetails" >
    <DeleteParameters>
      <asp:Parameter Name="OrderID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
      <asp:Parameter Name="OrderID" Type="Int32" />
      <asp:Parameter Name="ItemTypeID" Type="Int32" />
      <asp:Parameter Name="QuantityOrdered" Type="Double" />
      <asp:Parameter Name="PackagingID" Type="Int32" />
    </UpdateParameters>


....

The Code in the custom class for the Update works fine:

C#
public bool UpdateOrderDetails(Int32 OrderID, Int32 ItemTypeID, double QuantityOrdered, Int32 PackagingID)
{
  string _sqlCmd = "UPDATE OrdersTbl SET ItemTypeID = ?, QuantityOrdered = ?, PackagingID = ? WHERE (OrderId = ?)";
  OleDbConnection _conn = new OleDbConnection(_connectionString);

   // add parameters in the order they appear in the update command
  OleDbCommand _cmd = new OleDbCommand(_sqlCmd, _conn);
  _cmd.Parameters.Add(new OleDbParameter { Value = ItemTypeID });
  _cmd.Parameters.Add(new OleDbParameter { Value = QuantityOrdered });
  _cmd.Parameters.Add(new OleDbParameter { Value = PackagingID });
  _cmd.Parameters.Add(new OleDbParameter { Value = OrderID });

  try
  {
    _conn.Open();
    if (_cmd.ExecuteNonQuery() == 0)
      return false;
  }
  catch (OleDbException e)
  {
    return false;
  }
  finally
  {
    _conn.Close();
  }

  return true;
}


The Delete command is:

C#
public bool DeleteOrderDetails(Int32 OrderID)
{
  string _sqlCmd = "DELETE FROM OrdersTbl WHERE (OrderId = ?)";
  OleDbConnection _conn = new OleDbConnection(_connectionString);

  // add parameters in the order they appear in the update command
  OleDbCommand _cmd = new OleDbCommand(_sqlCmd, _conn);
  _cmd.Parameters.Add(new OleDbParameter { Value = OrderID });

  try
  {
    _conn.Open();
    if (_cmd.ExecuteNonQuery() == 0)
      return false;
  }
  catch (OleDbException e)
  {
    return false;
  }
  finally
  {
    _conn.Close();
  }

  return true;
}


When I run the debugger in the Update all the variables have valid values, and it works fine. But the Delete Command the OrderID is 0

I have tried re-binding the command is the editor and also tried <asp:ControlParameter with the OrderIDLabel that is in the template field.

Any ideas on why this is happening?
Posted

I still have no solution to this, anyone?
 
Share this answer
 
Hey please check this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.deleteparameters.aspx[^]
I think the orderid parameter isn't getting set correctly, hope this helps
 
Share this answer
 
v2
Comments
Warren Machanik 26-Mar-13 14:45pm    
Sorry in the delay in getting back, had not checked the answer as I have implemented a template field with a hyperlink that jumps to another page (and this works btw)

<asp:TemplateField>
<itemtemplate>
<asp:HyperLink ID="hlDelete" runat="server" Text="del"
NavigateUrl='<%# String.Format("~/Pages/DeleteOrderLine.aspx?OrderId={0}", Eval("OrderId")) %>'>




So after reading the above added an OnDeleteing routine, not quite sure why but had to be a in page <script> rather than a behind one.

If I get the eInputParameters there is a count of 1, I cannot see what the key is if I try access it as [" OrderID" or [1] or [0] it is null, not sure why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900