Click here to Skip to main content
15,868,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code of my add data function
protected void addData(string name,string email,string field,string address,string notes,int phone,string site)
        {
            DateTime dt = DateTime.Today;
            SqlConnection con = new SqlConnection(@"Data Source=GAURAV-PC\SQLEXPRESS;AttachDbFilename=|DataDirectory|\JAMS.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            SqlCommand Cmd = con.CreateCommand();
            SqlTransaction trans = con.BeginTransaction();
            Cmd.Connection = con;
            Cmd.Transaction = trans;
            try
            {
                Cmd.CommandText = "Insert into JAMS_Companies(company,email,website,phone,address,field,notes) values(@name,@email,@site,@phone,@address,@field,@notes)";
                Cmd.Parameters.AddWithValue("@name", name);
                Cmd.Parameters.AddWithValue("@email", email);
                Cmd.Parameters.AddWithValue("@site", site);
                Cmd.Parameters.AddWithValue("@phone", phone);
                Cmd.Parameters.AddWithValue("@address", address);
                Cmd.Parameters.AddWithValue("@field", field);
                Cmd.Parameters.AddWithValue("@notes", notes);
                //comm.Parameters.AddWithValue("@dt", dt);
                Cmd.ExecuteNonQuery();
                trans.Commit();
                this.Close();
            }
            catch (Exception e)
            {
                Console.Write("Haha" + e.ToString());
                trans.Rollback();
            }
            finally
            {
                con.Close();
            }
        }


This adds the data temporarily i.e when I rerun the program It shows data in grid but when I check database then there is no data.Kindly Help
Posted
Updated 12-Feb-11 8:21am
v2

Do you have two databases? Could you be inserting to one but looking at the other?
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Feb-11 17:24pm    
Now that sounds strangely familiar to me. 5+
Development DB, Test DB, Staging DB, Production DB.
Who am I, and where exactly and WTF??? :))
joyrules 13-Feb-11 0:44am    
No I have only one Database.I feel that transaction is not committing properly for some reason.When I debug the program at the same time it shows data in grid bur when I see Databse there are no entries .

Also once I close VS2008 and re open it and debug the entries are not shown in the grid.I am building a WPF project.And using Windows 7 I am also running VS 2008 in administrator mode.So there should be no permissions problem too
joyrules 13-Feb-11 1:42am    
One more thing I have heard is that the most probable cause could be that I didn't set the appropriate TableAdapter on my Table Adapter Manager in order to save to data table.

In WinForms that is done automatically as you drag the table onto the designer, in WPF you must do it manually...

I am new to WPF kindly help

Now the taManager tries to save the data, but skips all tables for which it doesn't have a table adapter configured.
Estys 13-Feb-11 11:01am    
I think you are right in that he's not looking at the right DB. I've seen this problem many times now with regard to local (private) databases. The DB he designed in the project gets copied to the bin when running/debugging the app.
If an insert fails an error or exception should occur. You seem to be handling and displaying exceptions correctly.

So either the insert is succeeding (somewhere) or the code is never executing or some other code is deleting the inserted record.
 
Share this answer
 
You are running your app from within VS.
The database in your project serves as a kind of template for the runtime.
You should look in the bin-directory for your DB, not the one in your project for the data.

What the value of "Copy to output directory" property of the .mdf?
If it's "Copy always", every time you run your app the database in the bin will be overwritten by the one in your project.
If you set the property to "Copy if newer" it will be copied to the bin only when you change the design or add values.
There's also the "Do not Copy" option. The values you updated while debugging will be preserved always.

Cheers
 
Share this answer
 
v2

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