Hi
I have a some code that look like this...
For n = 1To 40 sCurrControl = ("DropDownList" & (n).ToString & ("a")) ddl = TryCast(Page.FindControl(sCurrControl), DropDownList) CheckDropDownList(ddl, txtSignature.Text)Next Private Sub CheckDropDownList(ByVal MyDdlAs DropDownList,ByVal SignatureAs String)Select Case MyDdl.SelectedValue
When I run this I get a "Object reference not set to an instance of an object" error on the "Select Case MyDdl.SelectedValue" line.
What am I doing wrong...?
Regards
check to see if your drop down list is nothing before using it in a method:
For n = 1To 40
sCurrControl = ("DropDownList" & (n).ToString & ("a"))
ddl = TryCast(Page.FindControl(sCurrControl), DropDownList)
If Not DDL is nothing then
CheckDropDownList(ddl, txtSignature.Text)
end if
Next
The only reason your code isn't throwing an exception before then is because you are using a TryCast,
From MSDN:
TryCast
Introduces a type conversion operation that does not throw an exception. If an attempted conversion fails, CType and DirectCast both throw an InvalidCastException error. This can adversely affect the performance of your application. TryCast returns Nothing (Visual Basic), so that instead of having to handle a possible exception, you need only test the returned result against Nothing.
I'd need to see some more code. How and where are you creating these 40 DropDownList controls? Are you creating them dynamically? I'm more concerned with why the TryCast is returning nothing (although you should be checking before calling the sub). Are the dropdown controls located in another control? The code below will print the control ID's to troubleshoot what the problem might be... Also post some more code to look at.
FOR EACH objControl as Control in Page.Controls
DEBUG.PRINT(objControl.ID)
NEXT
Hi
I dont create the dropdownlist dynamically. I can access each dropdownvalue in For i loop, its when I try to access the dropdown values inside the CheckDropDownList subroutine.
All you are doing is passing the dropdownlist control to the subroutine. You are passing 'Nothing' to the subroutine, which is causing the problem. The problem is in your for x loop when you are attempting ddl = trycast(...). Please verify that the dropdown controls are located in the object that you are searching for them in. I gave you some code to test that out.. What troubleshooting have you done at this point?
Should I place that code just before the for x loop or where should I place it?
I would place it before the for x loop. Also, it might be helpful to see your .aspx page. Are you using a master page? Finding the control is a bit more complicated with content pages. Post your .aspx code so we can see where your dropdownlists are located and maybe help figure out where things are going south.
Hi
I added this before the for x loop..
ForEach objControlAs ControlIn Page.ControlsResponse.Write(objControl.ID &"<br>")
Next
and the only thing that was written on the page was "form1", which is very weird. I have 40 dropdownlist controls and a number of labels etc on the page. The dropdown values gets their values from a datatable.
Hi again
Just wanted to inform that I found the error (entirely my fault), I checked for a dropdown list that didn't exsit.
MSDN: The method(FindControl) searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container...
On a aspx page controls are nested one in another. You have Page-Control, Page-Control contains the Form-Control, Form-Control contains HtmlGenericControl. In the HtmlGenericControl are the controls you see, such a Label or TextBox. the order can variate depends on how your page is composed (see messages from sswanner1)
Function SuperFindControl(ByVal rootAs Control,ByVal nameAs String)As Control'root: the control you search in 'name: ID of the control you search forDim nodeAs Control = rootDim iAs Integer If name =""Then SuperFindControl =Nothing Exit Function End If While True While node.HasControls node = node.Controls.Item(0)If (node.ID = name)Then Return nodeEnd If End While While True If node.Equals(root)Then Return Nothing End If For i = 0To (node.Parent.Controls.Count - 1)If node.Parent.Controls.Item(i).Equals(node)Then node = node.ParentExit For End If Next If node.Controls.Count >= (i + 2)Then node = node.Controls.Item(i + 1)If node.ID = nameThen Return nodeEnd If Exit While End If End While End While Return Nothing'End Function
Congratulations!
supplement: you are searching for controls and can't find it :(
possible reason: the controls are not created yet. f.e: if you are serching on Page.PreInit event for your DropDownList, you don't find it while it appears later
You'll probably want to call form.findControl(...) instead of page.findControl(...)
0 comments:
Post a Comment