Reputation: 1
I m trying to fetch total in to TextBox5.text but error occured like No value given for one or more required parameters.
Try con.Close()
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Pradnya_DataB.mdb"
con.Open()
ss = "Select total from stock_bottle WHERE Bottle_type=" & Convert.ToString(TextBox3.Text.ToString())
cmd = New OleDbCommand(ss, con)
rd = cmd.ExecuteReader()
If rd.Read() Then
TextBox5.Text = rd("total").ToString()
End If
con.Close()
rd.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Upvotes: 0
Views: 16
Reputation: 216253
This error occurs when one or more of your column names and table name are not spelled correctly. In your case, this means that Total
or Bottle_type
or even Stock_Bottle
are not the correct name for your fields or table.
Saying that, and pending the due correction to your query, I suggest you to use a more robust code like this one
Try
Dim ss = "Select total from stock_bottle WHERE Bottle_type=@btype"
Using con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Pradnya_DataB.mdb")
Using cmd = new OleDbCommand(ss, con)
con.Open()
cmd.Parameters.Add("@btype", OleDbType.Int32).Value = TextBox3.Text
Dim result = cmd.ExecuteScalar()
If result IsNot Nothing Then
TextBox5.Text = result.ToString()
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
Here I have changed your query to a parameterized query. It is more safe and robust about the correct interpretation of the DataType used for your where clause.
I have also removed the global variable for the connection. (You don't get any advantage and you need to constantly check for the status of that connection).
I have added the Using statement to ensure proper closing of connection and the immediate release of the system resources kept by the connection.
Finally, if you need to retrieve just one row with one single column then do not use the more expensive OleDbDataReader
(useful when there are many rows and columns to read back) but directly the OleDbCommand with its ExecuteScalar method
Upvotes: 1