SQLCommandBuilder is Dumb?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2006
Posts: 88
Reputation: blacklocist is an unknown quantity at this point 
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

SQLCommandBuilder is Dumb?

 
0
  #1
Jul 1st, 2006
Hi all,

I am having a wee bit trouble with with the SQL Command Builder. It does not build anything but frustration. From all the books I have read and everything this is how you go about updating changes from dataset or datatable without writing your own updating logic.

Build a sqlconnection

build a sqldata adapter

create a datatable or dataset

build a sqlcommandbuilder object

then have the sqldata adapter fill the dataset or table

make some changes to the dataset or table

use the sqldataadpater.Update method and boom changes should happen.

But if I debug and look at the sqldata adpater under Insert, Update, Delete they are null. Isn't the sqlcommand builder designed to build updating logic on the fly. It builds nothing it seems. What am I doing wrong or has anyone else have the same problem. I know the code works cause I have used it in my ASP.Net applications but won't work with windows app.

  1.  
  2. private void btnSql_Click(object sender, EventArgs e)
  3. {
  4.  
  5. //creat a dataset
  6. DataSet dsExcel = new DataSet("Excel");
  7.  
  8. //create a sql data adapter
  9. SqlDataAdapter sqlDA = new SqlDataAdapter("Select * from ExcelToSql", sqlConn);
  10.  
  11. //build commands for the damn data adpater so we can update
  12. SqlCommandBuilder sqlComm1 = new SqlCommandBuilder(sqlDA);
  13.  
  14. //fill the dataset, table called Excel
  15. sqlDA.Fill(dsExcel, "Excel");
  16.  
  17. //make changes to the dataset
  18. dsExcel.Clear();
  19.  
  20. //update please work!!!
  21. sqlDA.Update(dsExcel, "Excel");
  22.  
  23.  
  24.  
  25.  
  26. }//end btnSql_Click

I have even attached a print screen of my laptop in debug mode, look at the highlighted update part. It's null....
Click image for larger version

Name:	untitled.JPG
Views:	11
Size:	265.9 KB
ID:	2120

Would like to be put in the ritght direction.
Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: SQLCommandBuilder is Dumb?

 
0
  #2
Jul 2nd, 2006
The reason it is Null is this :

  1. dsExcel.Clear();
You are clearing the Dataset, i.e. making it empty, and then updating it. That is why it would read NULL

Example of SQLCommandBuilder

public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)
{
   SqlConnection myConn = new SqlConnection(myConnection);
   SqlDataAdapter myDataAdapter = new SqlDataAdapter();
   myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
   SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

   myConn.Open();

   DataSet ds = new DataSet();
   myDataAdapter.Fill(ds, myTableName);

   //code to modify data in DataSet here

   //Without the SqlCommandBuilder this line would fail
   myDataAdapter.Update(ds, myTableName);

   myConn.Close();

   return ds;
}
Hope this helps
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Quick reply to this message  
Join Date: Apr 2006
Posts: 88
Reputation: blacklocist is an unknown quantity at this point 
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

Re: SQLCommandBuilder is Dumb?

 
0
  #3
Jul 3rd, 2006
What is was actually trying to do is just make a fast change to the dataset so I can see the result in the sql server. Okay I was just to damn lazy to write code to add a row.

Well now I am back at work and looking at my ASP project that I mentioned works no problem. It will add the row and everyhing.

Click image for larger version

Name:	untitled.JPG
Views:	8
Size:	616.8 KB
ID:	2135


I was debugging and found the same things too, the sql adapter update was null. So my next question is where can you, if you can, find the update logic. Also why does this ASP application work and the windows app doesn"t??
Quick reply to this message  
Join Date: Apr 2006
Posts: 88
Reputation: blacklocist is an unknown quantity at this point 
Solved Threads: 2
blacklocist blacklocist is offline Offline
Junior Poster in Training

Re: SQLCommandBuilder is Dumb?

 
0
  #4
Jul 3rd, 2006
Okay well I found out that you was 100% right and sorry or not taking your reply more seriously. So I wiped out all my code and started clean and did not use the Dataset.Clear method. And I was like wow this code it taking some time then looked at my sql server and behold, there was my row.

Thanks Man!!
Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: SQLCommandBuilder is Dumb?

 
0
  #5
Jul 3rd, 2006
Originally Posted by blacklocist
Okay well I found out that you was 100% right and sorry or not taking your reply more seriously. So I wiped out all my code and started clean and did not use the Dataset.Clear method. And I was like wow this code it taking some time then looked at my sql server and behold, there was my row.

Thanks Man!!
No problem at all.

Glad I could help and you got it working!
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Quick reply to this message  
Closed Thread

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC