Basically, if the Combo box selection (Field) is "Engineering", then only a row which corresponds to that selection will display.I have achieved this with the code below. I want to access the next row of Data which similar and only corresponds to this (Field) selection and fill the Text box Controls appropriately. All data saved under that combo box selection in that row is displayed. This works fine. The Module of Code below this under Button1_Click is my attempt at accessing the next row, however incorrect data is displayed. Is there anyway of fixing and display the data required which is - the next whole row of SQL data where the combo box selection meets criteria in the database.
Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Dim Command As New SqlCommand("Select * From Table_Upload where Field = @Field", Connection)
Dim DA As New SqlDataAdapter(Command)
Dim DT As New DataTable()
Dim DR As SqlDataReader
Command.Parameters.Add("@Field", SqlDbType.VarChar).Value = CmbField.SelectedItem
DA.Fill(DT)
CmbField.DataSource = DT
CmbField.DisplayMember = "Field"
Connection.Open()
DR = Command.ExecuteReader()
If DR.Read() Then
TxtTitle.Text = DR(0)
TxtComp.Text = DR(1)
TxtDate.Text = DR(2)
TxtSalary.Text = DR(3)
CmbEmp.SelectedItem = DR(4)
TxtDesc.Text = DR(5)
TxtExp.Text = DR(6)
TxtPhone.Text = DR(7)
TxtEmail.Text = DR(8)
TxtAddr.Text = DR(9)
End If
Connection.Close()
End Sub
CODE TO GO NEXT : Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnNext.Click Dim Command As New SqlCommand("Select * From Table_Upload where Field = @Field", Connection) Dim DA As New SqlDataAdapter(Command) Dim DS As New DataSet() Dim DR As SqlDataReader
Command.Parameters.Add("@Field", SqlDbType.VarChar).Value = CmbField.SelectedItem
DA.Fill(DS, "Table_Upload")
CmbField.DisplayMember = "Field"
Connection.Open()
DR = Command.ExecuteReader()
If DR.Read() Then
TxtTitle.Text = DR(+1).ToString()
TxtComp.Text = DR(+1).ToString()
TxtDate.Text = DR(+1).ToString()
TxtSalary.Text = DR(+1).ToString()
CmbEmp.SelectedItem = DR(+1).ToString()
TxtDesc.Text = DR(+1).ToString()
TxtExp.Text = DR(+1).ToString()
TxtPhone.Text = DR(+1).ToString()
TxtEmail.Text = DR(+1).ToString()
TxtAddr.Text = DR(+1).ToString()
End If
Connection.Close()
End Sub
Command.Parameters.Add("@Field", SqlDbType.VarChar).Value = CmbField.SelectedItem
, should return an Error, becauseCmbField.SelectedItem
is anObject
– evry1fallsSelectItem
is just fine. By coincidence, the type ofSqlParameter.Value
is alsoObject
. – Joel Coehoorn