Some people -- perhaps gullibly -- believe that there are multiple ways of being a catskinner.
When my attempt to search all the Controls on a Page for Checkboxes continued to fail ignominiously (for proof of that, see this), I thought maybe I could just look at the ID of the Control, rather than care what type of control it was at heart.
So, I commented out this line:
If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then
...and tried this instead:
If cntrl.ID.ToString().Contains("ckbx")
But I fared no better, and received this coldwater slap:
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------
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 69: LabelDebug.Text = LabelDebug.Text+" "+cntrl.GetType().ToString+" "
Line 70: 'If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then
Line 71: If cntrl.ID.ToString().Contains("ckbx")
Line 72: 'Dim objAsConvertible As IConvertible = TryCast(cntrl, IConvertible)
Line 73: 'If objAsConvertible Is Nothing Then
Source File: C:EnhancedMonthlySalesReportingcustomerreportingnetcustomerreportingnetpagescustmaint_categoryadmin.aspx.vb Line: 71
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
pages_custmaint_categoryadmin.Button1_Click(Object sender, EventArgs e) in C:EnhancedMonthlySalesReportingcustomerreportingnetcustomerreportingnetpagescustmaint_categoryadmin.aspx.vb:71
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +139
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491
Why is it that the control, which obviously exists, throws an exception as if there is no reference to it?
The entire block of code is:
Dim coName As String
Dim argVals(2) As String
Dim _Unit As String
Dim _MemberNo As String
Dim _CustNo As String
Dim curCheckboxIDVal As String
For Each cntrl As Control In Me.Controls
'If TypeOf cntrl Is System.Web.UI.WebControls.CheckBox Then
If cntrl.ID.ToString().Contains("ckbx")
If DirectCast(cntrl, CheckBox).Checked = True Then
Label2.Text = "label 2 text from checked"
curCheckboxIDVal = CStr(DirectCast(cntrl, CheckBox).ID)
coName = GetLabelTextForID(curCheckboxIDVal)
argVals = GetArgValsForCompanyName(coName)
_Unit = argVals(0)
_MemberNo = argVals(1)
_CustNo = argVals(2)
Label2.Text = _Unit
LabelDebug.Text = _MemberNo
Using conn As New SqlConnection(connStr), _
cmd As New SqlCommand(upd8DML, conn)
cmd.Parameters.Add("@Unit", SqlDbType.VarChar, 50).Value = _Unit
cmd.Parameters.Add("@MemberNo", SqlDbType.VarChar, 50).Value = _MemberNo
cmd.Parameters.Add("@CustNo", SqlDbType.VarChar, 50).Value = _CustNo
conn.Open()
cmd.ExecuteScalar()
End Using
End If
End If
Next
To get to the line that's throwing the error, cntrl has to be a valid Control in the Me.Controls collection; I reckon all Controls have an ID property. So why that err is being thrown on that line makes little, if any, sense to me.
See Question&Answers more detail:
os