Why does this not work?

shaunrigby

New member
Joined
Mar 31, 2007
Messages
4
Programming Experience
Beginner
Hi Guys,

I have the following while loop in my app, however when i run it only the last record from the DB is shown, in short, my app connects to the database and selects all records that meet criteria, then lists them all as labels, I have also tried adding it to a table layout panel howeverr that doesnt work either,
VB.NET:
[SIZE=2][COLOR=#0000ff]
Try
[/COLOR][/SIZE][SIZE=2]dbConn.Open()
dbReader = dbCommand.ExecuteReader()
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] dbReader.Read()
incomeLabel.Text = dbReader.GetString([/SIZE][SIZE=2][COLOR=#800000]"description"[/COLOR][/SIZE][SIZE=2])
incomeLabel.Name = dbReader.GetString([/SIZE][SIZE=2][COLOR=#800000]"description"[/COLOR][/SIZE][SIZE=2])
incomeLabel.Location = textLocation
[/SIZE][SIZE=2][COLOR=#008000]'incomeLabel.Anchor = AnchorStyles.Top
[/COLOR][/SIZE][SIZE=2]incomeLabel.BackColor = Color.Transparent
textLocation.Y = textLocation.Y + 50
FlowLayoutPanel.Controls.Add(incomeLabel)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MySqlException
MsgBox([/SIZE][SIZE=2][COLOR=#800000]"Query Error: "[/COLOR][/SIZE][SIZE=2] & ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MySqlException
MsgBox([/SIZE][SIZE=2][COLOR=#800000]"Connection Error: "[/COLOR][/SIZE][SIZE=2] & ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE]

Please feel free to move my post if it is on the wrong section
 
It sounds as you want to create lots of labels, but I don't see you create any, only reference one incomeLabel. To create a new label control in code:
VB.NET:
Dim newlabel As New Label
 
You have a couple of things that appear going wrong in your code, of course I don't know that you're showing all code.

You are narrowing your exceptions such that you are not seeing what is going wrong, try catching Ex as Exception. You are also not closing your reader when done with it. And as JohnH points out, you probably need to be creating new Label objects. Here is a sketch of the code you should be using:

VB.NET:
Try
 
Me.SuspendLayout()
 
dbConn.Open()
dbReader = dbCommand.ExecuteReader()
Dim nameCounter As Integer = 0
 
While dbReader.Read()
 
nameCounter += 1
Dim incomeLabel As New Label 'Or whatever the control
 
With incomeLabel
  .Text = dbReader("Description").ToString
  .Name = "LabelControl" & nameCounter
  .Location = textLocation
  .BackColor = Color.Transparent
End With
 
textLocation.Y += 50
FlowLayoutPanel.Controls.Add(incomeLabel)
 
End While
 
Catch Ex As Exception
   MessageBox.Show(Ex.Message)
Finally
   dbReader.Close
   dbConn.Close
   Me.ResumeLayout(False)
End Try
 
The others are right; you only ever create one label; repeatedly attempting to add the same label to the controls collection causes it to be ignored.

You know, a grid control would be so much easier for this.. Make a grid of:
No lines
Grey background
No headers
One column

Then fill it.

Youre doing your data access the hard way too. Read the DW2 link in my signature..
 
Back
Top