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
<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:
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);
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:
public bool DeleteOrderDetails(Int32 OrderID)
{
string _sqlCmd = "DELETE FROM OrdersTbl WHERE (OrderId = ?)";
OleDbConnection _conn = new OleDbConnection(_connectionString);
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?