'How do I loop through an MS SQL database with VB.NET?

I'm trying to implement the following php code in visual basic. I wrote it in PHP because I knew I could do what I wanted to in that language, but I can't for the life of me figure out how to get it working in Visual Basic to loop through a MS SQL database.

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'FakeDB');

$result = mysqli_query($con,"SELECT * FROM FakeDB");
$concatDupes = Array();
for ( $i = 0, $i< count($result), $i++) {
    if ( $result[$i] == $result[$i + 1] {
         array_push($concatDupes,$result[$i] + $result[$i + 1])
    } else {
            array_push( $concatDupes, $result[$i])
    }
}

I've tried to use the code in this post but to no avail. (I put MsgBox("hello") inside the while loop and nothing happened) I've also attempeted the following, also with zero success.

Dim i = Me.PaymentTableAdapter.GetData().HazWasteAmtColumn.ToString()
MsgBox(i) 'no message box
Dim i = Me.PaymentTableAdapter.GetData().Count
MsgBox(i) ' also no message box
Dim i = Me.PaymentTableAdapter.GetData().Count
MsgBox(GetType(i)) 'type of i is not defined

I know my query is returning results because it works fine in a DataGridView.

Other useful info:

  • Watching the i (any of the i's referenced above) always shows Nothing, no matter what I've tried.
  • Here's what happens when I call Public Sub ReadData(ByVal connectionString As String, ByVal queryString As String) (full function in linked page) with:

    Dim connect = WindowsApplication1.My.Settings.ShopMgtConnectionString
    Dim querty As String = "SELECT * "
    querty = querty + "FROM SM.PartItem "
    ReadData(connect, querty)
    

    immediate window output (the error list shows nothing):

    A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
    A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
    A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
    A first chance exception of type 'System.Data.ConstraintException' occurred in System.Data.dll
    A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll
    

EDIT: Here's the ReadData() method

Public Sub ReadData(ByVal connectionString As String, _
    ByVal queryString As String)
    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)

        connection.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader(0).ToString())
        End While
        reader.Close()
    End Using 
End Sub

EDIT, Again... I've taken a screen recording of the program running. It's just not doing anything useful.

http://youtu.be/i7oNVDXUw78



Solution 1:[1]

Make sure you are reading non null data:

While reader.Read()
   If reader.IsDBNull(0) = False Then
     Console.WriteLine(reader.GetValue(0).ToString())
   End If
End While

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1