Retrieving information from database into text box.

prabhu_venkatraman

New member
Joined
Apr 28, 2014
Messages
3
Programming Experience
3-5
Hi!

I am trying to retrive information data from Access database into textbox in vb.net. I am able to get the count but not able to display it in the form.

For example: total state in india is 29. so need to display all the names. I have textbox in the form as statename1.text, statename2.text, statename3.text till statename29.text. So I want to display in loop.

vb.net code.
dim i, p as integer
command = New OleDbCommand("Select state from country where country="India""", connection)
dataReader = command.ExecuteReader
dataReader.Read()

i = 1
P = 0


Do While i <> 30
statename & i &.text = dataReader(P).ToString()



P = P + 1
i = i + 1
Loop

I am trying something like this but i know, its not right. Please advice. I am getting error on "statename & i &.text"

Regards,

Prabhu Venkatraman
 
Last edited:
Instead of having a bunch of Textboxes on the form that takes up a bunch of space & makes coding this a little more difficult, why not use a ListView? You can loop the DataTable and just create a ListViewItem for each record in your DataTable.
 
That's a very bad Do loop. Don't use incremented counters in a Do or While loop. That's what For loops are for. If you're going to use a Do loop then it should be like this:
Do While dataReader.Read()
    '...
Loop
If you know exactly how many iterations you want to perform then you should be using a For loop like this:
For i = 0 To 28
    dataReader.Read()

    '...
Next
If you want to use the Do loop then you should put your TextBoxes into a Queue or Stack and then Dequeue or Pop the next TextBox from the collection inside the loop. If you want to use the For loop then you should put the TextBoxes into an array and use the loop counter as an index into that array.
 
but you haven't replied my question, how to name textbook dynamically means keep state name as fixed and just increment number....

statename1.text...statename2.text etc.
 
but you haven't replied my question, how to name textbook dynamically means keep state name as fixed and just increment number....

statename1.text...statename2.text etc.

That's because your question is moot. If you do it properly then the answer to your question is meaningless. It's like asking how many pairs of shoes you'll need to walk around the world when you're flying in a plane. Who cares?
 
Back
Top