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

Discussion in 'ASP .Net' started by =?Utf-8?B?ZGF2aWQ=?=, Apr 5, 2006.

  1. 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.
    =?Utf-8?B?ZGF2aWQ=?=, Apr 5, 2006
    #1
    1. Advertising

  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" <> wrote in message
    news:...
    >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.
    >
    Swanand Mokashi, Apr 5, 2006
    #2
    1. Advertising

  3. =?Utf-8?B?ZGF2aWQ=?=

    zombie Guest

    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.
    zombie, Apr 5, 2006
    #3
  4. 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.
    >
    >
    =?Utf-8?B?ZGF2aWQ=?=, Apr 5, 2006
    #4
  5. 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.
    >
    >
    =?Utf-8?B?ZGF2aWQ=?=, Apr 5, 2006
    #5
  6. 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" <> wrote in message
    > news:...
    > >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.
    > >

    >
    >
    >
    =?Utf-8?B?ZGF2aWQ=?=, Apr 5, 2006
    #6
  7. 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.
    >
    >
    =?Utf-8?B?ZGF2aWQ=?=, Apr 5, 2006
    #7
  8. makes sense -- I think in C# :)
    "david" <> wrote in message
    news:...
    > 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" <> wrote in message
    >> news:...
    >> >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.
    >> >

    >>
    >>
    >>
    Swanand Mokashi, Apr 5, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shilpi Chaudhry
    Replies:
    1
    Views:
    3,025
    Hermit Dave
    Sep 15, 2004
  2. Matt
    Replies:
    8
    Views:
    53,434
    saravanavsk
    Jan 25, 2010
  3. Raheel Sharif
    Replies:
    0
    Views:
    136
    Raheel Sharif
    Sep 27, 2009
  4. Terry
    Replies:
    3
    Views:
    96
    Terry
    Oct 15, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    341
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page