views:

26

answers:

3

In my code I enter a string in a text box and have the query check the server for the entry. If it exists it returns the string and the date into two separate variables. The database only has TrackingNumber and Date. Below is the code. As of right now i get "Invalid attempt to read when no data is present." at the "If (sdr("TrackingNumber") IsNot Nothing) Then" line. What am i doing wrong?

    Public Function CreateSqlParameter(ByVal name As String, ByVal dbType As DbType, ByVal direction As ParameterDirection, ByVal value As Object) As SqlParameter

    Dim parameter As SqlParameter = New SqlParameter()
    parameter.ParameterName = name
    parameter.DbType = dbType
    parameter.Direction = direction
    parameter.Value = value
    Return parameter

End Function




Private Sub DupOKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DupOKButton.Click


    Dim trackNumber As String
    Dim dateSent As DateTime

    trackNumber = Me.DupTNText.Text

    Dim connectionString As String = "Data Source=XXXXX;Initial Catalog=ScannedDB;Integrated Security=True;Pooling=False;Encrypt=False"

    Dim cmdText As String = "SELECT TrackingNumber, [Date] FROM ScannedDBTable WHERE TrackingNumber = @TrackingNumber"

    Using connection As New SqlClient.SqlConnection(connectionString)

        Dim cmd As New SqlClient.SqlCommand(cmdText, connection)

        cmd.Parameters.Add(CreateSqlParameter("@TrackingNumber", DbType.String, ParameterDirection.Input, trackNumber))
        cmd.Parameters.Add(CreateSqlParameter("@Date", DbType.DateTime, ParameterDirection.Input, dateSent))

        connection.Open()

        Dim sdr As SqlDataReader = cmd.ExecuteReader()

        If (sdr("TrackingNumber") IsNot Nothing) Then
            trackNumber = Convert.ToString(sdr("@TrackingNumber"))
            dateSent = Convert.ToString(sdr("@Date"))
        Else
        End If

'Rest of code...
A: 

Is tracking number a varchar in the database?

If so, you may need to make you VB code send an int instead of a string.

Meiscooldude
@Meiscooldude i changed it to Int64 and still trhows the same error.
0bfus
A: 

If sdr is a reader you should check for DBNull.Value.

If (sdr("TrackingNumber") IS DBNull.Value)
  'it's null
else
  'it's not null
end if

Also read to see if anything exists in your reader first. If (reader.read()) then...

If (sdr.read()) then
  if (sdr("TrackingNumber") IS DBNull.Value) Then
        'its null
  else
        'not null
  end if
end if
JonH
A: 

Your probably not getting any rows back, try:

While (sdr.read())



        If (sdr("TrackingNumber") IsNot Nothing) Then 
            trackNumber = Convert.ToString(sdr("@TrackingNumber")) 
            dateSent = Convert.ToString(sdr("@Date")) 
        Else 
        End If     

End While
Gratzy
This is vb.net not C#.
JonH
Pretty sure he could make the adjustment
Gratzy
Just take out the {} and add `loop` at the end :)
egrunin
@egrunin - no loop wouldn't work. See edit
JonH
Thank you so much! Is it my understanding that sql reader statements need to be in while loops?
0bfus
@JonH now where would a simple MsgBox("Not in DB") go if the entered number is not in the database?
0bfus
@Obfus no it doesnt .read() advances the reader putting it a while loop, loops through all the rows.
Gratzy
@Gratzy Oh i see, thanks for the clarification. I am newer to the SQL part of programing. btw do you know where a simple MsgBox("Not in DB") go if the entered number is not in the database?
0bfus
@Obfus after the while you could do something like if dateSent = nothing then MsgBox("Not in DB") end. Not sure of the exact vb.net syntax
Gratzy