A inserção de Gridview do ObjectDataSource falha com o dicionário de valores vazios
Eu tenho um gridview para o qual eu criei um modelo de inserção na linha de rodapé.
Eu tenho um ObjectDataSource que está vinculado a um objeto de negócios.
Eu tenho um manipulador de eventos OnInserting que nunca é acionado.
O programa encontra um erro quando eu chamo .Insert no ObjectDataSource. O erro que recebo é que não há valores e que devo verificar para garantir que o dicionário de valores não esteja vazio.
Não vejo uma maneira de inserir um dicionário como parâmetro. Eu já vi menção de pegar o ObjectDataSourceView e usar o método Insert, mas não vejo nenhuma menção de como fazer isso e o MSDN afirma que você não tem acesso.
A reflexão é o caminho a seguir aqui? Existe uma maneira melhor de ter uma linha de inserção em um gridview? Estou perdendo algo óbvio em meus passos aqui?
Abaixo está o código:
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>
Métodos CodeBehind:
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;
}