473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Control Search

I have a function below which is searching for a control on a page.
When i debug this function the first time it worked fine when i was
stepping through it then when i tried it again it error with the error
of object not set to an instance of an object I didn't change anything
between the two debugging attempts does any one know why it would
error.

Public Function getControl(ByVal theControl As ControlCollection, ByVal
toFind As String) As Control

Dim ctrl As Control = Nothing

For i As Integer = 1 To theControl.Count - 1
'If Not theControl.Item(i) Is Nothing Then
If theControl.Item(i).ID.ToString.ToUpper = toFind.ToUpper
Then
ctrl = theControl.Item(i)
Exit For
End If
'End If
Next

Return (ctrl)

End Function

Jan 10 '06 #1
3 1077
you can write that much more elegantly using a foreach and making proper use
of return

foreach control as Control in theControl
if control.ID.ToString.ToUpper = toFind.ToUpper then
return control
end if
next
return nothing

It's hard to see where your error is without knowing more about the context.
which line is giving the null reference? My guess is that theControl is
nothing, so when you do theControl.Count you get an error.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"bannaman" <jk****@f2s.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
I have a function below which is searching for a control on a page.
When i debug this function the first time it worked fine when i was
stepping through it then when i tried it again it error with the error
of object not set to an instance of an object I didn't change anything
between the two debugging attempts does any one know why it would
error.

Public Function getControl(ByVal theControl As ControlCollection, ByVal
toFind As String) As Control

Dim ctrl As Control = Nothing

For i As Integer = 1 To theControl.Count - 1
'If Not theControl.Item(i) Is Nothing Then
If theControl.Item(i).ID.ToString.ToUpper = toFind.ToUpper
Then
ctrl = theControl.Item(i)
Exit For
End If
'End If
Next

Return (ctrl)

End Function

Jan 10 '06 #2
Thanks for your response. it errors on the line. The parameter
theControl is populated when it is passed into the function but
something odd goes on after this

If theControl.Item(i).ID.ToString.ToUpper = toFind.ToUpper

Jan 10 '06 #3
It's possible that the control collection is being modified, though I doubt
that.

it is possible for ID to be nothing, so doing ID.ToString() might cause the
error. This is especially true if the control is being dynamically
added..though it might happen in other cases. If you debug your code, it
should be easy for you to figure out what value is nothing.

On a side note, if you have a parent control, you can simply do,
parentControl.FindControl("ID")

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"bannaman" <jk****@f2s.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Thanks for your response. it errors on the line. The parameter
theControl is populated when it is passed into the function but
something odd goes on after this

If theControl.Item(i).ID.ToString.ToUpper = toFind.ToUpper

Jan 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Kao | last post by:
HI All: I am currently implementing a TextBox to allow user to type the things the user wants to search. The actual search routine is happening on Textbox Validated event. My user wants me trap...
2
by: buran | last post by:
Dear ASP.NET Programmers, I have a web user control (a search menu) which has 2 validation controls (one for input and another for the search criterion). I am including this search user control...
4
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
3
by: Michael | last post by:
Hi all.. I have a solution containing 5 projects, one of which is a custom control. In one of the projects, I dragged the .ascx file from the custom control project and dropped it onto a form....
1
by: Mia Williams via .NET 247 | last post by:
(Type your message here) I have user control (listpanel control) and I ahve to display the search results inside the list panel user control.. how can I do that? Can i access the list panel control...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
5
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
2
by: Italian Pete | last post by:
Hi, I have a search bar as a User Control sitting on a Page. The asp web control search button has its OnClick event set to a method "Search" which sets a bool isSearch to true. When I try to...
0
by: sbuggle | last post by:
Hi, I posted an earilier post vb6 and shellexecute to accomplish my task but think this is better with the webrowser control. I have to do a search with vb6 and somehow open up the third url on...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.