1

Error: Unrecognised escape sequence.

Code:

    string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DriveBuy.accdb";
    OleDbConnection con = new OleDbConnection(ConnectionString);


    if (FileUpload1.HasFile) 
    {
        String sPath = MapPath(FileUpload1.FileName);
        FileUpload1.PostedFile.SaveAs(sPath); 

        con.Open();
        string mysql; 
        mysql = "INSERT INTO Cars(Make,Model,Price,Picture) VALUES (?,?,?,?)";
        OleDbCommand cmd = new OleDbCommand(mysql, con);
        cmd.Parameters.AddWithValue("@p1", tbMake.Text);
        cmd.Parameters.AddWithValue("@p2", tbModel.Text);
        cmd.Parameters.AddWithValue("@p3", Convert.ToDecimal(tbPrice.Text));
        cmd.Parameters.AddWithValue("@p4", FileUpload1.FileName);
        cmd.ExecuteNonQuery();
        con.Close();
    }
    else
    {
        lblError.Text = "Image was not uploaded";
    }

Any ideas? I'm simply trying to input the data into a access database. The error appears on the connection string, the "\"

Cheers

1
  • It might be from ConnectionString = "" and VALUES (?,?,?,?). Commented May 9, 2013 at 13:55

2 Answers 2

1

You're passing an empty connection string

 string ConnectionString = "";

In order to connect to your database you need to specify a valid connection string. This site might be helpful.

EDIT

Data directory is supposed to be the directory of your access db

// fix your data source to the correct directory
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\DriveBuy.accdb";

EDIT2

Sorry your parameters are in fact correct as pointed in the comments by D Stanley, thanks.

Sign up to request clarification or add additional context in comments.

5 Comments

I forgot to put in my connection string, sorry. Updated still errors.
OleDb provider does not support named parameters. VALUES (?,?,?,?) should be correct.
Ooops you're right. Thanks for pointing it out. Also I hate how every provider uses parameters differently. It caused a lot of bloatware SQL stuff been written over the years.
Worked, thank you. Only problem is I'll have to edit the directory when moving between computers. Ah well it works.
Glad it did, ideally you should move the connection string in your config and change it there once your in another computer. Should be easier than recompiling your code.
0

Instead of:

string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DriveBuy.accdb";

Do this:

string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\DriveBuy.accdb";

or this:

string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DriveBuy.accdb";

The error is coming because in C# "\" is an escape sequence used for special meanings in the string. For eg, \n means newline etc. So we've to use double backslash so that compiler donot treat back slash as an escape sequence.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.