Show all data in a single

teymoorei

Active member
Joined
Jan 11, 2012
Messages
36
Programming Experience
1-3
Where is my problem?

ad = New SqlDataAdapter("Select * from tblinfo", Connection)

ds = New DataSet
ad.Fill(ds)
Dim i = 0


While (i <= ds.Tables(0).Rows.Count + 1)
txtlist.Text = ds.Tables(0).Rows(i).Item(2).ToString
i = (i + 1)
End While

Please Help Me
 
For future reference, please actually explain what you're trying to achieve rather than expecting us to work it out from code that doesn't actually do it. Can we assume that you want to display the data from all the rows in the one TextBox? If so then consider this. Let's say that you have a dog and I give you a piece of paper with dog names on it (Rover, Fido, Spot). If you go through that list and change your dog's name to each one as you get to it, what will your dog's name be at the end? Will it be a combination of all the names or will it be just the last one? It's fairly obvious, isn't it? You're replacing the previous value each time, not combining them. That's exactly what your code is doing. You need to combine the values. I think that you should be able to combine two Strings without too much help.
 
Just finished typing this so I will post anyway to add some additional points.

Hi,

Firstly, when posting to the Forum you must always remember to post a full and clear explanation of what it is you are trying to do, what is not working and any exception messages that you may be getting so that we do not have to work it out for ourselves. The basic thing to remember is that the more that you help us to help you the better your changes of getting a clear answer first time round.

In your case, the reason that things are failing is due to this line of code:-

VB.NET:
While (i <= ds.Tables(0).Rows.Count + 1)

The mistake here is that there is no DataRow at .Rows.Count+1 since the last data row exists at location .Rows.Count-1. Now that's solved, let me take some time to make a few other comments:-

1) When posting code, please use Code tags on the Advanced button rather than Quote tags. It makes things easier to read and keeps the code formatting.

2) Turn both Option Strict and Option Explicit ON now to make sure you declare your variable types correctly and you pick up any type conversion errors as you encounter them while you code. This can be done in Tools->Options from the main menu for all future projects and in the Compile Tab from Project->Properties on the main menu for the current project.

3) If you are going to be using a single Database Table in your project then you should always use a DataTable object and not a DataSet object.

4) Always remember to use descriptive names for your variable declarations. You will thank yourself for doing this one day since as your projects grow bigger you will soon lose track of what variables are supposed to be doing what.

5) When iterating a collection with a FIXED number of objects you should always use a For Loop and not a While Loop since a While loop should only ever be used when you do not know the number of elements to be returned from the loop.

6) Do you realise that you are overwriting the information in the TextBox on every iteration of the Loop? This should be set to multiline and using the AppendText method of the TextBox.

Finally then, here is an example of how this can be cleaned up a bit (TextBox with Multiline set to True):-

VB.NET:
Dim myConnection As New SqlConnection("Data Source=IANVAIO\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=True")
Dim daEmployees As New SqlDataAdapter("Select * from Employees", myConnection)
Dim dtEmployees As New DataTable
 
daEmployees.Fill(dtEmployees)
For Each currentRow As DataRow In dtEmployees.Rows
  txtList.AppendText(String.Format("The Field Value is {0}{1}", currentRow.Item(2).ToString, Environment.NewLine))
Next

Hope that helps.

Cheers,

Ian
 
Try using a DataGridView; you can use the built-in controls (easiest); i.e. drag a DataGridView control onto your form; or you can write a class with the methods and procedures you want and build the form manually
 
Back
Top