[VB.NET]Datareader

hobbes2103

Active member
Joined
Jul 10, 2003
Messages
43
Hello!

Here is my code :

Dim myConnection As New OleDb.OleDbConnection
Dim myReader As OleDb.OleDbDataReader
Dim myConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\erco\erco.mdb"
myBase = New OleDb.OleDbConnection(myConnectionString)
myBase.Open()

Dim myCommand As New OleDb.OleDbCommand
Dim mySelect As String = "SELECT code,name FROM articles WHERE code=1"
myCommand = New OleDb.OleDbCommand(mySelect, myBase)
myReader = myCommand.ExecuteReader()

Dim str As String
While (myReader.Read())
str = myReader("name")
End While
myReader.Close()
 
Now, my problem is : if "name" is empty (it is not my primary key, so sometimes there is no name), it wont work.
I would like str to get the value "".
 
try this :
Code:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim sConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyDb.mdb;"
        Dim Conn As New OleDb.OleDbConnection(sConnString)

        Try
            Dim sCommand As String = "Select * From Table1"

            Conn.Open()
            Dim Command As New OleDb.OleDbCommand(sCommand, Conn)
            Dim reader As OleDb.OleDbDataReader
            reader = Command.ExecuteReader
            Dim x As Integer
            For x = 0 To reader.FieldCount - 1
                MessageBox.Show(reader.GetName(x)) /// the names of your fields.
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Conn.Close()
        End Try
    End Sub
it may help.
 
Code:
        Dim str As String
        Dim myReader As OleDbDataReader
        While (myReader.Read())
            If Not IsDBNull(myReader("name")) Then
                str = myReader("name")
            Else
                str = ""

            End If

        End While
        myReader.Close()



Jon
 
Back
Top