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.
I don't think that it has to do with the dropdown because there is an item selected in the dropdown. What else could it possibly be?
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.
ok now I am getting this error: Object reference not set to an instance of an object. 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 reference not set to an instance of an object. Source Error: Line 249: Dim cb As CheckBox Line 250: cb = CType(dgMajors.FindControl("major"), CheckBox) Line 251: If cb.Checked
= True Then Line 252: cmdAddMajors = New SqlCommand("INSERT UNIVPROG (UNIVID,MAJORID) " _ Line 253: & "VALUES ('" & drpUniv.SelectedItem.Value & "', '" & dgE.DataItem("id") & "')", conAddmajors) Source File: C:\UPSR\Admin\Test.aspx.vb Line: 251 Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] UPSR.Test.btnAddMajors_Click(Object sender, EventArgs e) in C:\UPSR\Admin\Test.aspx.vb:251 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 my code block
Private Sub btnAddMajors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMajors.Click
Dim dgE As DataGridItem
Dim conAddmajors As SqlConnection
Dim cmdAddMajors As SqlCommand
conAddmajors = New SqlConnection(connectionString)
For Each dgE In dgMajors.Items
Dim cb As CheckBox
cb = CType(dgMajors.FindControl("major"), CheckBox)
If cb.Checked = True Then
cmdAddMajors = New SqlCommand("INSERT UNIVPROG (UNIVID,MAJORID) " _
& "VALUES ('" & drpUniv.SelectedItem.Value & "', '" & dgE.DataItem("id") & "')", conAddmajors)
conAddmajors.Open()
cmdAddMajors.ExecuteNonQuery()
conAddmajors.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.
It seems like now the call to FindControl is failing. It simply isn't finding the textbox named "major" so "cb" is a null reference (Nothing), so when you do cb.Checked you get a null reference exception.
I am not sure why it cannot find the checkbox it is created automatically in the datagrid. and that is the id of the checkbox.
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.
Perhaps you need to call FindControl on the specific DataGridItem that has that checkbox? DataGridItem item = MyDataGrid.Items[selectedIndex]; CheckBox cb = (CheckBox)item.FindControl("major"); ...
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 SubEilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Object Variable Error
Aug 06, 2003 05:34 PM|LINK
mreyeros
Contributor
4022 Points
736 Posts
Re: Object Variable Error
Aug 06, 2003 06:04 PM|LINK
mreyeros
Contributor
4022 Points
736 Posts
Re: Object Variable Error
Aug 06, 2003 06:06 PM|LINK
Private Sub btnAddMajors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddMajors.Click Dim dgE As DataGridItem Dim conAddmajors As SqlConnection Dim cmdAddMajors As SqlCommand conAddmajors = New SqlConnection(connectionString) For Each dgE In dgMajors.Items Dim cb As CheckBox cb = CType(dgMajors.FindControl("major"), CheckBox) If cb.Checked = True Then cmdAddMajors = New SqlCommand("INSERT UNIVPROG (UNIVID,MAJORID) " _ & "VALUES ('" & drpUniv.SelectedItem.Value & "', '" & dgE.DataItem("id") & "')", conAddmajors) conAddmajors.Open() cmdAddMajors.ExecuteNonQuery() conAddmajors.Close() End If Next End SubEilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Object Variable Error
Aug 06, 2003 06:22 PM|LINK
mreyeros
Contributor
4022 Points
736 Posts
Re: Object Variable Error
Aug 06, 2003 07:22 PM|LINK
Eilon
Contributor
5753 Points
976 Posts
Microsoft
Re: Object Variable Error
Aug 06, 2003 08:06 PM|LINK