I don't know how to solve this problem.

I want to create simple project in visual studio 08 in C# windows application which can save update delete data from database.

here are the steps i use :

open new project
right click on solution explorer -add- add new items-service-based-database when i select this option it will create new database with dataset.

now the problem is how can i save the data in this database. I tried this code but it shows save data in run time when i stop and run the program data i entered not save in the database.

code :
try
{

SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\DB\\emp.mdf;Integrated Security=True;User Instance=True";
            cn.Open();

          SqlCommand cmd = new SqlCommand("insert into emp(id,nm,city,dept)values(@id,@nm,@city,@dept)", cn);
                cmd.Parameters.AddWithValue("@id", textBox1.Text);
                cmd.Parameters.AddWithValue("@nm", textBox2.Text);
                cmd.Parameters.AddWithValue("@city", textBox3.Text);
                cmd.Parameters.AddWithValue("@dept", textBox4.Text);
                cmd.ExecuteNonQuery();

                MessageBox.Show("Update successful");
            }
 catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 finally
            {
                cn.Close();
            }

how can i save the data in database.

I hope u can understand the problem.

Recommended Answers

All 12 Replies

When you run Visual Studio in Debug mode it makes a copy of the database (if it's a file based one) and uses that to do the adds, updates, etc. This is so you can always start from the original database to figure out what is wrong. Look in the project folders, you should see a copy of the DB file in there under the debug directory, IIRC.

The code looks fine, the only problem is, that the ID should not be let to be insterted by the user (from textBox). It has to be created some how automatically, so starting from 1,2,3, and so on (each new id is +1, incrementally).
I would suggest you to get the last inserted id, and increment it, so you get a new one. Then insert other data as well.

Thnx for reply...

can u explain me this process. bcoz i have no idea about this.

when i add database it save in the bin-debug-empdatabase.
is this is a proper path.

I didn't find the IIRC.

and id is auto increment.

IIRC = If I Recall Correctly, it's not something you'll find. You should see two database files in the debug directory, just like you see two exe files there. It will be something like Databasename.vhost.dbf (this is just an example, yours will be different).

Ok...
I find the database. but it not work when i attach it in sql server. it shows the error message in vs 08
Error 1 Unable to copy file "D:\tryset\WindowsFormsApplication1\WindowsFormsApplication1\DB\employee.mdf" to "bin\Debug\DB\employee.mdf". The process cannot access the file 'bin\Debug\DB\employee.mdf' because it is being used by another process. WindowsFormsApplication1

and also it overwrite the last save record again and again.
I mean it not create new row for new record.

can i have to make some changes in code to save the data. is this code is correct.

VS is probably overwriting your DB every time you compile and test your program.
Make sure that the Copy to Output Directory property for your database file is set to Copy if newer. Then it will only copy when you change the table structures.

If you are using VS to connect to this DB (in Debug folder) to inspect the content then you will only be able to do this when the program is not running.
Also, remember to disconnect the DB in VS before debugging or your App will probably find it cannot access the DB.

but how to save data in this database bcoz when i stop the program and run it again i lost the previous data.

the code i use to save data is correct or not.

it always perform temporary saving. how can i save data permanent in DB.

Did you read my post?
What is Copy to Output Directory set to for your .dbf file?

yes i read the post. thanx for u r reply. program runs properly. but now i have the above problem. with save data.

Click on your <DBname>.mdf (...\DB\emp.mdf) file in your project.
Look in the Properties view.
What is the value for the property "Copy to Output Directory"?

If it is set to "Copy always" then every time you build your project the DB file in the Debug folder is overwritten!
This is probably why your data is apparently not saved between runs.

hey its working..

Thank u so much...

Thanxs a lot.......

can i ask u for one more help please.

after installation of this program on another pc where do i find the database. the setup install properly its working.

when i deploy the project it will create these files

emp.mdf.deploy
emp_log.ldf.deploy
emp
emp.exe.config.deploy
emp.exe.deploy
emp.exe.manifest

the mdf file come with the deployment setup.

but i want to check the data properly save in the database how can i check this.

can i attach this mdf file to the sql server before installation. but how it will take a the path for database connection.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.