Object Variable Errorhttp://forums.asp.net/t/301603.aspx/1?Object+Variable+ErrorWed, 06 Aug 2003 20:06:32 -0400301603301603http://forums.asp.net/p/301603/301603.aspx/1?Object+Variable+ErrorObject Variable Error 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(&quot;major&quot;), CheckBox) Line 251: If cb.Checked = True Then Line 252: cmdAdd = New SqlCommand(&quot;INSERT UNIVPROG (UNIVID,MAJORID) &quot; _ Line 253: &amp; &quot;VALUES ('&quot; &amp; drpUniv.SelectedItem.Value &amp; &quot;', '&quot; &amp; dgE.DataItem(&quot;id&quot;) &amp; &quot;')&quot;, 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) &#43;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) &#43;108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) &#43;57 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) &#43;18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) &#43;33 System.Web.UI.Page.ProcessRequestMain() &#43;1277 here is the code block that is giving the error: <pre class="prettyprint">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(&quot;major&quot;), CheckBox) If cb.Checked = True Then cmdAdd = New SqlCommand(&quot;INSERT UNIVPROG (UNIVID,MAJORID) &quot; _ &amp; &quot;VALUES ('&quot; &amp; drpUniv.SelectedItem.Value &amp; &quot;', '&quot; &amp; dgE.DataItem(&quot;id&quot;) &amp; &quot;')&quot;, conAdd) conAdd.Open() cmdAdd.ExecuteNonQuery() conAdd.Close() End If Next End Sub</pre> 2003-08-06T16:34:08-04:00301686http://forums.asp.net/p/301603/301686.aspx/1?Re+Object+Variable+ErrorRe: Object Variable Error Could it be that drpUniv.SelectedItem is Nothing? This would be the case when there is no item selected in the dropdown. 2003-08-06T17:34:28-04:00301734http://forums.asp.net/p/301603/301734.aspx/1?Re+Object+Variable+ErrorRe: Object Variable Error 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? 2003-08-06T18:04:01-04:00301737http://forums.asp.net/p/301603/301737.aspx/1?Re+Object+Variable+ErrorRe: Object Variable Error 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(&quot;major&quot;), CheckBox) Line 251: If cb.Checked = True Then Line 252: cmdAddMajors = New SqlCommand(&quot;INSERT UNIVPROG (UNIVID,MAJORID) &quot; _ Line 253: &amp; &quot;VALUES ('&quot; &amp; drpUniv.SelectedItem.Value &amp; &quot;', '&quot; &amp; dgE.DataItem(&quot;id&quot;) &amp; &quot;')&quot;, 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) &#43;108 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) &#43;57 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) &#43;18 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) &#43;33 System.Web.UI.Page.ProcessRequestMain() &#43;1277 here is my code block <pre class="prettyprint">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(&quot;major&quot;), CheckBox) If cb.Checked = True Then cmdAddMajors = New SqlCommand(&quot;INSERT UNIVPROG (UNIVID,MAJORID) &quot; _ &amp; &quot;VALUES ('&quot; &amp; drpUniv.SelectedItem.Value &amp; &quot;', '&quot; &amp; dgE.DataItem(&quot;id&quot;) &amp; &quot;')&quot;, conAddmajors) conAddmajors.Open() cmdAddMajors.ExecuteNonQuery() conAddmajors.Close() End If Next End Sub</pre> 2003-08-06T18:06:34-04:00301771http://forums.asp.net/p/301603/301771.aspx/1?Re+Object+Variable+ErrorRe: Object Variable Error It seems like now the call to FindControl is failing. It simply isn't finding the textbox named &quot;major&quot; so &quot;cb&quot; is a null reference (Nothing), so when you do cb.Checked you get a null reference exception. 2003-08-06T18:22:19-04:00301862http://forums.asp.net/p/301603/301862.aspx/1?Re+Object+Variable+ErrorRe: Object Variable Error 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. 2003-08-06T19:22:20-04:00301922http://forums.asp.net/p/301603/301922.aspx/1?Re+Object+Variable+ErrorRe: Object Variable Error Perhaps you need to call FindControl on the specific DataGridItem that has that checkbox? DataGridItem item = MyDataGrid.Items[selectedIndex]; CheckBox cb = (CheckBox)item.FindControl(&quot;major&quot;); ... 2003-08-06T20:06:32-04:00