Trouble with database connections

Hello all,
I'm trying to get some data from a table in an Access database, and my program is behaving weirdly (at least i think so).

Ok, so for now, i want to run this query:

Dim query As String = MonthCalendar1.SelectionStart ' + " " + ComboBox1.Text + ComboBox2.Text + " " + ComboBox3.Text
Dim myQuery1 As String = "SELECT [BID]FROM Table1 WHERE [Date]=" + query + ""
Dim myConn1 As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Documents and settings\dbase.mdb")
Dim myCommand1 As OleDbCommand = New OleDbCommand(myQuery1, myConn1)
myConn1.Open()
Dim myReader1 As OleDbDataReader = myCommand1.ExecuteReader(CommandBehavior.CloseConnection)
myReader1.Read()
Dim robert As String = myReader1.ToString(0)
MsgBox(robert)
** end code

so, when i run that program, it returns the letter S, when it should be returning the word Duke.

What am I doing wrong

Any help is greatly appreciated! thanks in advance.
-Robert



Answer this question

Trouble with database connections

  • bergeron

    as in:
    Dim myQuery1 As String = "SELECT [BID]FROM Table1 WHERE [Date]='" + query + "'"
    or
    Dim myQuery1 As String = "SELECT [BID]FROM Table1 WHERE [Date]=#" + query + "#"


    because both ways, the messagebox is only showing "S"


  • NickNotYet

    hmm, why isn't it even reading I'm sorry, but I completely don't understand what's going on.

    Sorry for being such a nube
    -Robert


  • CrazyEye

    it's throwing the exception:
    Unhandles exception has occured in your application. If you click Continue, the application will ignore this error and attempt to continue. if you quit, the application will close immediately.

    See the end of this message for details on invoking
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************

    System.InvalidOperationException: No data exists for the row/column.

    at System.Data.OleDb.OleDbDataReader.DoValueCheck(Int32 ordinal)

    at System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)

    at mcmorrow_chooser.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Robert \Local Settings\Application Data\Temporary Projects\mcmorrow chooser\Form1.vb:line 17

    at System.Windows.Forms.Control.OnClick(EventArgs e)

    at System.Windows.Forms.Button.OnClick(EventArgs e)

    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

    at System.Windows.Forms.Control.WndProc(Message& m)

    at System.Windows.Forms.ButtonBase.WndProc(Message& m)

    at System.Windows.Forms.Button.WndProc(Message& m)

    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    it goes on, but i think that's pretty much the most important part


  • Quinn01

    In addition to Ken's suggestion you should also get the string from the field of the reader not the reader itself...such as:

    MyReader(0).ToString

    or

    Cstr(MyReader(0))



  • sofakng

    Robert3234 wrote:
    as in:
    Dim myQuery1 As String = "SELECT [BID]FROM Table1 WHERE [Date]='" + query + "'"
    or
    Dim myQuery1 As String = "SELECT [BID]FROM Table1 WHERE [Date]=#" + query + "#"


    because both ways, the messagebox is only showing "S"

    NO.....From Your query string you need to include pound signs like this:

    Dim query As String = "#" + MonthCalendar1.SelectionStart + "# "...



  • russ_mac

    OK, sorry to be consitsantly and persitatantly annoying, but debut exposes doubt, and as I tried out another string...

    when i try to use this string

    Dim query As String = MonthCalendar1.SelectionStart

    Dim time As String = ComboBox1.Text + ComboBox2.Text + " " + ComboBox3.Text
    MsgBox(time)
    Dim myQuery1 As String = "SELECT [Renter]FROM Table1 WHERE [Date]=#" + MonthCalendar1.SelectionStart + "# AND [Time]=#" + time + "#"

    and i try to use
    MsgBox(CStr(myReader1(0))) to display it, the program throws an exception at the messagebox line!

    it should be returning a string, but instead i get an exception.

    sorry for being such a dweeb / annoyance / noob.

    -Robert


  • hye_heena

    here's my code, by the way

    Dim query As String = MonthCalendar1.SelectionStart

    Dim time As String = ComboBox1.Text + ComboBox2.Text + " " + ComboBox3.Text
    MsgBox(time)
    Dim myQuery1 As String = "SELECT [Renter]FROM Table1 WHERE [Date]=#" + MonthCalendar1.SelectionStart + "# AND [Time]=#" + time + "#"
    Dim myConn1 As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Robert\Desktop\Mcmorrow.mdb")
    Dim myCommand1 As OleDbCommand = New OleDbCommand(myQuery1, myConn1)
    myConn1.Open()
    Dim myReader1 As OleDbDataReader = myCommand1.ExecuteReader(CommandBehavior.CloseConnection)
    myReader1.Read()
    'MsgBox(CStr(myReader1(0)))
    If myReader1.Read Then
    MsgBox(myReader1.GetString(0).ToString)
    End If


  • George Waters

    In a query you should enclose dates in #


  • danni123

    WHat exception is being thrown Somehting about using the reader when the connection is closed maybe...also what is the datatype of the field that you are quering IS it date, time, datetime

  • Kevin Kelly101

    you are not getting any databack that is why you are getting the error.

    if myreader1.read then
    dim robert as .........
    end if



  • Trouble with database connections