Solved

ExecuteNonQuery only updates one row in a dataset

Posted on 2009-04-10
9
791 Views
Last Modified: 2012-05-06
I have code that fills a dataset and then I want it to iterate through the rows of the dataset so I can fill in a field that was just added to the table.  When I iterate through the dataset, all of the values change as they are supposed to but in the end, it only updates the very first row in the dataset.  Any help would be appreciated.  Thanks
//

            //

            // Fill Dataset ad

            //

            //

            cmd.CommandText = @"SELECT CBSequenceNumber, CBNumber, CBAttempt, CBTimeZone FROM CallbacksVirtualQueueHistory WHERE CBNumber <> 'NULL' ORDER BY CBSequenceNumber";

            ad.SelectCommand = cmd;

            con.ConnectionString = str;

            cmd.Connection = con;

            con.Open();

            ad.Fill(dsMonitor);
 

            MonitorList.DataContext = dsMonitor.Tables[0].DefaultView;

            MonitorTxt.Text = dsMonitor.Tables[0].Rows.Count.ToString();
 

            //

            //

            // Set up the Update Command and Parameters

            //

            //

            cmd.CommandText = "UPDATE CallbacksVirtualQueueHistory SET CBTimeZone = @CBTimeZone WHERE (CBSequenceNumber = @CBSequenceNumber) AND (CBAttempt = @CBAttempt)";

            con.CreateCommand();

            SqlParameter paramTZ = new SqlParameter("@CBTimeZone", System.Data.SqlDbType.VarChar, 2);

            SqlParameter paramSN = new SqlParameter("@CBSequenceNumber", System.Data.SqlDbType.VarChar, 8);

            SqlParameter paramA = new SqlParameter("@CBAttempt", System.Data.SqlDbType.VarChar, 4);

            cmd.Parameters.Add(paramTZ);

            cmd.Parameters.Add(paramSN);

            cmd.Parameters.Add(paramA);
 
 

            int iCount = dsMonitor.Tables[0].Rows.Count;

            int i;

            //

            //

            // Iterate through dataset and Update records

            //

            //

            for (i = 0; i < iCount; i++)

            {

                string strTimeZone;

                string strNumber = dsMonitor.Tables[0].Rows[i].ItemArray[1].ToString();

                strNumber = strNumber.Substring(0, 3);

                strTimeZone = getTimeZone(strNumber);

                dsMonitor.Tables[0].Rows[i].ItemArray[3] = strTimeZone.ToString();
 

                paramTZ.Value = strTimeZone.ToString();

                paramSN.Value = dsMonitor.Tables[0].Rows[i].ItemArray[0];

                paramA.Value = dsMonitor.Tables[0].Rows[i].ItemArray[2];
 

                ad.UpdateCommand = cmd;

                ad.UpdateCommand.ExecuteNonQuery();

            }

            con.Close(); 

        }

Open in new window

0
Comment
Question by:belpepsi
  • 4
  • 4
9 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24117620
Did you try moving lines 50 and 51 outside of your loop (before line 53)?
0
 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24117623
Have you tested value of   iCount?

Check it first before loop

MessageBox.Show(iCount.ToString());
0
 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24117678
You have to change code:

   paramTZ.Value = strTimeZone.ToString();
                paramSN.Value = dsMonitor.Tables[0].Rows[i].ItemArray[0];
                paramA.Value = dsMonitor.Tables[0].Rows[i].ItemArray[2];
 
                ad.UpdateCommand = cmd;
                ad.UpdateCommand.ExecuteNonQuery();

to this:


                cmd.Parameters["@CBTimeZone"].Value = strTimeZone.ToString();
                cmd.Parameters["@CBSequenceNumber"].Value = dsMonitor.Tables[0].Rows[i].ItemArray[0];
                cmd.Parameters["@CBAttempt"].Value = dsMonitor.Tables[0].Rows[i].ItemArray[2];
 
               
                cmd.ExecuteNonQuery();
0
 
LVL 3

Author Comment

by:belpepsi
ID: 24117706
kaufmed - When I do that, no records are updated.

nkhelashvili - yes, iCount will show the 30000 records that are in the database
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Author Comment

by:belpepsi
ID: 24117740
nkhelashvili: - same result, only the first row is affected
0
 
LVL 7

Expert Comment

by:nkhelashvili
ID: 24117941
try to debug your program at the lines I told to change it.  Check if the values are different...
0
 
LVL 3

Author Comment

by:belpepsi
ID: 24117986
nkhelashvili: - the values change during each iteration of the for loop.  From that I see that I am iterating row by row through the dataset.  

CBSequenceNumber and CBAttempt are the primary key into this table.  (forgot to add that earlier)
0
 
LVL 7

Accepted Solution

by:
nkhelashvili earned 250 total points
ID: 24118088
Have you tested working of your application with sql profiler?   I suggest you to check it
0
 
LVL 3

Author Closing Comment

by:belpepsi
ID: 31568988
Thank you.  When I used the profiler, I noticed that my declarations for my parameters were of the wrong type.  Should have been like this:

SqlParameter paramTZ = new SqlParameter("@CBTimeZone", System.Data.SqlDbType.VarChar, 2);
            SqlParameter paramSN = new SqlParameter("@CBSequenceNumber", System.Data.SqlDbType.BigInt, 8);
            SqlParameter paramA = new SqlParameter("@CBAttempt", System.Data.SqlDbType.Int, 4);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now