Hi everyone. I know this is probably an easy question to answer ( I havent written code in a few years and i cant figure out whats going on with this code)
I am getting data from a SQL database through vb using inline SQL and also the SQLdatareader. I am getting back results because it does enter the .hasrows if statement. but soon as i try to populate a text box with the first item in the reader it kicks out of the if statement. my code is posted below. any help would be great. i have been stuck on this for a day now and am growing frustrated with it.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connstr As String = ConfigurationManager.ConnectionStrings("MaintenancePlannerConnectionString").ConnectionString
Dim conn As New SqlConnection(connstr)
Dim cmdselect As New SqlCommand("Select vehiclenumber,vehicleyear,vehiclemake,vehiclemodel,VehicleCargoInfo,VehicleLicense," & _
"VehicleRegistrationExp,VehicleRegistrationAddress,VehicleInspection,VehicleVinNumber,VehicleTitle,VehicleTitleIssued,VehicleTireSize," & _
"VehicleWeight,GVWR,VehicleClass,VehicleAlarm,VehicleRearHVAC,VehicleCost,VehiclePurchaseDate,vehicleinfo from vehicleinformation where vehicleid='" + ddvehicle.SelectedItem.Value + "'", conn)
txtmakeupdate.Text = "test"
Dim dtrReader As SqlDataReader
Try
conn.Open()
dtrReader = cmdselect.ExecuteReader()
If dtrReader.HasRows Then
txtyearupdate.Text = dtrReader.GetValue(1) 'i had a 0 in here at first, and i have tried 1 not sure what the first column would be indexed as. neither of them worked
txtmakeupdate.Text = dtrReader.GetValue(1)
txtmodelupdate.Text = dtrReader.GetValue(2)
txtcargoupdate.Text = dtrReader.GetValue(3)
txtplateupdate.Text = dtrReader.GetValue(4)
txtregexpupdate.Text = dtrReader.GetValue(5)
txtregaddupdate.Text = dtrReader.GetValue(6)
txtvinupdate.Text = dtrReader.GetValue(7)
txttitlenumberupdate.Text = dtrReader.GetValue(8)
txttitleIssueDate.Text = dtrReader.GetValue(9)
txttiresizeupdate.Text = dtrReader.GetValue(10)
txtWeightUpdate.Text = dtrReader.GetValue(11)
txtGvwrupdate.Text = dtrReader.GetValue(12)
txtVehicleClassupdate.Text = dtrReader.GetValue(13)
DDalarmUpdate.SelectedItem.Equals(dtrReader.GetValue(14))
ddHVACupdate.SelectedItem.Equals(dtrReader.GetValue(15))
txtCostUpdate.Text = dtrReader.GetValue(16)
txtpurchasedateupdate.Text = dtrReader.GetValue(17)
txtvehicleinfoupdate.Text = dtrReader.GetValue(18)
End If
dtrReader.Close()
conn.Close()
Catch ex As Exception
End Try
End Sub
I am getting data from a SQL database through vb using inline SQL and also the SQLdatareader. I am getting back results because it does enter the .hasrows if statement. but soon as i try to populate a text box with the first item in the reader it kicks out of the if statement. my code is posted below. any help would be great. i have been stuck on this for a day now and am growing frustrated with it.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connstr As String = ConfigurationManager.ConnectionStrings("MaintenancePlannerConnectionString").ConnectionString
Dim conn As New SqlConnection(connstr)
Dim cmdselect As New SqlCommand("Select vehiclenumber,vehicleyear,vehiclemake,vehiclemodel,VehicleCargoInfo,VehicleLicense," & _
"VehicleRegistrationExp,VehicleRegistrationAddress,VehicleInspection,VehicleVinNumber,VehicleTitle,VehicleTitleIssued,VehicleTireSize," & _
"VehicleWeight,GVWR,VehicleClass,VehicleAlarm,VehicleRearHVAC,VehicleCost,VehiclePurchaseDate,vehicleinfo from vehicleinformation where vehicleid='" + ddvehicle.SelectedItem.Value + "'", conn)
txtmakeupdate.Text = "test"
Dim dtrReader As SqlDataReader
Try
conn.Open()
dtrReader = cmdselect.ExecuteReader()
If dtrReader.HasRows Then
txtyearupdate.Text = dtrReader.GetValue(1) 'i had a 0 in here at first, and i have tried 1 not sure what the first column would be indexed as. neither of them worked
txtmakeupdate.Text = dtrReader.GetValue(1)
txtmodelupdate.Text = dtrReader.GetValue(2)
txtcargoupdate.Text = dtrReader.GetValue(3)
txtplateupdate.Text = dtrReader.GetValue(4)
txtregexpupdate.Text = dtrReader.GetValue(5)
txtregaddupdate.Text = dtrReader.GetValue(6)
txtvinupdate.Text = dtrReader.GetValue(7)
txttitlenumberupdate.Text = dtrReader.GetValue(8)
txttitleIssueDate.Text = dtrReader.GetValue(9)
txttiresizeupdate.Text = dtrReader.GetValue(10)
txtWeightUpdate.Text = dtrReader.GetValue(11)
txtGvwrupdate.Text = dtrReader.GetValue(12)
txtVehicleClassupdate.Text = dtrReader.GetValue(13)
DDalarmUpdate.SelectedItem.Equals(dtrReader.GetValue(14))
ddHVACupdate.SelectedItem.Equals(dtrReader.GetValue(15))
txtCostUpdate.Text = dtrReader.GetValue(16)
txtpurchasedateupdate.Text = dtrReader.GetValue(17)
txtvehicleinfoupdate.Text = dtrReader.GetValue(18)
End If
dtrReader.Close()
conn.Close()
Catch ex As Exception
End Try
End Sub