Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to vb.net and i am working on a project which shows data from the ms sql db to the listview. However, i am having a problem and got stucked as below:

VB.NET
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       lvwTest.View = View.Details
       lvwTest.Columns.Add("Customer Name", 100, HorizontalAlignment.Left)


       Dim command As SqlCommand = _
           New SqlCommand("SELECT CustName FROM Report", Connection)

       Connection.Open()
       Dim reader As SqlDataReader = command.ExecuteReader()
       Do While (reader.Read())
           lvwTest.Items.Add(reader.GetInt32(0))       
Loop
       reader.Close()
       Connection.Close()
   End Sub

Can someone help me out with this problem.

Thank you very much.
Posted
Updated 17-Apr-10 0:51am
v2

Example straight from the docs;

VB.NET
Dim reader As SqlClient.SqlDataReader
Dim recordData As String = ""
Dim recordCount As Integer = 0
Dim i As Integer = 0
sqlConnection1.Open()
reader = sqlDataAdapter1.SelectCommand.ExecuteReader()
While reader.Read()
    For i = 0 To reader.FieldCount - 1
        recordData &= reader(i).ToString()
        recordData &= "-"
    Next
    recordData &= ControlChars.CrLf
    recordCount += 1
End While
sqlConnection1.Close()
MessageBox.Show("Records processed: " & recordCount)
MessageBox.Show("Data:" & ControlChars.CrLf & recordData)


However, i would use a bindingsource, and then bind the controls to that.

There is a good video on MSDN on Forms over Data, and there are a few others in the series that are worth watching.
http://msdn.microsoft.com/en-gb/vbasic/bb725824.aspx[^]
 
Share this answer
 
You don't specify exactly what the problem is, so it's hard to say.

I do however notice that you're trying to read an integer value, and the name of your listview column ("Customer Name") suggests that the CustName field is a varchar/string.

In that case you should use reader.GetString(0) instead of reader.GetInt32(0)

Good luck,
Johnny J.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900