Click here to Skip to main content
12,560,652 members (57,566 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET

I am new to 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)
       Dim reader As SqlDataReader = command.ExecuteReader()
       Do While (reader.Read())
   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
Rate this: bad
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
reader = sqlDataAdapter1.SelectCommand.ExecuteReader()
While reader.Read()
    For i = 0 To reader.FieldCount - 1
        recordData &= reader(i).ToString()
        recordData &= "-"
    recordData &= ControlChars.CrLf
    recordCount += 1
End While
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.[^]
Rate this: bad
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.

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 | Mobile
Web02 | 2.8.161026.1 | Last Updated 4 May 2010
Copyright © CodeProject, 1999-2016
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