Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (12 votes)
Hi!
I am new to .NET .
I have to get two column of data from a table on an SQL-Server. I have to add them up and display the result in an HTML table.

Note: I am not using a gridview!

Thank you!
Posted
Updated 16-Dec-10 0:46am
v6
Comments
Hiren solanki 16-Dec-10 7:27am    
Just bind datasource with returned query result to gridview. It will be rendered as a HTML table only.

using System.Text;//namesspace
using System.Data;

public void ConvertToHtml(DataTable dt)
{
StringBuilder sb=new StringBuilder();
sb.Append("<'table'>");
sb.Append("<body>");
sb.Append("<'tr'>");
sb.Append("<'td'>ColumnFirst<'td>ColumnSecond /td /tr>");
for(int i=0;i'<'dt.Rows.Count;i++)
{
sb.Append("<'tr><table><tbody><tr><td>"+dt.rows[i][ColumnFirst]+"/td /tr");
sb.Append("<'tr><table><tbody><tr><td>"+dt.rows[i][ColumnSecond]+"/td /tr");
}
sb.Append("</body>/table");
Div1.InnerHtml=sb.ToString();// add this to div runat="server" in aspx page
}
</td></tr></tbody></table></td></tr></tbody></table>


//Note Remove All ' occurrence.
//give proper closing HTML Tag.
 
Share this answer
 
v2
Comments
Hiren solanki 16-Dec-10 7:26am    
Good answer. But I think as he's using datagrid why to go with this?
justinonday 16-Dec-10 23:18pm    
This is not Datagrid .
i am trying to display table .
The 1st part of your answer has already been answered.
I will try to answer the 2nd part.

srinivasvempa wrote:
I have to add them up and display the result in an HTML table.


In your aspx page, add a label in the <td> part of the html table where you want to display the value. Initially set it's visibility to false.
After you get the values from the database, add them (that's easy).
Assign the value to the label and set it's visibility to true.

Let me know if I got your question wrong.
 
Share this answer
 
To select only 2 rows from sql server,
Select Top 2 from tablename

and add two rows in the sense what?
If columns datatype are decimal, integer, etc. then you can add in sql query only using Sum() operator.
 
Share this answer
 
v2
Comments
Toniyo Jackson 16-Dec-10 7:45am    
Question changed
This is actually a fairly ambiguous question. Do you mean that you have to get the sum of both columns individually? If so, you would just use
SQL
SELECT SUM(Column1) As SumOfColumn1, SUM(Column2) As SumOfColumn2 FROM MyTable WHERE SomeCondition
 
Share this answer
 
You could easily have found this using google.

select top 2 * from table



EDIT =========================

This was the original text which I responded to:

Hi I am new to .net i have to get two rows of data from sqlserver table and i have to add both rows how can i get rows from table . i dont need all table info

You COMPLETELY changed the question, and then voted my answer a 1.


EDIT #2 =========================

I hope you saw the original version of this because the last sentence still stands. It's really too bad the thought police have endorsed your action by deleting my original message.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900