Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1255
  • Last Modified:

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>
0
gogetsome
Asked:
gogetsome
1 Solution
 
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now