ObjectDataSource Gridview Insert Fails W / Empty Values ​​Dictionary

I have a gridview to which I created the Insert template in the footer row.

I have an ObjectDataSource object associated with a business object.

I have an OnInserting event handler that never fires.

The program detects an error as soon as I call. Insert an ObjectDataSource object. The error I am getting is that there are no values ​​and that I have to check to make sure that the dictionary of values ​​is not empty.

I do not see a way to insert a dictionary as a parameter. I saw a mention of capturing an ObjectDataSourceView and using the Insert method, but I don't see a mention of how to do this, and MSDN claims that you do not have access.

Is thinking the way here? Is there a better way to have an insert row in a gridview? Did I miss something obvious in my steps here?

Below is the code:
ObjectDataSource:

<asp:ObjectDataSource ID="LeasesDS" runat="server" OnInserting="LeasesDS_Inserting" DataObjectTypeName="CLS.BusObjects.LeaseObj" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetLeasesByCustomerID" TypeName="CLS.BusObjects.LeaseObj" UpdateMethod="Update"> <SelectParameters> <asp:Parameter Name="customerID" Type="Int32" /> </SelectParameters> <InsertParameters> <asp:Parameter Name="CustomerID" Type="Int32" /> <asp:Parameter Name="PurchaseDate" Type="DateTime" /> <asp:Parameter Name="AutoYear" Type="Int32" /> <asp:Parameter Name="Make" Type="String" /> <asp:Parameter Name="Model" Type="String" /> <asp:Parameter Name="LeaseEndDate" Type="DateTime" /> </InsertParameters> </asp:ObjectDataSource> 


CodeBehind Methods:

 protected void LeasesGrid_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Insert" && Page.IsValid) { LeasesDS.Insert(); } } protected void LeasesDS_Inserting(object sender, ObjectDataSourceMethodEventArgs e) { DropDownList GridCustomersList = (DropDownList)LeasesGrid.FooterRow.FindControl("GridCustomersList"); TextBox PurchaseDate = (TextBox)LeasesGrid.FooterRow.FindControl("PurchaseDate"); TextBox AutoYear = (TextBox)LeasesGrid.FooterRow.FindControl("AutoYear"); TextBox Make = (TextBox)LeasesGrid.FooterRow.FindControl("Make"); TextBox Model = (TextBox)LeasesGrid.FooterRow.FindControl("Model"); TextBox LeaseEndDate = (TextBox)LeasesGrid.FooterRow.FindControl("LeaseEndDate"); e.InputParameters["CustomerID"] = Convert.ToInt32(GridCustomersList.SelectedValue); DateTime? purchaseDate = null; if (!string.IsNullOrEmpty(PurchaseDate.Text)) purchaseDate = Convert.ToDateTime(PurchaseDate.Text); e.InputParameters["PurchaseDate"] = purchaseDate; int? autoYear = null; if (!string.IsNullOrEmpty(AutoYear.Text)) autoYear = Convert.ToInt32(AutoYear.Text); e.InputParameters["AutoYear"] = autoYear; string make = null; if (!string.IsNullOrEmpty(Make.Text)) make = Make.Text; e.InputParameters["Make"] = make; string model = null; if (!string.IsNullOrEmpty(Model.Text)) model = Model.Text; e.InputParameters["Model"] = model; DateTime? leaseEndDate = null; if (!string.IsNullOrEmpty(LeaseEndDate.Text)) leaseEndDate = Convert.ToDateTime(LeaseEndDate.Text); e.InputParameters["LeaseEndDate"] = leaseEndDate; } 
+4
source share
1 answer

Your example is incompatible because you have DataObjectTypeName set, but your LeasesDS_Inserting method adds parameters as if it weren’t.

If you do not need the name DataObjectTypeName, you can delete it, and then you will have a better chance that this will work.

If you need a DataObjectTypeName or just prefer it (like me), there is a way to make this work, but it is not very.

In your LeasesGrid_RowCommand method, you can use code like this to get an idea:

 IDataSource ds = (IDataSource)LeasesDS; DataSourceView view = ds.GetView(LeasesGrid.DataMember); 

After receiving the view, you can create a dictionary containing the values ​​that must be written to the data object, which will ultimately be passed to your Insert method. The code for this will be very similar to what you have in LeasesDS_Inserting, but you will embed in your own dictionary instead of e.InputParameters.

Once your dictionary is ready, you can call Insert in a view with something like this:

 view.Insert(dict, delegate { return false; }); 

That should do it.

You no longer need the LeasesDS_Inserting method, because the Insert Insert method will do the job of converting the dictionary that you passed into the data object that it will pass into your Insert method.

If you need to process the properties of this data object more, you will see it in the dictionary of input parameters passed to LeasesDS_Inserting.

+5
source

All Articles