0

so I have a C# software which will save data to my database but every time I run my program and try to save data I get this message, please any help?

enter image description here

try
{
    SqlConnection cnn = new SqlConnection(@"Data  Source=.\SQLEXPRESS;
        AttachDbFilename=C:\Users\Hp\Documents\Visual Studio 2010\Projects\Bank_System\Bank_System\Bank_System.sdf;
        Integrated Security=True;User Instance=True");
    cnn.Open();
    SqlCommand cmd1 = 
       new SqlCommand("insert into user values('" + 
           textBox6.Text + "','" + textBox1.Text + "','" + textBox4.Text + "'," + 
           textBox3.Text + ",'" + textBox2.Text +  "','" + textBox5.Text + "')",
           cnn);
    SqlDataReader dr1 = cmd1.ExecuteReader();
    dr1.Close();
    MessageBox.Show(" Record inserted ", " information inserted");
    cnn.Close();
}
catch (SqlException ex)
{
    MessageBox.Show(ex.Message);
}
Yurii
  • 4,811
  • 7
  • 32
  • 41
  • Please show your code, perhaps I am wrong but I don't recognize this message as coming from the Framework code – Steve May 17 '14 at 08:08

2 Answers2

0

Copy your database Bank_System.sdf in \bin\debug\ folder and change your connection string like this :

SqlConnection cnn = new SqlConnection("Data Source=" +@".\SQLEXPRESS;
            AttachDbFilename=Bank_System.sdf;
            Integrated Security=True;User Instance=True");

It should be worked, if an error occurs, try to execute your app from yourapp.exe located in \bin\debug\ folder

aEk BKF
  • 131
  • 1
  • 2
  • 15
0

You are working with an SDF file. This file is for SQL Server Compact not for SQL Server Express (or full).

In this case the connection string should be simply:

 @"Data Source=<fullpath_and file_to_your_sdf_file>;Persist Security Info=False;"

Notice that in C# you need to add the verbatim character in front of strings that contains special characters like the backslash

Working with Sql Server Compact requires to install the libraries required from the Microsoft Downloads and to use the proper classes. So, remove the SqlConnection and the SqlCommand classes and use the SqlCeConnection and SqlCeCommand (and so on for the other data client classes used in you app).

Of course the SqlCeConnection class can understand this different connection string syntax and allow to work with the SDF file

Said that, please revise your code that builds the sql command. Using string concatenation like your code does is a secure recipe for errors. From parsing errors (quotes inside your strings will break the syntax) to more serious error like Sql Injections

This could be an approach using a parameterized query....

try
{
    string cmdText = "insert into user values(@p1, @p2, @p3,@p4,@p5,@p6)";
    using(SqlCeConnection cnn = new SqlCeConnection(@"Data Source=C:\Users\Hp\Documents\Visual Studio 2010\Projects\Bank_System\Bank_System\Bank_System.sdf;Integrated Security=True"))
    using(SqlCeCommand cmd1 = new SqlCeCommand(cmdText, cnn))
    {
        cnn.Open();
        cmd.Parameters.AddWithValue("@p1", textBox6.Text);
        cmd.Parameters.AddWithValue("@p2", textBox1.Text);
        cmd.Parameters.AddWithValue("@p3", textBox4.Text);
        cmd.Parameters.AddWithValue("@p4", textBox3.Text);
        cmd.Parameters.AddWithValue("@p5", textBox2.Text);
        cmd.Parameters.AddWithValue("@p6", textBox5.Text);
        cmd1.ExecuteNonQuery();
        MessageBox.Show(" Record inserted ", " information inserted");
     }
}
catch (SqlException ex)
{
    MessageBox.Show(ex.Message);
}
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • i tried it and start to give me "Error locating your server" – user3647102 May 17 '14 at 11:25
  • Did you change the classes from SqlConnection to SqlCeConnection? Have you checked if the path given to the connectionstring is correct and the SDF file is there? – Steve May 17 '14 at 12:07
  • why do i have to change it to SqlCeConnection ? – user3647102 May 17 '14 at 12:42
  • Sql Server Compact is a different kind of database. You need to use specific classes to work with it. [Sql Server vs Sql Server Compact](http://technet.microsoft.com/en-us/library/bb896140.aspx) – Steve May 17 '14 at 12:50