-1

I am trying this code to add new row to database, the new row is saved but when I close the program down, reopen it, the row has disappeared.

private void Dep_Add_Click(object sender, EventArgs e)
{
   try
   {
       SqlCommandBuilder cb = new SqlCommandBuilder(Dep_da);

       DataRow dRow = Dep_ds.Tables["Department"].NewRow();
       dRow[1] = Dep_Name.Text;
       Dep_ds.Tables["Department"].Rows.Add(dRow);

       Dep_MaxRows = Dep_MaxRows + 1;
       Dep_inc = Dep_MaxRows - 1;

       Dep_da.Update(Dep_ds,"Department");
   }
   catch (Exception exceptionObject)
   {
      MessageBox.Show(exceptionObject.Message);
   }

So I hope someone can help me

svick
  • 236,525
  • 50
  • 385
  • 514
AbdelMalek
  • 2,836
  • 2
  • 19
  • 12
  • You don't see any error messages or anything? – marc_s Jul 02 '11 at 20:14
  • No there is no any message appear – AbdelMalek Jul 02 '11 at 20:19
  • 2
    Look in your solution and your folders. Is this for an Attached Db? Those are copied (overwrite) to the bin folder before use. – H H Jul 02 '11 at 20:21
  • The message appear only when I add a repetitive record into a unique Column – AbdelMalek Jul 02 '11 at 20:23
  • The program retrieve all the record I inserted in sql server good but when I add any record in c # the record didn’t appear again when I restart the visual studio – AbdelMalek Jul 02 '11 at 20:27
  • 1
    OK 2 comments contradicting each other here: 1: "No there is no any message appear" and 2: "The message appear only ..." . So what is it please? Add stacktraces and exact error messages. – H H Jul 02 '11 at 22:50
  • Can you **plase show us** the connection string you're using for your database??? – marc_s Jul 03 '11 at 07:34

3 Answers3

1

You don't give much context information, so this is a bit of a guess...

If you are doing this from inside Visual Studio, and are writing to a local (attached) database, then every time you execute the program, the local db is being copied from your project to your bin folder, so you are starting each execution with the same copy of the database, overwriting the db that was modified in the last execution.

You should be able to test if this is the case by running your exe from explorer or the command line.

Jason
  • 86,222
  • 15
  • 131
  • 146
1

You already asked this question here, though with slightly different code.

I gave an answer for you on your previous question - which is to use the SqlDataAdapter.Update command. You're doing that here, but you didn't post the rest of your code so I don't know if you changed anything.

Community
  • 1
  • 1
Tim
  • 28,212
  • 8
  • 63
  • 76
0

You would probably call DataSet.Tables[0].AcceptChanges() method?

Deepak Kumar
  • 672
  • 4
  • 15
  • 31