Click here to Skip to main content
13,248,562 members (85,786 online)
Rate this:
Please Sign up or sign in to 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:

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
    cmd.Connection = con
    If Literature.SelectedValue = "" Then
        e.Cancel = True
        label1.Visible = True
        label1.Text = "You must select a Literature."
        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."
                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") & "')"
                    End If
                End If
                End If
            End If
        End If
End Sub
Posted 14-Oct-12 5:18am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171114.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100