0
Imports System.Data
Imports System.Data.OleDb

Public Class Form1
    Dim cnnOLEDB As New OleDbConnection
    Dim cmdOLEDB As New OleDbCommand
    Dim cmdInsert As New OleDbCommand
    Dim cmdDelete As New OleDbCommand
    Dim cmdUpdate As New OleDbCommand


    Dim conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Steven\Desktop\Database.accdb"

    Private Sub searchbtn_Click(sender As Object, e As EventArgs) Handles searchbtn.Click

        If ssearch.Text <> "" Then
            cmdOLEDB.CommandText = "SELECT StudentID from Students Where TP = " & CInt(ssearch.Text)
            cmdOLEDB.Connection = cnnOLEDB


            Dim rd As OleDbDataReader = cmdOLEDB.ExecuteReader()
            If rd.Read = True Then
                Form2.Show()
                Form2.TextBox1.Text = rd(0).ToString
            Else
                MessageBox.Show("Information not found in Database")

            End If
            rd.Close()
        End If

        Me.Hide()
        Form2.Show()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cnnOLEDB.ConnectionString = conn

        cnnOLEDB.Open()

    End Sub

    Private Sub ssearch_TextChanged(sender As Object, e As EventArgs) Handles ssearch.TextChanged

    End Sub

    Private Sub insertbtn_Click(sender As Object, e As EventArgs) Handles insertbtn.Click
        If ssearch.Text <> "" Then

            cmdInsert.CommandText = "INSERT INTO Students (StudentID) VALUES (" & ssearch.Text & ");"

            cmdInsert.CommandType = CommandType.Text
            cmdInsert.Connection = cnnOLEDB
            cmdInsert.ExecuteNonQuery()
            MsgBox(ssearch.Text & " " & "Record inserted.")
            ssearch.Text = ""
        Else
            MsgBox("Please Enter Student ID")

        End If
        cmdInsert.Dispose()

    End Sub
End Class

As you can see i can insert data into the database but I can't search it. When I run it it show errors on Dim rd As OleDbDataReader = cmdOLEDB.ExecuteReader() saying:

No value given for one or more required parameters visual basic error

What was my mistake?

varocarbas
  • 12,354
  • 4
  • 26
  • 37
user63566
  • 53
  • 7
  • Did you try and remove the CINT in the execute reader command, or make that part of the if evaluation? Also, you may want to terminate the statement like you did with the update. – Vinnie Sep 11 '15 at 11:09
  • Problem is in You sql query. Instead `& CInt(ssearch.Text)` use `& ssearch.Text`... without converting to `integer`.... and, instead `If rd.Read = True` use `If rd.HasRows = True : rd.Read() : ...` and rest of Your code. – nelek Sep 11 '15 at 11:09
  • @nelek also I am not sure about the exact point of your suggestions. Why do you think that CInt(ssearch.Text) is wrong? It is definitively not doing too much, but it is OK as far as ints can be implicitly converted into strings (= no error is triggered because of this). – varocarbas Sep 11 '15 at 11:15

0 Answers0