skip to Main Content

I am encountering an error while attempting to update a row in a GridView control using DropDownList and TextBox controls. The GridView contains a TemplateField with a DropDownList control for the "Status" column and an EditItemTemplate with TextBox controls for other columns. The DropDownList is populated with status options, and when I try to update a row, I receive the following error message:

"System.Data.SqlClient.SqlException: Cannot insert the value NULL into column ‘ID statusa’, table ‘Karlo_Miskovic.dbo.Narudžba’; column does not allow nulls. UPDATE fails."

I have reviewed my code and made sure that the DropDownList control has a selected value, and the TextBox controls have the appropriate values. However, the error still persists. I would appreciate any insights or suggestions on resolving this issue.

Here is the relevant code snippet:
.asxp:

<asp:TemplateField HeaderText="Status" SortExpression="Status">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlStatus" runat="server" SelectedValue='<%# Bind("[Status]") %>'>
            <asp:ListItem Text="Neobrađeno" Value="Neobrađeno"></asp:ListItem>
            <asp:ListItem Text="U obradi" Value="U obradi"></asp:ListItem>
            <asp:ListItem Text="Završeno" Value="Završeno"></asp:ListItem>
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblStatus" runat="server" Text='<%# Bind("[Status]") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

.vb:

Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
    ' Retrieve the new values entered by the user
    Dim orderID As Integer = Integer.Parse(GridView1.DataKeys(e.RowIndex).Value.ToString())
    Dim deliveryDate As Date = Date.Parse(CType(GridView1.Rows(e.RowIndex).FindControl("txtDatumIsporuke"), TextBox).Text)
    Dim status As String = CType(GridView1.Rows(e.RowIndex).FindControl("ddlStatus"), DropDownList).SelectedValue
    ' Other variable assignments...

    ' Rest of the code...

End Sub

I have also ensured that the database column ‘ID statusa’ is not set to allow null values. Despite these measures, the error persists. Any assistance in resolving this issue would be greatly appreciated.

3

Answers


  1. Chosen as BEST ANSWER

    I did this and it worked. Instead of:

    Dim status As String = CType(GridView1.Rows(e.RowIndex).FindControl("ddlStatus"), DropDownList).SelectedValue
    

    I did this:

    Dim ddlStatus As DropDownList = CType(GridView1.Rows(e.RowIndex).FindControl("ddlStatus"), DropDownList)
    ddlStatus.SelectedValue
    

  2. add this code and try again

                if (!IsPostBack)
                {
                   //bind your gridview
                    BindGridView1();
                }
    

    in vb

    If Not IsPostBack Then
        'bind your gridview
        BindGridView1()
    End If
    
    Login or Signup to reply.
  3. Depending on your project settings, you could also use

    Dim ddlStatus As DropDownList = 
       GridView1.Rows(e.RowIndex).FindControl("ddlStatus")
    
    ddlStatus.SelectedItem.Value -- gets value
    ddlStatus.SelectedItem.Text -- gets text 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search