Problem with connecting to a MS Access database (.mdb)

Last post 03-31-2008 8:51 AM by kaushalparik27. 3 replies.

Sort Posts:

  • Problem with connecting to a MS Access database (.mdb)

    03-31-2008, 6:33 AM
    • Member
      1 point Member
    • ekb
    • Member since 03-31-2008, 10:19 AM
    • Posts 2

     Here is my code:

    1    Dim strSQL As String
    2    strSQL = "SELECT * From [user] WHERE username='" & user.Text & "' AND password = '" & password.Text & "'"
    3    
    4    Dim MemberDataConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & Server.MapPath("App_Data/tournament.mdb"))
    5    
    6    Dim MemberCommand As New OleDbCommand(strSQL, MemberDataConn)
    7    MemberDataConn.Open()
    8    Dim MemberDBReader As OleDbDataReader
    9    
    10   MemberDBReader = MemberCommand.ExecuteReader()
    11   If MemberDBReader.Read() Then
    12   	Session("UserName") = user.Text
    13   Else
    14   	ErrorLabel.Text = "Sorry no matching user name and password is found."
    15   	Session("UserName") = ""
    16   End If
    17   MemberDBReader.Close()
    18   MemberDataConn.Close()
    19   
    20   If Session("UserName") <> "" Then Server.Transfer("youare.aspx")
    

    The problem is at the second line. If I remove "[ ]" from the table name (user), my code just doesn't work. I looked up at different sources, they do not have the "[ ]" around their table names. Why my code needs the "[ ]" then? and what differences does it make to have or not to have "[ ]" ?

    thank you for your helps

  • Re: Problem with connecting to a MS Access database (.mdb)

    03-31-2008, 7:44 AM
    Answer

    ekb:
    strSQL = "SELECT * From [user] WHERE username='" & user.Text & "' AND password = '" & password.Text & "'"
     

    that is because "user" would be a keyword / reserve word in MS Access... just like Sql Server; so if we use any reserve words in our table as tablename or fieldname then it is required to be enclosed in "[ ]"..

    hope it helps./. 

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: Problem with connecting to a MS Access database (.mdb)

    03-31-2008, 8:41 AM
    • Member
      1 point Member
    • ekb
    • Member since 03-31-2008, 10:19 AM
    • Posts 2

    thank you for your helps. Big Smile

  • Re: Problem with connecting to a MS Access database (.mdb)

    03-31-2008, 8:51 AM

    ekb:
    thank you for your helps.

    pleasure to help you.. and dont forget to mark the answer and resolve the thread.. Thanx./. 

    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
Page 1 of 1 (4 items)