473,498 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically assign textbox ID to a textbox variable in a loop?

I try to use "for" loop to assign textbox control ID to a textbox variable in
server side codebehind for a web form. But I met some problem.
What I want to do is solving the following-like code by a loop:
static code:
txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1")
txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2")
.....
txtQ10.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q10")

where Q1, .. Q10 are field name of a table; and txtQ1, ..., txtQ10 are
textbox control ID in design.

I implement them again in a FOR loop as follows
------code -------
Dim field As String = "Q"
Dim score As TextBox
For i As Integer = 1 To 35
field = "Q" + CStr(i)
score.ID = "txtQ" + CStr(i)
If Not IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
Else
score.Text = "NA"
End If

Next
--------------
I got error message:
----------------------
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 228: For i As Integer = 1 To 35
Line 229: field = "Q" + CStr(i)
Line 230: score.ID = "txtQ" + CStr(i)
Line 231: If Not
IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
Line 232: score.Text =
ds.Tables("mmsSpecRecord").Rows(0)(field)
Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230

------------------------------------
How to change the string to textbox ID object?

Thank you for your help.

Apr 5 '06 #1
7 9756
Hi,

You are getting the error message because you have not initialized the
TextBox object. Put the line score = new TextBox at the start of the
For loop, it will work. Also if you are initialiazing an object in the
loop it would be good if you put score = nothing at the end of the loop
if you are not going to use this object again.

Regards,
Peeyush.

Apr 5 '06 #2
Have you tried using the FindControl method ?

Dim score As TextBox
Dim id as String = "txtQ" + CStr(i)

score = (TextBox) Page.FindControl(id)

and then score.Text = .... etc
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services

"david" <da***@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
I try to use "for" loop to assign textbox control ID to a textbox variable
in
server side codebehind for a web form. But I met some problem.
What I want to do is solving the following-like code by a loop:
static code:
txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1")
txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2")
....
txtQ10.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q10")

where Q1, .. Q10 are field name of a table; and txtQ1, ..., txtQ10 are
textbox control ID in design.

I implement them again in a FOR loop as follows
------code -------
Dim field As String = "Q"
Dim score As TextBox
For i As Integer = 1 To 35
field = "Q" + CStr(i)
score.ID = "txtQ" + CStr(i)
If Not IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
Else
score.Text = "NA"
End If

Next
--------------
I got error message:
----------------------
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 228: For i As Integer = 1 To 35
Line 229: field = "Q" + CStr(i)
Line 230: score.ID = "txtQ" + CStr(i)
Line 231: If Not
IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
Line 232: score.Text =
ds.Tables("mmsSpecRecord").Rows(0)(field)
Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230

------------------------------------
How to change the string to textbox ID object?

Thank you for your help.

Apr 5 '06 #3
Thank you both of you, I will try it immediately.

David

"zombie" wrote:
Hi,

You are getting the error message because you have not initialized the
TextBox object. Put the line score = new TextBox at the start of the
For loop, it will work. Also if you are initialiazing an object in the
loop it would be good if you put score = nothing at the end of the loop
if you are not going to use this object again.

Regards,
Peeyush.

Apr 5 '06 #4
Hi, zombie:
I have tried it as your comment, there is no error. But the score as i=10 is
not txtQ10. So the txtQ10.Text = "" and score.Text = 0 as i=10 (from
database).

David

"zombie" wrote:
Hi,

You are getting the error message because you have not initialized the
TextBox object. Put the line score = new TextBox at the start of the
For loop, it will work. Also if you are initialiazing an object in the
loop it would be good if you put score = nothing at the end of the loop
if you are not going to use this object again.

Regards,
Peeyush.

Apr 5 '06 #5
Thank you, Swanand.
Combined your comment and zombie's, it works. The type conversion would be
score = CType(FindControl("txtQ" + CStr(i)), TextBox)

David

"Swanand Mokashi" wrote:
Have you tried using the FindControl method ?

Dim score As TextBox
Dim id as String = "txtQ" + CStr(i)

score = (TextBox) Page.FindControl(id)

and then score.Text = .... etc
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services

"david" <da***@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
I try to use "for" loop to assign textbox control ID to a textbox variable
in
server side codebehind for a web form. But I met some problem.
What I want to do is solving the following-like code by a loop:
static code:
txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1")
txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2")
....
txtQ10.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q10")

where Q1, .. Q10 are field name of a table; and txtQ1, ..., txtQ10 are
textbox control ID in design.

