Wrox Programmer Forums
|
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 1st, 2008, 01:25 PM
Registered User
 
Join Date: Dec 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataTable Update

Hello, I have been working on this for a while and it is driving me insane, so any help would really be appreciated.

I send in to the below method a DataTable that has been edited through a dataGridView. I then get the original DataTable, set the original DataTable to equal the changed Datatable and then update the new Table. The problem I have is the Parameters I set in the BuildCommands method are always null.

Thanks for the help


 public void updateDt(DataTable dt)
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            DataTable dtTempDataTable = new DataTable();
     string SqlStmt = "SELECT * testId, testField FROM TEST";

            try

           {

               string commandstring = SQLStmt)

               dataAdapter = new SqlDataAdapter(commandstring, DatabaseConnection);

               BuildCommands(dataAdapter);

               dataAdapter.Fill(dtTempDataTable);//fills with 4 rows
               dtTempDataTable = dt, //set new dt to dt with user changes
               dtTempDataTable.AcceptChanges();
               dataAdapter.Update(dtTempDataTable); //doesnt do a thing
            }
            catch { }
            finally
            {

            }

        }

 private void BuildCommands(SqlDataAdapter dataAdapter)
        {
            SqlConnection connection = (SqlConnection)dataAdapter.SelectCommand.Connectio n;
            SqlParameter workParam = null;

            string query = "Update TEST Set testField = @testField WHERE testid= @testid";

            dataAdapter.UpdateCommand = new SqlCommand(query, connection);


            workParam = dataAdapter.UpdateCommand.Parameters.Add("@testFie ld", SqlDbType.BigInt);
            workParam.SourceColumn = "testfield";


            workParam = dataAdapter.UpdateCommand.Parameters.Add("@testid" , SqlDbType.BigInt);
            workParam.SourceColumn = "testId";

 }


 
Old May 1st, 2008, 06:26 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

While I haven't worked with updated tables and the AcceptChanges method of the data adapter, I want to point out a basic problem with your code.

First you fill a data table variable:

               dataAdapter.Fill(dtTempDataTable);//fills with 4 rows

Then you do the table assignment:

               dtTempDataTable = dt, //set new dt to dt with user changes

This second line makes the prior line completely useless because you are overwriting the variable with the object that was passed into the method.

I think what you are trying to accomplish is a merge of the data. You are expecting that assignment to essentially merge the changed table into the loaded table in order to establish the rows that were changed. Unfortunately, this simply doesn't work that way.

From my understanding of the disconnected data model, you load a table, change the rows, then commit the changes using the AcceptChanges method. Without seeing the rest of the code in order to see how you are modifying the row values, etc, I can't provide you with more information.

The important point I'm trying to make is that the table variable assignment instruction is negating the previous loading of the table. Unless the table passed into the method (dt) contains rows that are in the 'modified' state, the AcceptChanges() method won't do anything.

I can't say for sure what the problem is with the parameters. Are the parameters null or are the parameter values null?

-Peter
peterlanoie.blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Datatable Limno .NET Framework 1.x 0 May 28th, 2008 02:37 PM
DataTable and Database collie ASP.NET 1.x and 2.0 Application Design 3 August 7th, 2007 11:56 AM
Problem with DataTable ayazhoda ASP.NET 1.x and 2.0 Application Design 1 April 8th, 2007 10:12 PM
Datagrid + datatable edukulla C# 2005 2 September 11th, 2006 04:53 PM
how to use DataTable.Select Salte C# 2 December 22nd, 2004 04:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.