Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2014
    Posts
    146
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Connecting to db (again!)

    Hello

    In ASP.NET (Visual Studio 2013), I am trying to connect to an MS Access database, open it, insert simple form data when a 'Submit' button is clicked on that form, and then close the database.

    I have this little script:

    Code:
    Protected Sub CreateUser_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Using conn As New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("Provider=Microsoft.Jet.OLEDB.4.0;Data source="C:\myDatabase.mdb;").ConnectionString)
                Dim Sql As String = "INSERT INTO userlist (username,password, strEmail) VALUES (@username,@password, @strEmail)"
                Dim cmd As New OleDbCommand(Sql, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@username", username.Text)
                cmd.Parameters.AddWithValue("@password", password.Text)
                cmd.Parameters.AddWithValue("@strEmail", strEmail.Text)
                cmd.ExecuteNonQuery()
                conn.Close()
            End Using
        End Sub
    The error I am getting, which is
    BC30004: Character constant must contain exactly one character.
    relates to this line:

    Code:
    Using conn As New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("Provider=Microsoft.Jet.OLEDB.4.0;Data source="C:\myDatabase.mdb;").ConnectionString)
    I am not sure what the error message means, but in my aspx.vb file, the "Provider=Microsoft.Jet.OLEDB.4.0;Data source="C:\ is underlined in
    blue.

    Apart from that, does the logic and syntax of the script look fine?

    Thanks!

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I assume that connection string is in the config file. it should be something like
    Code:
    ......
    <connectionStrings>
       <add key="something" value="Provider=Microsoft.Jet.OLEDB.4.0;Data source="C:\myDatabase.mdb;">
    .....
    </connectionStrings>
    then something like this should work.
    Code:
    Using conn As New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("something").ToString())
    But I've been out of VB world for a bit now :P
    But the error is probably coming from an attempted escape on the "\"

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    Regular Coder
    Join Date
    Feb 2014
    Posts
    146
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Hello Alykins

    Thanks again for replying.

    Do you mean that I should have something like this in the aspx.vb file:

    Code:
    Sub Page_Load()
    
            Dim myMDBConnection As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\myDatabase.mdb;")
            myMDBConnection.Open()
            myMDBConnection.Close()
        End Sub
    AND

    Code:
    <connectionStrings>
       <add key="something" value="Provider=Microsoft.Jet.OLEDB.4.0;Data source="C:\myDatabase.mdb;">
    .....
    </connectionStrings>
    in the Web.config file?

    Cheers

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Quote Originally Posted by Blue1 View Post
    Hello Alykins

    Thanks again for replying.

    Do you mean that I should have something like this in the aspx.vb file:

    Code:
    Sub Page_Load()
    
            Dim myMDBConnection As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("something").ToString().Trim())
            myMDBConnection.Open()
            'do stuff here
            myMDBConnection.Close()
        End Sub
    AND

    Code:
    <connectionStrings>
       <add key="something" value="Provider=Microsoft.Jet.OLEDB.4.0;Data source="C:\myDatabase.mdb;">
    .....
    </connectionStrings>
    in the Web.config file?

    Cheers
    See in red

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    Blue1 (06-20-2014)

  • #5
    Regular Coder
    Join Date
    Feb 2014
    Posts
    146
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Hello alykins

    Thank you very much for that!

    I have some work still to do but at least now the main pieces of the jigsaw are in place - I have a picture of which aspects of script belong in particular files - and that is all down to you, so I am very grateful for all your invaluable guidance.

    Thank you very much indeed!

    Blue1

  • #6
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,758
    Thanks
    41
    Thanked 191 Times in 190 Posts
    np- practice practice practice :P

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •