user2023203
user2023203

Reputation: 556

"Illegal characters in path" error when connecting to SQL Server

I'm trying to connect to database and I get the following error:

Illegal characters in path.

Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection Con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\targil3.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    SqlDataAdapter adapt = new SqlDataAdapter();
    adapt.InsertCommand = new SqlCommand(" INSERT INTO tblEmployee VALUES (@employeeNumber, @employeePrivateName, @employeeFamilyName ,@city, @street, @houseNo, @phoneNumber, @birthDate, @startWorkingDate)", Con);
    adapt.InsertCommand.Parameters.Add("@employeeNumber", SqlDbType.Char).Value = textBox1.Text;
    adapt.InsertCommand.Parameters.Add("@employeePrivateName", SqlDbType.VarChar).Value = textBox2.Text;
    adapt.InsertCommand.Parameters.Add("@employeeFamilyName", SqlDbType.VarChar).Value = textBox3.Text;
    adapt.InsertCommand.Parameters.Add("@city", SqlDbType.VarChar).Value = textBox4.Text;
    adapt.InsertCommand.Parameters.Add("@street", SqlDbType.VarChar).Value = textBox5.Text;
    adapt.InsertCommand.Parameters.Add("@houseNo", SqlDbType.Int).Value = textBox6.Text;
    adapt.InsertCommand.Parameters.Add("@phoneNumber", SqlDbType.Char).Value = textBox7.Text;
    adapt.InsertCommand.Parameters.Add("@birthDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);
    adapt.InsertCommand.Parameters.Add("@startWorkingDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);


    Con.Open();
    adapt.InsertCommand.ExecuteNonQuery();
    Con.Close();
}

How to do I connect to the database so the I can insert into it?

Upvotes: 2

Views: 5169

Answers (2)

Ehsan
Ehsan

Reputation: 32681

This is because you have got characters in your connection string which need to be escaped. To overcome this issue you have got couple of options.

Either to explicitly escape those characters by using

\\

OR

use the @ sign before assignment.

PS: Though i would recommend you to specify your connection string in app.config and read it like this

string conString = System.Configuration.ConfigurationManager.ConnectionStrings["yourConnectionString"].ToString();

and you should consider the user of

using (SqlConnection sqlConn = new SqlConnection(conString ))
{
   try
   {
        //your sql statements here
    }
   catch (InvalidOperationException)
    {

    }
    catch (SqlException)
    {

    }
    catch (ArgumentException)
    {

    }
 }

You won't face any errors specified above.

Upvotes: 0

Darren
Darren

Reputation: 70728

You'll need to escape \targil3.mdf

Use \\ or put an @ before the assignment of the string, for instance.

SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\targil3.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

Upvotes: 8

Related Questions