Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to insert data into a table using a formview and listview. i have a formview with just a dropdownlist and an insert button. i have a listview that displays 50 products at a time each with a checkbox next to it.

what i want to do is select an item from the formview dropdownlist, and check one, or multiple products from the listview and insert the values into a table.

Table example after making your selections and inserting:

formview DDL1 value2, listview value1
formview DDL1 value2, listview value25
formview DDL1 value2, listview value38


I get the error: object reference not set to instance of an object. I know what the error is but i dont know how to fix my code. It's blowing up at the insert section of my code.

can anyone help?
Here is my formview inserting code:

VB
Protected Sub LitFormView_ItemInserting(sender As Object, e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles LitFormView.ItemInserting
    Dim cmd As New SqlCommand
    Dim con As New SqlConnection
    Dim label1 As Label = CType(LitFormView.FindControl("Label1"), Label)
    Dim Literature As DropDownList = CType(LitFormView.FindControl("DropDownList1"), DropDownList)
    Dim prodcheckbox As CheckBox = CType(ProdListView.FindControl("CheckBox1"), CheckBox)
    Dim catcheckbox As CheckBox = CType(CatListView.FindControl("CheckBox1"), CheckBox)
    Dim subcheckbox As CheckBox = CType(SubListView.FindControl("CheckBox1"), CheckBox)
    Dim PRDProdIDLabel As Label = CType(ProdListView.FindControl("PRDProdIDLabel"), Label)
    Dim PRDProdNameLabel As Label = CType(ProdListView.FindControl("PRDProdNameLabel"), Label)

    con.ConnectionString = SqlDataSource1.ConnectionString
    con.Open()
    cmd.Connection = con

    If Literature.SelectedValue = "" Then
        e.Cancel = True
        label1.Visible = True
        label1.Text = "You must select a Literature."
    Else
        label1.Visible = False
        If e.Cancel = False Then
            If prodcheckbox Is Nothing And catcheckbox Is Nothing And subcheckbox Is Nothing Then
                e.Cancel = True
                label1.Visible = True
                label1.Text = "You must Check at least 1 Product, or a Category, or a Sub-Category."
            Else
                label1.Visible = False
                If e.Cancel = False Then

'THIS IS WHERE I AM GETTING THE ERROR - I don't know how to get the listview row id or which row is being checked in the checkboxes.
                    'For Each ListItem In ProdListView.Items

                    If prodcheckbox.Checked Then
                        cmd.CommandText = "INSERT INTO LITProdData(LITProdLITID, LITProdName, LITProdPRDProdID, LITProdPRDProdName, LITUser) values('" & Trim(Literature.SelectedValue) & "', '" & Trim(Literature.SelectedItem.Text) & "', '" & Trim(PRDProdIDLabel.Text) & "', '" & Trim(PRDProdNameLabel.Text.Replace("'", "")) & "', '" & Session("UserInfo") & "')"
                        cmd.ExecuteNonQuery()
                    End If
                    'Next
                End If
                End If
            End If
        End If

End Sub
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900