vb.net datareader.. while read .... flll text boxes problem

I have a datareader
I also have a bunch of txtboxes on a web form

as its reading all of the records I need to populate the txtboxes with the values
the text boxes are named txtAge0, txtAge1, txtAge2 and so on......
and txtAcres0, txtAcres1, txtAcres2 and so on....

my data reader brings back two columns age, and Acres

what I have so far is the ability to get all the data... make sure it is correct, but cant figure out how to populate the txt boxes
 Protected Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
        'Dim ProjId As String = ProjectInfo.ProjID
        Dim sYear As String = cmbPlanYear.SelectedValue
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("LRT_GISConnectionString").ConnectionString
        Dim strQuery As String = "SELECT age, SUM(TotAc) FROM HarvestUnits WHERE Plan_Year like " & sYear & "Group BY age "
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()

            Dim sdr As SqlDataReader = cmd.ExecuteReader()          
       
            While sdr.Read()              
                
                MsgBox(sdr(0).ToString)
                MsgBox(sdr(1).ToString)    
                'this is where I would need to take sdr(0) and put it in txtAge0.txt
                and sdr(1) txtAcres0

               'on the next row it would be txtAge1 and txtAcres1
              
            End While
        Catch ex As Exception

        End Try
    End Sub

Open in new window

David ModugnoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
You should create the textboxes dynamically.
I am not sure if you are using ASP.NET or win forms,  ASP.NET example is shown below:
Dim txtAge As TextBox
Dim txtAcres As TextBox
Dim i As Integer = 0
If reader.HasRows Then
    Do While reader.Read()
        txtAge = New TextBox()
        txtAge.ID = "txtAge" & I
        txtAge.Text = sdr(0).ToString
        Panel1.Controls.Add(txtAge)
        txtAcres = New TextBox()
        txtAcres.ID = "txtAcres" & I
        txtAcres.Text = sdr(0).ToString
        Panel1.Controls.Add(txtAcres)
        //ASP.NET code to add a new line
        Dim lit As New Literal()
        lit.Text = "<br />"
        Panel1.Controls.Add(lit)
        //end ASP.NET code
	i = i + 1
    Loop
End If

Open in new window

Notes:
1) Panel1 is the panel containing the buttons in your UI page or form.
2) If you are targeting a desktop app like win forms then you need to provide Location property for every dynamic textbox:
txtAge.Location  = New Point(i * tzt1.Size.Height, 0)
txtAcres.Location  = New Point(i * tzt1.Size.Height, txt1.Size.Width)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Assuming your textboxes are named in sequence, like tx0, tx1, then you could do something like this:

Dim i As Integer
While sdr.Read()
     Controls("txtAge" & i.ToString) = sdr(0).ToString
     Controls("txtAcres" & i.ToString) = sdr(1).ToString
End While
David ModugnoAuthor Commented:
If I try -
      Controls("txtAge" & i.ToString) = sdr(0).ToString
I get an error that "property item is read only"
thanks
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

David ModugnoAuthor Commented:
Miguel,
Looking at your answer (and this is an asp web project) are you saying I have to create the text boxes dynamically?
David ModugnoAuthor Commented:
I am now creating the text boxes dynamically... worked great.. the results showed up perfect with one column the age and the other acres.. thanks
because the result is too lone I am breaking them up into 4 panels.. the problem is now the results show up in once column... age over acres and so on.. not sure how to fix it
here is how I am doing it
thanks
asp.net
    <asp:Panel ID="Panel1" runat="server" CssClass="FloatLeft" Width="300"></asp:Panel>
    <asp:Panel ID="Panel2" runat="server" CssClass="FloatLeft" Width="300"></asp:Panel>
    <asp:Panel ID="Panel3" runat="server" CssClass="FloatLeft" Width="300"></asp:Panel>
    <asp:Panel ID="Panel4" runat="server" CssClass="FloatLeft" Width="300"></asp:Panel>

Open in new window


vb
 If sdr.HasRows Then
            Do While sdr.Read()

                If i < 21 Then
                    txtAge = New TextBox()
                    txtAge.ID = "txtAge" & i
                    txtAge.Text = sdr(0).ToString
                    Panel1.Controls.Add(txtAge)
                    txtAcres = New TextBox()
                    txtAcres.ID = "txtAcres" & i
                    txtAcres.Text = sdr(1).ToString
                    Panel1.Controls.Add(txtAcres)
                    Dim lit As New Literal()
                    lit.Text = "<br />"
                    Panel1.Controls.Add(lit)

                ElseIf i < 41 Then
                    txtAge = New TextBox()
                    txtAge.ID = "txtAge" & i
                    txtAge.Text = sdr(0).ToString
                    Panel2.Controls.Add(txtAge)
                    txtAcres = New TextBox()
                    txtAcres.ID = "txtAcres" & i
                    txtAcres.Text = sdr(1).ToString
                    Panel2.Controls.Add(txtAcres)
                    Dim lit As New Literal()
                    lit.Text = "<br />"
                    Panel2.Controls.Add(lit)

                ElseIf i < 61 Then
                    txtAge = New TextBox()
                    txtAge.ID = "txtAge" & i
                    txtAge.Text = sdr(0).ToString
                    Panel3.Controls.Add(txtAge)
                    txtAcres = New TextBox()
                    txtAcres.ID = "txtAcres" & i
                    txtAcres.Text = sdr(1).ToString
                    Panel3.Controls.Add(txtAcres)
                    Dim lit As New Literal()
                    lit.Text = "<br />"
                    Panel3.Controls.Add(lit)

                Else
                    txtAge = New TextBox()
                    txtAge.ID = "txtAge" & i
                    txtAge.Text = sdr(0).ToString
                    Panel4.Controls.Add(txtAge)
                    txtAcres = New TextBox()
                    txtAcres.ID = "txtAcres" & i
                    txtAcres.Text = sdr(1).ToString
                    Panel4.Controls.Add(txtAcres)
                    Dim lit As New Literal()
                    lit.Text = "<br />"
                    Panel4.Controls.Add(lit)

                End If



                i = i + 1
            Loop
        End If

Open in new window

Miguel OzSoftware EngineerCommented:
Regarding your post question, if your remove:
CssClass="FloatLeft" Width="300"

does it work OK? (From the point of view of showing the textboxes correctly aligned one on the side of the other)

IF not - it seems like a CSS issue that could be better resolved in a CSS zone, please post the rule definition for "FloatLeft" and any other rule affecting textboxes or div. If you are using a third party CSS library like bootstrap please specify version as well as which VS/.NET version you are using.
David ModugnoAuthor Commented:
if i pull it out the panels are not next to each other and it does not change anything as far as the side by side text box issue

the strange thing is if I get rid of the other panels and just put all the info in one it looks right
 .FloatLeft
       {
           float:left
       }

Open in new window

David ModugnoAuthor Commented:
Ok even stranger... after I put back the .floatLeft... it works now... I used the undo arrow..
David ModugnoAuthor Commented:
That worked great .. thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.