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();

}

Recommended Answers

All 4 Replies

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

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!

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.