Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi !
In my VS 2010 WFA, whenever I insert data to my Service Based Database(SQL Server 2008) data are saving successfully but after closing the application(VS 2010) such data are getting deleted, this problem is happening with every form of the application.
I'm unable to understand and solve this. So please help me.
My connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Product.mdf;Integrated Security=True;User Instance=True"

What I have tried:

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Product.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd = new SqlCommand("insert into Login_User values('"+textBox1.Text+"','"+textBox2.Text+"') ",con);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Succesfully added.");
            textBox2.Text = "";
            textBox1.Text = "";
            con.Close();
Posted
Updated 29-Aug-21 6:36am
v2

1 solution

Based on your connection string you're attaching the database. Chances are that the database which you're attaching is included in your project and being copied from your project output directory every time you start the application. This means that every change you have done when the program was running gets overwritten by the initial copy when the program starts next time.

Try removing the database from your project and moving it to a static location from where you attach the database.

Another observation, at the moment you concatenate values directly to the SQL statement. This leaves you wide open to SQL injection and other problems. Instead you should use bind variables. Have a look at Properly executing database operations[^]

Third observation (or actually a guess). Are you perhaps storing user name and password. Storing password as plain text should never be done. Have a look at Password Storage: How to do it.[^]
 
Share this answer
 
Comments
Md M Ahmad 29-Aug-21 13:41pm    
My database exists in two place in my project-1.> D:\Cricket\Product Entry\Product Entry
2.> D:\Cricket\Product Entry\Product Entry\bin\Debug.
What should I do now?
Which is Static Location?
Wendelius 29-Aug-21 13:57pm    
As said, you probably have the database in your project and that's why it's also in \bin\Debug directory. Try excluding it from the project and use the path D:\Cricket\Product Entry\Product Entry in your connection string.
Md M Ahmad 30-Aug-21 1:17am    
I removed the database from \bin\debug directory and changed connectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Product.mdf;Integrated Security=True;User Instance=True" to @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Cricket\Product Entry\Product Entry\Product.mdf;Integrated Security=True;User Instance=True", even after the database is adding in \bin\debug directory automatically.
Wendelius 30-Aug-21 1:40am    
Don't remove it from the directory, remove it from your project. This ensures that the database is not copied each time the project is built.

If needed, you can copy the database to a whole different location and use that location in your connections string. This could clarify the situation.
Md M Ahmad 30-Aug-21 2:29am    
Sir, I am a student and trying to learn.
I appreciate your precious time, what are you saying to do me I am totally unable to understand. Can you please give me example or connect with my PC remotely through teamviewer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900