Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Trying to display SQL data better

Gussy81

Coder
Hi, I am display all rows from my SQL table but it's all getting displayed in one label in a block. Can I break it down and assign a label for each table column? My code is below.
 
Last edited:
Solution
It's just a matter of enhancing your stringBuilder to include the HTML for a table, for example like this

ASP.net:
    stringBuilder.Append("<table>"); 
    foreach (DataRow row in dt2.Rows)
    {
        stringBuilder.Append("<tr>")
        stringBuilder.Append("<td>Client Ref: ").Append(row["client_ref"]).Append("</td>");
        stringBuilder.Append("<td>Crosk Ref: ").Append(row["crosk_ref"]).Append("</td>");
        stringBuilder.Append("<td>Client Name: ").Append(row["client_name"]).Append("</td>");
        stringBuilder.Append("<td>Section Name: ").Append(row["section_name"]).Append("</td>");
        stringBuilder.Append("</tr>")
        }
 
    stringBuilder.Append("</table>");

(I haven't tested this code, but I think...
Oh sorry about that. Here it is:

ASP.net:
if (dt2.Rows.Count > 0)
{
    StringBuilder stringBuilder = new StringBuilder();
    foreach (DataRow row in dt2.Rows)
    {
        stringBuilder.Append("Client Ref: ").Append(row["client_ref"]).Append("<br/>");
        stringBuilder.Append("Crosk Ref: ").Append(row["crosk_ref"]).Append("<br/>");
        stringBuilder.Append("Client Name: ").Append(row["client_name"]).Append("<br/>");
        stringBuilder.Append("Section Name: ").Append(row["section_name"]).Append("<br/><br/>");
    }
    CLIENTREF.Text = stringBuilder.ToString();
}
else
{
    CLIENTREF.Text = "No records found!";
}
 
It's just a matter of enhancing your stringBuilder to include the HTML for a table, for example like this

ASP.net:
    stringBuilder.Append("<table>");  
    foreach (DataRow row in dt2.Rows)
    {
        stringBuilder.Append("<tr>")
        stringBuilder.Append("<td>Client Ref: ").Append(row["client_ref"]).Append("</td>");
        stringBuilder.Append("<td>Crosk Ref: ").Append(row["crosk_ref"]).Append("</td>");
        stringBuilder.Append("<td>Client Name: ").Append(row["client_name"]).Append("</td>");
        stringBuilder.Append("<td>Section Name: ").Append(row["section_name"]).Append("</td>");
        stringBuilder.Append("</tr>")
        }
  
    stringBuilder.Append("</table>");

(I haven't tested this code, but I think it should be ok)
 
It's just a matter of enhancing your stringBuilder to include the HTML for a table, for example like this

ASP.net:
    stringBuilder.Append("<table>"); 
    foreach (DataRow row in dt2.Rows)
    {
        stringBuilder.Append("<tr>")
        stringBuilder.Append("<td>Client Ref: ").Append(row["client_ref"]).Append("</td>");
        stringBuilder.Append("<td>Crosk Ref: ").Append(row["crosk_ref"]).Append("</td>");
        stringBuilder.Append("<td>Client Name: ").Append(row["client_name"]).Append("</td>");
        stringBuilder.Append("<td>Section Name: ").Append(row["section_name"]).Append("</td>");
        stringBuilder.Append("</tr>")
        }
 
    stringBuilder.Append("</table>");

(I haven't tested this code, but I think it should be ok)
Beautiful!! thats just what i needed. thank you
 
Solution

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom