2

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?

7
  • 2
    Stop using AttachDbFileName. Attach the database to your Express instance, then connect using the logical database name instead of specifying a path. Commented Jul 29, 2013 at 15:26
  • Could you please tell me how to do that? Commented Jul 29, 2013 at 15:27
  • Hey, try Google, it's a fine searching engine! I found http://msdn.microsoft.com/en-us/library/ms190209(v=sql.105).aspx Commented Jul 29, 2013 at 15:29
  • Ozren Tkalčec Krznarić, I don't need to attach to sql server, I need to attach it to c# Commented Jul 29, 2013 at 15:31
  • You are confusing two different types of "attach"... Commented Jul 29, 2013 at 15:33

2 Answers 2

8

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");
Sign up to request clarification or add additional context in comments.

2 Comments

@user2023203 - this sounds like a new question, you'll have to make a new question stating what errors you get when attempting to update/insert into the database.
@user2023203 how are you verifying this? Going back to my very first comment, one of the problems with using this AttachDbFileName setting is that if you have two copies of your app, or your app and Visual Studio or Management Studio all use that same connection string, they're actually each creating their own copy of the database. So when you run an update in one app, the other app won't see those changes. So, again, STOP USING THIS "FEATURE." Attach your database properly.
0

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.

1 Comment

I agree with defining it in app.config, that way should it for some reason change, you only have to change it one place in code, as opposed to every place that it's called.

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.