I am getting the following error: Object variable or With block variable not set. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated
in the code. Exception Details: System.NullReferenceException: Object variable or With block variable not set. Source Error: Line 250: cb = CType(dgE.FindControl("major"), CheckBox) Line 251: If cb.Checked = True Then Line 252: cmdAdd = New SqlCommand("INSERT
UNIVPROG (UNIVID,MAJORID) " _ Line 253: & "VALUES ('" & drpUniv.SelectedItem.Value & "', '" & dgE.DataItem("id") & "')", conAdd) Line 254: conAdd.Open() Source File: C:\UPSR\Admin\Test.aspx.vb Line: 252 Stack Trace: [NullReferenceException: Object variable
or With block variable not set.] Microsoft.VisualBasic.CompilerServices.LateBinding.LateIndexGet(Object o, Object[] args, String[] paramnames) +1242 UPSR.Test.btnAddMajors_Click(Object sender, EventArgs e) in C:\UPSR\Admin\Test.aspx.vb:252 System.Web.UI.WebControls.Button.OnClick(EventArgs
e) +108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
postData) +33 System.Web.UI.Page.ProcessRequestMain() +1277 here is the code block that is giving the error:
Private Sub btnAddMajors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMajors.Click
Dim dgE As DataGridItem
Dim conAdd As SqlConnection
Dim cmdAdd As SqlCommand
conAdd = New SqlConnection(connectionString)
For Each dgE In dgMajors.Items
Dim cb As CheckBox
cb = CType(dgE.FindControl("major"), CheckBox)
If cb.Checked = True Then
cmdAdd = New SqlCommand("INSERT UNIVPROG (UNIVID,MAJORID) " _
& "VALUES ('" & drpUniv.SelectedItem.Value & "', '" & dgE.DataItem("id") & "')", conAdd)
conAdd.Open()
cmdAdd.ExecuteNonQuery()
conAdd.Close()
End If
Next
End Sub
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
mreyeros
Contributor
4022 Points
736 Posts
Object Variable Error
Aug 06, 2003 04:34 PM|LINK
Private Sub btnAddMajors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMajors.Click Dim dgE As DataGridItem Dim conAdd As SqlConnection Dim cmdAdd As SqlCommand conAdd = New SqlConnection(connectionString) For Each dgE In dgMajors.Items Dim cb As CheckBox cb = CType(dgE.FindControl("major"), CheckBox) If cb.Checked = True Then cmdAdd = New SqlCommand("INSERT UNIVPROG (UNIVID,MAJORID) " _ & "VALUES ('" & drpUniv.SelectedItem.Value & "', '" & dgE.DataItem("id") & "')", conAdd) conAdd.Open() cmdAdd.ExecuteNonQuery() conAdd.Close() End If Next End Sub