I have a gridview with 3 dropdown list. now I encounter problems during the run and edit my gridview
and this is the error message:
![http://uupload.ir/files/14pq_capture.png](https://i.imgur.com/5bIjubF.png)
this is my gridview code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate" />
<asp:BoundField DataField="BirthPlace" HeaderText="BirthPlace" SortExpression="BirthPlace" />
<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="0">زن</asp:ListItem>
<asp:ListItem Value="1">مرد</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ReligionId" SortExpression="ReligionId">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="Religion" DataTextField="Title" DataValueField="Title">
</asp:DropDownList>
<asp:SqlDataSource ID="Religion" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [Title] FROM [IMS_Religion]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ReligionId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MaritalId" SortExpression="MaritalId">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Marital" DataTextField="Status" DataValueField="Status">
</asp:DropDownList>
<asp:SqlDataSource ID="Marital" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [Status] FROM [IMS_Marital]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("MaritalId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [IMS_Person] WHERE [Id] = @Id"
InsertCommand="INSERT INTO [IMS_Person] ([GuidId], [FirstName], [LastName], [BirthDate], [BirthPlace], [Gender], [ReligionId], [MaritalId])
VALUES (@GuidId, @FirstName, @LastName, @BirthDate, @BirthPlace, @Gender, @ReligionId, @MaritalId)"
SelectCommand="SELECT * FROM [IMS_Person]"
UpdateCommand="UPDATE [IMS_Person] SET [FirstName] = @FirstName, [LastName] = @LastName, [BirthDate] = @BirthDate,
[BirthPlace] = @BirthPlace, [Gender] = @Gender, [ReligionId] = @ReligionId, [MaritalId] = @MaritalId WHERE [Id] = @Id"
OnUpdating="SqlDataSource1_Updating"
>
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="BirthDate" Type="DateTime" />
<asp:Parameter Name="BirthPlace" Type="String" />
<asp:Parameter Name="Gender" Type="Byte" />
<asp:Parameter Name="ReligionId" Type="Byte" />
<asp:Parameter Name="MaritalId" Type="Byte" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="BirthDate" Type="DateTime" />
<asp:Parameter Name="BirthPlace" Type="String" />
<asp:Parameter Name="Gender" Type="Byte" />
<asp:Parameter Name="ReligionId" Type="Byte" />
<asp:Parameter Name="MaritalId" Type="Byte" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
//int Gender = Gender(GridView1.DropDownList3.Text);
DropDownList G1 = (DropDownList)this.GridView1.FindControl("DropDownList3");
DropDownList R1 = (DropDownList)this.GridView1.FindControl("DropDownList2");
DropDownList M1 = (DropDownList)this.GridView1.FindControl("DropDownList1");
int gender1 = Gender(G1.SelectedValue);
int religion1 = Religion(R1.SelectedValue);
int marital1 = Marital(M1.SelectedValue);
try
{
e.Command.Parameters["@Gender"].Value = gender1;
e.Command.Parameters["@ReligionId"].Value = religion1;
e.Command.Parameters["@MaritalID"].Value = marital1;
}
catch
{
}
}
how can I do edit in my grid correctly?