returning a single value thru an OleDbCommand
-
Hello again, I need some help with a simple OLEDB query. I need to return one value from an Access database. the code below is a snapshot of what i have so far.
Imports System.Data.OleDb Public Class Prices Public Shared ReadOnly Property Price(ByVal Name As String, ByVal Size As String) As Double Get Dim connString As String = "Data Source=""C:\Documents and Settings\user\" & _ "My Documents\Visual Studio Projects\Quiznos Register\Assoc. Files\Quiznos.mdb"";" & _< "Jet OLEDB:Engine Type=5;" & _ "Provider=""Microsoft.Jet.OLEDB.4.0"";" & _ "persist security info=False;" Dim conn As New OleDbConnection(connString) Dim cmdGetPrice As New OleDbCommand("SELECT Products.Price FROM Products Products WHERE" & _ "(Products.Name='Classic Italian') AND (Products.Size='Small')", conn) Try If conn.State <> ConnectionState.Open Then conn.Open() End If Catch ex As OleDbException MsgBox(ae.Message) End Try Select Case Name Case "Classic Italian" Select Case Size Case "Small" Price = cmdGetPrice.ExecuteNonQuery '<----- This is where it gives me the error. 'other cases for other sizes End Select ' other cases End Select Return Price conn.Close() End Get End Property End Class
at the line "Price = cmdGetPrice.ExecuteNonQuery", i get an InvalidOperationException. what do i need to do to return this single value thanks ahead of time, stephen -
Hello again, I need some help with a simple OLEDB query. I need to return one value from an Access database. the code below is a snapshot of what i have so far.
Imports System.Data.OleDb Public Class Prices Public Shared ReadOnly Property Price(ByVal Name As String, ByVal Size As String) As Double Get Dim connString As String = "Data Source=""C:\Documents and Settings\user\" & _ "My Documents\Visual Studio Projects\Quiznos Register\Assoc. Files\Quiznos.mdb"";" & _< "Jet OLEDB:Engine Type=5;" & _ "Provider=""Microsoft.Jet.OLEDB.4.0"";" & _ "persist security info=False;" Dim conn As New OleDbConnection(connString) Dim cmdGetPrice As New OleDbCommand("SELECT Products.Price FROM Products Products WHERE" & _ "(Products.Name='Classic Italian') AND (Products.Size='Small')", conn) Try If conn.State <> ConnectionState.Open Then conn.Open() End If Catch ex As OleDbException MsgBox(ae.Message) End Try Select Case Name Case "Classic Italian" Select Case Size Case "Small" Price = cmdGetPrice.ExecuteNonQuery '<----- This is where it gives me the error. 'other cases for other sizes End Select ' other cases End Select Return Price conn.Close() End Get End Property End Class
at the line "Price = cmdGetPrice.ExecuteNonQuery", i get an InvalidOperationException. what do i need to do to return this single value thanks ahead of time, stephenHello! It seems that your query is returning more than one record.... Try out this query.. SELECT Price FROM Products WHERE (Name='Classic Italian') AND (Size='Small') This is coz no need to give alias when working with a single table. Also check the table 'Products' for rows having same 'Name' and'Size'. If it doesnt work......try to use a dataset to give value to varialble 'Price' _mubashir -- modified at 7:06 Friday 26th August, 2005