1
Answer

How to Loop and Update SQL Table Summing any existing Values

i thnak you all for the great support you gave me as it is i still need your support again.
i want to retrive records from a tableA and update tableB i need the value in tableB to sum if it find any value in the values field; i have tried using the code below :
   
    DataTable dt = GetDatafromDatabase(); //===== returns a DataTable
    string SQLT = "SELECT * FROM tbl_TempReport";
    string colname;
    if (cn.State == ConnectionState.Closed)
    {
     cn.Open();
    }
    SqlCommand cmdT = new SqlCommand(SQLT, cn);
    SqlDataReader rt = cmdT.ExecuteReader();
    while (rt.Read())
    {
// For each row, print the values of each column.
    foreach (DataColumn column in dt.Columns)
    {
    foreach (DataRow row in dt.Rows)
    {
    colname = column.ColumnName.ToString();
    int mValue = 0;
    string m_Code = rt["code"].ToString();
    if (m_Code == colname)
    {
     if (m_Code == colname && mValue > 0) { mValue +=        Convert.ToInt32(row[column].ToString()); } else { mValue =     Convert.ToInt32(row[column].ToString()); }
//============ insert into tbl_TempReport to match the appropriate column
    string SQLP = "UPDATE tbl_TempReport SET VALUEP = @VALUEP WHERE code =     @code";
    SqlCommand cmdp = new SqlCommand(SQLP, cn);
    cmdp.Parameters.AddWithValue("@VALUEP", SqlDbType.Int).Value = mValue;
    cmdp.Parameters.AddWithValue("@code", SqlDbType.NVarChar).Value = rt["code"].ToString();
    cmdp.ExecuteNonQuery();
    }
    }
    }
    } 
 
Please i appreciate any help. Thank you all 
Answers (1)