I implement them again in a FOR loop as follows
------code -------
Dim field As String = "Q"
Dim score As TextBox
For i As Integer = 1 To 35
field = "Q" + CStr(i)
score.ID = "txtQ" + CStr(i)
If Not IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
Else
score.Text = "NA"
End If

Next
--------------
I got error message:
----------------------
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 228: For i As Integer = 1 To 35
Line 229: field = "Q" + CStr(i)
Line 230: score.ID = "txtQ" + CStr(i)
Line 231: If Not
IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
Line 232: score.Text =
ds.Tables("mmsSpecRecord").Rows(0)(field)
Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230

------------------------------------
How to change the string to textbox ID object?

Thank you for your help.


Apr 5 '06 #6
Thank you, zombie.
Combined your comment and Swanand's, it works. The type conversion would be
score = CType(FindControl("txtQ" + CStr(i)), TextBox)

David
"zombie" wrote:
Hi,

You are getting the error message because you have not initialized the
TextBox object. Put the line score = new TextBox at the start of the
For loop, it will work. Also if you are initialiazing an object in the
loop it would be good if you put score = nothing at the end of the loop
if you are not going to use this object again.

Regards,
Peeyush.

Apr 5 '06 #7
makes sense -- I think in C# :)
"david" <da***@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
Thank you, Swanand.
Combined your comment and zombie's, it works. The type conversion would be
score = CType(FindControl("txtQ" + CStr(i)), TextBox)

David

"Swanand Mokashi" wrote:
Have you tried using the FindControl method ?

Dim score As TextBox
Dim id as String = "txtQ" + CStr(i)

score = (TextBox) Page.FindControl(id)

and then score.Text = .... etc
--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services

"david" <da***@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
>I try to use "for" loop to assign textbox control ID to a textbox
>variable
>in
> server side codebehind for a web form. But I met some problem.
> What I want to do is solving the following-like code by a loop:
> static code:
> txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1")
> txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2")
> ....
> txtQ10.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q10")
>
> where Q1, .. Q10 are field name of a table; and txtQ1, ..., txtQ10 are
> textbox control ID in design.
>
> I implement them again in a FOR loop as follows
> ------code -------
> Dim field As String = "Q"
> Dim score As TextBox
> For i As Integer = 1 To 35
> field = "Q" + CStr(i)
> score.ID = "txtQ" + CStr(i)
> If Not IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) )
> Then
> score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
> Else
> score.Text = "NA"
> End If
>
> Next
> --------------
>
>
> I got error message:
> ----------------------
> 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 228: For i As Integer = 1 To 35
> Line 229: field = "Q" + CStr(i)
> Line 230: score.ID = "txtQ" + CStr(i)
> Line 231: If Not
> IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field) ) Then
> Line 232: score.Text =
> ds.Tables("mmsSpecRecord").Rows(0)(field)
>
>
> Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230
>
> ------------------------------------
> How to change the string to textbox ID object?
>
> Thank you for your help.
>


Apr 5 '06 #8

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

Similar topics

4
12539
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
3
12459
by: N. Demos | last post by:
How do you dynamically assign a function to an element's event with specific parameters? I know that the code is different for MSIE and Mozilla, and need to know how to do this for both. I...
2
3631
by: Bill Agee | last post by:
I have a subform called PaymentDetails and would like to dynamically assign the recordsource after the form/subform is opened. The recordsource for Payment Details is "PaymentDetails_qry" which...
0
3368
by: cyberbless | last post by:
I would like to dynamically assign a Select Statment to a a SqlDataSource. Problem is the SqlDataSource.Select() Command requires a "dataSourceSelectArgument" as one of its arguments. 1. What...
0
1163
by: petri | last post by:
Could any one say me,how to create a row dynamically with a textbox and comobox in a button click in a grid.Also i need to create the textbox and combobox with id as i need to do calculation...
4
2182
by: .Net Sports | last post by:
I need to dynamically assign a datalist attribute upon a helper function receiving data from a querystring. If a user picks a certain region, i need the datalist to display its back color, or any...
9
5708
by: yookify | last post by:
I want to assign a textbox value to dropdownlistbox,where it should be updated in my database... Can u please give me some suggestions? In textbox the date is assigned. In dropdownlistbox one user...
2
5348
by: Zabivb | last post by:
how to use mkdir to create dynamically folders using textbox ? coding ====== <?php $foldername="text"; mkdir = $foldername; ?>
1
1703
by: shinu bhaskar | last post by:
how to assign a JavaScript variable to a jsp variable in a single page Ex : <script type="text/javascript"> var MM=Calculate_Form('<%=DATE_INSTALL%>','<%=TILLDATE%>'); alert(MM); </script>...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
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
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6881
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...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.