943,580 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 8356
  • C# RSS
Jul 1st, 2006
0

SQLCommandBuilder is Dumb?

Expand Post »
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.

C# Syntax (Toggle Plain Text)
  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:	135
Size:	265.9 KB
ID:	2120


Would like to be put in the ritght direction.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006
Jul 2nd, 2006
0

Re: SQLCommandBuilder is Dumb?

The reason it is Null is this :

C# Syntax (Toggle Plain Text)
  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
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
Jul 3rd, 2006
0

Re: SQLCommandBuilder is Dumb?

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:	105
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??
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006
Jul 3rd, 2006
0

Re: SQLCommandBuilder is Dumb?

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!!
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
blacklocist is offline Offline
87 posts
since Apr 2006
Jul 3rd, 2006
0

Re: SQLCommandBuilder is Dumb?

Quote 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!
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: Problem in displaying returned value from function
Next Thread in C# Forum Timeline: Editing Sql Row





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC