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
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
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
End If
End If
End If
End If
End Sub