Hi
I am trying to count the rows from a table at the same time I want to update the new column I m trying the following code but not getting results please help me....

string str1 = "SELECT Extrausage FROM perclientreport WHERE Bill>0"; 

cmd = new OleDbCommand(str1, con); 

con.Open();

cmd.ExecuteNonQuery();

 

for (int i = 0; i < (dt.Rows.Count); i++) 

{

double ext = Convert.ToDouble(dt.Rows[i]); 

double bill = (ext * 1024); 

bill = (bill / 100);

string str2 = "UPDATE perclientreport SET Bill=" + bill; 

cmd = new OleDbCommand(str2, con); 

con.Open();

cmd.ExecuteNonQuery();

}








catch (Exception ex) 

{

MessageBox.Show(ex.Message); 

}

finally



{

con.Close();

}

Edited 5 Years Ago by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

Does your table get filled up?

If not, please try to change your query select string with this one:

string query = "SELECT Extrausage FROM perclientreport WHERE Bill > '" + 0 + "'";

and about updating:

double ext = Convert.ToDouble(dt.Rows[i]);
            ext = (ext * 1024) / 100;
            string str2 = "UPDATE perclientreport SET Bill = '" + ext + "'";

Hope it helps,
Mitja

Edited 5 Years Ago by Mitja Bonca: n/a

My problem is I m not getting values from each row for one column I m using the following code nd getting one exception at the underlined code

 for (int i = 0; i < (dt.Rows.Count); i++)
                {

                    double ext = Convert.ToDouble(dt.Rows[i]);
                    double bill = (ext * 1024);
                    bill = (bill / 100);

The exception is
"Unable to cast object of type 'System.Data.DataRow' to type 'System.IConvertible'."
Plz help me
Thank you!

Edited 3 Years Ago by Nick Evan: Fixed formatting

This logic is better placed in a stored procedure that can be called from the application. There is no reason to loop through a result set and send a sql command across the network for each individual update.

UPDATE perclientreport
SET Bill = (Extrausage * 1024)/100
WHERE Bill > 0

Edited 5 Years Ago by buddylee17: n/a

You should write your for loop like this:

for (int i = 0; i < (dt.Rows.Count)[B] - 1[/B]; i++)  //Since index is zero based.
 double ext = Convert.ToDouble(dt.Rows[i][B].Item(0)[/B]);   //Value of first cell will be provided to convert

Edited 5 Years Ago by ShahanDev: n/a

This article has been dead for over six months. Start a new discussion instead.