Hi, i am working on this code that is going to query a database for an entry and return the value of the tracking number and the time to print on a label if the tracking number exist in the databse. it does not right now it only seems to reprint the number that is enetered in a text box and it does not grab the date. any ideas? here is the code.
Dim TrNum As String = Me.DupTNText.Text
Dim TrDate As <=something if it is even needed
Dim connectionString As String = "Data Source=XXXXX;Initial Catalog=YYYYY;Integrated Security=True;Pooling=False;Encrypt=False"
Dim cmdText As String = "SELECT TrackingNumber, Date" & _
"FROM YYYYY " & _
"WHERE TrackingNumber = @TrackingNumber, Date = @Date"
Using connection As New SqlClient.SqlConnection(connectionString)
Dim cmd As New SqlClient.SqlCommand(cmdText, connection)
cmd.Parameters.AddWithValue("@TrackingNumber", TrNum)
cmd.Parameters.AddWithValue("@Date", TrDate)
connection.Open()
'this is where my print code begins and it works fine it just uses TrNum for the tracking number and TrDate for the date.
Here is my edited code with the changes applied, am i getting closer?
Dim dupText
Dim trackingNumber As String
Dim dateSent
dupText = Me.DupTNText
Dim connectionString As String = "Data Source=XXXXX;Initial Catalog=YYYYY;Integrated Security=True;Pooling=False;Encrypt=False"
Dim cmdText As String = "SELECT TrackingNumber, Date" & _
"FROM YYYYY " & _
"WHERE TrackingNumber = @TrackingNumber, Date = @Date"
Using connection As New SqlClient.SqlConnection(connectionString)
Dim cmd As New SqlClient.SqlCommand(cmdText, connection)
cmd.Parameters.AddWithValue("@TrackingNumber", dupText)
cmd.Parameters("@TrackingNumber").Direction = ParameterDirection.Input
connection.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If (sdr("TrackingNumber") IsNot Nothing) Then
trackingNumber = Convert.ToString(sdr("TrackingNumber"))
dateSent = Convert.ToString(sdr("Date"))
End If
EDIT #3
Changes Applied, i am pretty sure its the query string now. i get
"The parameterized query '(@TrackingNumber nvarchar(4000),@Date datetime)SELECT TrackingNu' expects the parameter '@TrackingNumber', which was not supplied."
witht the string as it is below. What other part of the code would you need to see to help determine the problem? I feel like i am close and thanks i am learning a bunch from this! sorry also for the change of variable names.
Private Sub DupOKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DupOKButton.Click
Dim dupText
Dim trackingNumber As String
Dim dateSent
dupText = Me.DupTNText
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 AND Date = @Date"
Using connection As New SqlClient.SqlConnection(connectionString)
Dim cmd As New SqlClient.SqlCommand(cmdText, connection)
cmd.Parameters.Add(CreateSqlParameter("@TrackingNumber", DbType.String, ParameterDirection.Input, trackingNumber))
cmd.Parameters.Add(CreateSqlParameter("@Date", DbType.DateTime, ParameterDirection.Input, dateSent))
connection.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If (sdr("TrackingNumber") IsNot Nothing) Then
trackingNumber = Convert.ToString(sdr("TrackingNumber"))
dateSent = Convert.ToString(sdr("Date"))
End If
'Printing code...
EDIT #4
Private Sub DupOKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DupOKButton.Click
Dim trackingNumber As String
Dim dateSent As DateTime
trackingNumber = 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 AND Date = @Date"
Using connection As New SqlClient.SqlConnection(connectionString)
Dim cmd As New SqlClient.SqlCommand(cmdText, connection)
cmd.Parameters.Add(CreateSqlParameter("@TrackingNumber", DbType.String, ParameterDirection.Input, trackingNumber))
cmd.Parameters.Add(CreateSqlParameter("@Date", DbType.DateTime, ParameterDirection.Input, dateSent))
connection.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If (sdr("TrackingNumber") IsNot Nothing) Then
trackingNumber = Convert.ToString(sdr("TrackingNumber"))
dateSent = Convert.ToString(sdr("Date"))
End If
EDIT #5
Private Sub DupOKButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DupOKButton.Click
Dim trackingNumber As String
Dim dateSent As DateTime
trackingNumber = 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, trackingNumber))
'cmd.Parameters.Add(CreateSqlParameter("@Date", DbType.DateTime, ParameterDirection.Input, dateSent))
connection.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
If (sdr("TrackingNumber") IsNot Nothing) Then
trackingNumber = Convert.ToString(sdr("TrackingNumber"))
dateSent = Convert.ToString(sdr("Date"))
Else
End If