Gridview with dropdown

Hello, I have a gridview with a template column which contains a dropdown in the editItemTemplate. I added the appropriate items to the collection(the dropdown is not databound). When editing in the gridview, I select one of the dropdown's values and click update. I get the error message below. My code is below the error message.

Why is the selected value of the dropdown not being sent in the update?

Here is the error message:
Cannot insert the value NULL into column 'CUSTCLAS', table 'TEST.dbo.RM00101'; column does not allow nulls. UPDATE fails.
The statement has been terminated.

Here is my code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CUSTNMBR"
                    DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" CellPadding="4" Font-Names="Verdana" Font-Size="X-Small" ForeColor="#333333" GridLines="None" PageSize="500" Width="100%">
                    <Columns>
                        <asp:CommandField ShowEditButton="True" />
                        <asp:BoundField DataField="CUSTNMBR" HeaderText="CUSTNMBR" ReadOnly="True" SortExpression="CUSTNMBR" />
                        <asp:BoundField DataField="CUSTNAME" HeaderText="CUSTNAME" SortExpression="CUSTNAME" ReadOnly="True" />
                        <asp:TemplateField HeaderText="CUSTCLAS" SortExpression="CUSTCLAS">
                            <EditItemTemplate>
                                <asp:DropDownList ID="DropDownList1" runat="server">
                                    <asp:ListItem Selected="True">COMMERCIAL</asp:ListItem>
                                    <asp:ListItem>GOV - CITY</asp:ListItem>
                                    <asp:ListItem>GOV - COUNTY</asp:ListItem>
                                    <asp:ListItem>GOV - FEDERAL</asp:ListItem>
                                    <asp:ListItem>GOV - STATE</asp:ListItem>
                                    <asp:ListItem>NATIONAL ACCTS</asp:ListItem>
                                </asp:DropDownList>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("CUSTCLAS") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast"
                        NextPageText="Next" PreviousPageText="Previous" PageButtonCount="25" Position="TopAndBottom" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#E3EAEB" />
                    <EditRowStyle BackColor="#F3BB34" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <PagerStyle BackColor="#578B4D" ForeColor="White" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#578B4D" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TESTConnectionString %>"
                    SelectCommand="SELECT [CUSTNMBR], [CUSTNAME], [CUSTCLAS] FROM [RM00101] ORDER BY [CUSTCLAS], [CUSTNAME]"
                    UpdateCommand="UPDATE [RM00101] SET [CUSTCLAS] = @CUSTCLAS WHERE [CUSTNMBR] = @CUSTNMBR">
                    <UpdateParameters>
                        <asp:Parameter Name="CUSTCLAS" Type="String" />
                        <asp:Parameter Name="CUSTNMBR" Type="String" />
                    </UpdateParameters>
                </asp:SqlDataSource>
gogetsomeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:
Hi gogetsome,

The SqlDataSource has no connection to that DropDownList in the EditItemTemplate, you have to fill in the parameters for the SqlDataSource's UpdateCommand.  So in the RowUpdating event, you'll have some code like:

Dim MyValue As String = CType(GridView1.Rows(e.RowIndex).FindControl("DropDownList1"), DropDownList).SelectedValue
SqlDataSource1.UpdateParameters("CustClas").DefaultValue = MyValue
' do something about your other parameter, CustNmbr
SqlDataSource1.Update()

-- Jason
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.