|
-
Dec 17th, 2008, 03:41 PM
#1
Thread Starter
Lively Member
Data save twicw in the database
hi guys... i have a problem with my code...'
I dont get why using this code it save twice on the databse the intCategory item sace twice in the databse.
System.Data.SqlClient.SqlDataAdapter dacategs = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM tblICCategories", SqlConn);
System.Data.SqlClient.SqlCommandBuilder cbcategs = new System.Data.SqlClient.SqlCommandBuilder(dacategs);
System.Data.DataTable dtcategs = new System.Data.DataTable();
dacategs.Fill(dtcategs);
if (Item.Categories.Count > 0)
{
System.Data.SqlClient.SqlDataAdapter dacat = new System.Data.SqlClient.SqlDataAdapter("SELECT * FROM tblICInventoryCategory WHERE strProductID = '" + Item.Sku.Replace("'", "''") + "'", SqlConn);
System.Data.SqlClient.SqlCommandBuilder cbcat = new System.Data.SqlClient.SqlCommandBuilder(dacat);
System.Data.DataTable dtcat = new System.Data.DataTable();
dacat.Fill(dtcat);
dtcat.Clear();
dacat.Update(dtcat);
CommerceBuilder.Catalog.Category categ;
System.Data.DataRow rowcat;
foreach (int cat in Item.Categories)
{
categ = CommerceBuilder.Catalog.CategoryDataSource.Load(cat);
rowcat = dtcat.NewRow();
rowcat["strProductID"] = SizeDown(Item.Sku, 50);
rowcat["strGroup"] = "";
rowcat["intConcurrencyID"] = 0;
rowcat["intCategoryID"] = 0;
foreach (System.Data.DataRow rowc in dtcategs.Rows)
if (rowc["strCategory"].ToString().Equals(categ.Name))
{
rowcat["intCategoryID"] = rowc["intCategoryID"];
break;
}
dtcat.Rows.Add(rowcat);
}
dacat.Update(dtcat);
-
Dec 17th, 2008, 06:11 PM
#2
Re: Data save twicw in the database
1. Please wrap your code snippets in Code tags for readability.
2. Don't use string concatenation to build SQL statements. For various reasons, including not having to replace single quotes, use parameters. The Data Access link in my signature provides examples.
3. Debug your code. Place a break point at the top and then step through it line by line, seeing what happens at each step. You can also examine the contents of your DataTable just before calling Update.
-
Dec 17th, 2008, 06:38 PM
#3
PowerPoster
Re: Data save twicw in the database
in addition to jmc's comments, the reason your database is updating twice is because of you calling the Update method on the dataadapter, twice. one at the end of the code, and the other before the foreach loop.
in the foreach loop you are adding a row... then doing an update, so new data is being inserted depending on how many rows are being added into the datatable
-
Dec 17th, 2008, 06:48 PM
#4
Re: Data save twicw in the database
 Originally Posted by Techno
in addition to jmc's comments, the reason your database is updating twice is because of you calling the Update method on the dataadapter, twice.
There you go. I didn't even spot that. Just goes to show how much more difficult it is to read code that isn't formatted. The eyes just don't want to see what's in front of them.
Mind you, the DataTable is empty when the first Update call is made so I'm not sure that that's the problem. I'm not quite sure why you're calling Fill, then Clearing the DataTable you just Filled, then Updating an empty DataTable. Why populate the table only to clear it? Why update when there's not even any existing data in the table, never mind any new data? Are you simply trying to have the appropriate columns added to the DataTable automatically? If so then just call FillSchema.
Last edited by jmcilhinney; Dec 17th, 2008 at 06:51 PM.
-
Dec 17th, 2008, 06:49 PM
#5
PowerPoster
Re: Data save twicw in the database
true. i actually just woke up from sleeping... and my eyes are still a little hazy.
-
Dec 18th, 2008, 10:46 AM
#6
Thread Starter
Lively Member
Re: Data save twice in the database
Thanks guys,
I already figured out the cause of my error hihihi.....
But still I'm so thankful for the information that you have shared with me.
Promise i really appreciate it....
Thanks a lot guys.....
Keep up the good work....
You're The Man Guys.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|