Skip to content Skip to sidebar Skip to footer

Standard Html Table With Total From Sql Server Table

Refer to this link: SQLDataReader GetDateTime Format The result should be: ID Number01 TheDate Number02 ---------------------------------------- 1 10 01/

Solution 1:

you can use this html

<tfoot>
    <tr>
      <td>Tot</td>
      <td>60</td>
      <td></td>
      <td>30</td>
    </tr>
    <tr>
       <td>Avg</td>
       <td>20</td>
       <td></td>
       <td>10</td>
    </tr>
  </tfoot>

this add two lines at the end of the table.

to calculate the total and avarage in the definitions

int totnum1 = 0;
decimal totnum2 = 0;
int numRow = 0;
decimal avg1 = 0;
decimal avg2 = 0;

in the loop

totnum1 += reader.GetInt32(1);
totnum2 += reader.GetInt32(3);
numRow ++;

at the end of loop

avg1 = totnum1 / numRow;avg2 = totnum2 / numRow;

you could write the html as in the last question using totnum1, totnum2 avg1 and avg2 in place of the number in the example above

public string getWhileLoopData() 
{
 string htmlStr = "";
 SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
 SqlCommand thisCommand = thisConnection.CreateCommand();
 thisCommand.CommandText = "SELECT * FROM MyTable WHERE TheDate = @TheDate";
 thisCommand.Parameters.AddWithValue("@TheDate", txtDate.Text);


int totnum1 = 0;
decimal totnum2 = 0;
int numRow = 0;
decimal avg1 = 0;
decimal avg2 = 0;



 thisConnection.Open();
 SqlDataReader reader = thisCommand.ExecuteReader();

 while (reader.Read()) {
     int id = reader.GetInt32(0);

     int Number01 = reader.GetInt32(1);
     DateTime TheDate = reader.GetDateTime(2);
     Decimal Number02 = reader.GetDecimal(3);

     totnum1 += reader.GetInt32(1);
     totnum2 += reader.GetInt32(3);
     numRow ++;

     //string Pass = reader.GetString(2);
     htmlStr += "<tr><td>" + id + "</td><td>" + Number01 + "</td><td>" + TheDate + "</td><td>" + Number02 + "</td></tr>";
 }

 thisConnection.Close();

avg1 = totnum1 / numRow;
avg2 = totnum2 / numRow;

htmlStr += string.Format("<tfoot><tr><td>Tot</td><td>{0}</td><td></td><td>{1}</td></tr>", totnum1 , totnum2 );
htmlStr += string.Format("<tfoot><tr><td>Avg</td><td>{0}</td><td></td><td>{1}</td></tr></tfoot>", avg1 , avg2 );


 return htmlStr;
}

Post a Comment for "Standard Html Table With Total From Sql Server Table"