G
Guest
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
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