Click here to Skip to main content
13,150,385 members (31,603 online)
Rate this:
 
Please Sign up or sign in to vote.
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:

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 16-Apr-10 23:57pm
Updated 17-Apr-10 0:51am
DaveAuld193.5K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Example straight from the docs;

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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 4 May 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100