Gridview with dropdown

Posted on 2006-03-23
Last Modified: 2007-10-18
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%">
                        <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">
                                <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:Label ID="Label1" runat="server" Text='<%# Bind("CUSTCLAS") %>'></asp:Label>
                    <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: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">
                        <asp:Parameter Name="CUSTCLAS" Type="String" />
                        <asp:Parameter Name="CUSTNMBR" Type="String" />
Question by:gogetsome
    1 Comment
    LVL 22

    Accepted Solution

    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

    -- Jason

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now