none
C# TableAdapter doesn't insert and update RRS feed

  • Question

  • I have a database called DB.mdf, in my program I use this code to insert a new row in this database:

    DBDataSet ds = new DBDataSet();
    DBDataSetTableAdapters.IPTableAdapter ipadap = new DBDataSetTableAdapters.IPTableAdapter();
    
    ipadap.InsertQuery(ip);

    InsertQuery is: INSERT INTO [IP] ([ID], [indirizzo]) VALUES (0, @indirizzo);

    The program executes all steps, but not inserts the row on database. Why?

    UPDATE Now I have tried this code:

    DBDataset.IPRow newRegionRow;
    newRegionRow = db.IP.NewIPRow();
    newRegionRow.ID = "6";
    newRegionRow.indirizzo = "NorthWestern";
    // Add the row to the Region table
    this.db.IP.Rows.Add(newRegionRow);
    // Save the new row to the database
    this.ipadap.Update(this.db.IP);

    And in this case not write a new row in database

    • Moved by Kristin Xie Tuesday, April 28, 2015 6:07 AM move to better forum
    Monday, April 27, 2015 2:14 PM

Answers

  • How did you confirm that the row wasn't inserted?  Is the DB in the output directory of your project?  If so then be aware that building your app will overwrite the changes with whatever is in your project.  You should either move the DB outside your output directory, use LocalDB instead or verify the changes before building again.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Fred BaoModerator Tuesday, April 28, 2015 6:06 AM
    • Marked as answer by Chianta Tuesday, April 28, 2015 8:24 PM
    Monday, April 27, 2015 3:21 PM
  • Hello Chianta,

    It seems that the data could be saved to be a copied database as what I and CoolDadTx thought, please have a try to set the Copy to Output Directory property of the .mdf file to Do not copy to see if you could see these inserted data in your database:https://msdn.microsoft.com/en-us/library/ms246989.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Chianta Tuesday, April 28, 2015 7:38 PM
    Tuesday, April 28, 2015 6:51 AM
    Moderator

All replies

  • How did you confirm that the row wasn't inserted?  Is the DB in the output directory of your project?  If so then be aware that building your app will overwrite the changes with whatever is in your project.  You should either move the DB outside your output directory, use LocalDB instead or verify the changes before building again.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Fred BaoModerator Tuesday, April 28, 2015 6:06 AM
    • Marked as answer by Chianta Tuesday, April 28, 2015 8:24 PM
    Monday, April 27, 2015 3:21 PM
  • The DB is LocalDB. If I open the DB with Server Explorer, the table is empty.

    I put this block

    DBDataset.IPRow newRegionRow; newRegionRow = db.IP.NewIPRow(); newRegionRow.ID = "6"; newRegionRow.indirizzo = "NorthWestern"; // Add the row to the Region table this.db.IP.Rows.Add(newRegionRow); // Save the new row to the database this.ipadap.Update(this.db.IP);

    in a try/catch constructor, and there aren't any errors. For example, I execute two times the same code. At the second time, I have an error on primary key on DB. But in DB there aren't any rows.  

    Thanks


    • Edited by Chianta Monday, April 27, 2015 3:31 PM
    Monday, April 27, 2015 3:31 PM
  • If you're using LocalDB then you need to be careful about which database it is viewing.  You need to be looking at the DB that matches your config string at runtime.  By default you are probably looking at the database that you are using at design time which won't have the inserted record. 
    Monday, April 27, 2015 4:01 PM
  • Hello Chianta,

    It seems that the data could be saved to be a copied database as what I and CoolDadTx thought, please have a try to set the Copy to Output Directory property of the .mdf file to Do not copy to see if you could see these inserted data in your database:https://msdn.microsoft.com/en-us/library/ms246989.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Chianta Tuesday, April 28, 2015 7:38 PM
    Tuesday, April 28, 2015 6:51 AM
    Moderator
  • Chianta,

    If you use a table adapter all the SQL is generic made for you. 

    It makes no sense to disturb that by changing that with own code.


    Success
    Cor


    Tuesday, April 28, 2015 8:29 AM
  • Hello Chianta,

    It seems that the data could be saved to be a copied database as what I and CoolDadTx thought, please have a try to set the Copy to Output Directory property of the .mdf file to Do not copy to see if you could see these inserted data in your database:https://msdn.microsoft.com/en-us/library/ms246989.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thanks!!!  I didn't know that Visual Studio, during develop, create a copy of database in /bin and it work with the copy of database.

    Thanks

    Tuesday, April 28, 2015 7:39 PM