1

I have this HTML table:

<table ID="tbl_ClientSearch" style="width: 83%;" runat="server">
   <thead>
      <tr>
         <td>Client Account Number</td>
         <td>Client Name</td>
      </tr>
   </thead>
   <tbody>
      <tr>                  
         <td><asp:Literal ID="CliNox" runat="server"></asp:Literal></td>      
         <td><asp:Literal ID="CliNamex" runat="server"></asp:Literal></td>            
      </tr>
   </tbody>
</table>

Then I have this query:

SqlCommand command = new SqlCommand(@"EXEC [dbo].[CTLC_SearchClient] '" + SearchVal + "';");
SqlConnection conn = new SqlConnection(connString);
conn.Open();
command.Connection = conn;
SqlDataReader readers = command.ExecuteReader();
if (readers.HasRows)
{
   while (readers.Read())
   {
      string CliNo = readers.GetString(0);
      string CliName = readers.GetString(1);
      CliNox.Text = string.Format("<a href='ClientInfo.aspx?ClientNumber={0}'>{0}</a>", CliNo.ToString());
      CliNamex.Text = CliName.ToString();
   }
   conn.Close();
}

This works but when I need to return multiple rows, this only returns 1 row since I only declared 1 for the table body. How can I solve this? Thank you in advance.

5
  • Have you checked your SP whether it returns more than one row or not? Commented Mar 17, 2017 at 4:46
  • @MAdeelKhalid andeel Khalid Yes. It works fine. But the my program only shows the last row since I think it overwrites the value on the html table row. Commented Mar 17, 2017 at 4:48
  • 1
    i.e is write your program should show only last row, you have coded that way i.e you have only one <tr></tr> and you are overwriting that row cells, for your requirement you need to create table structure dynamically or simply use Gridview or repeater control. Commented Mar 17, 2017 at 4:50
  • 1
    Use a repeater control: msdn.microsoft.com/en-us/library/zzx23804(v=vs.85).aspx . Also your SQL Command is wide open for SQL Injection attacks. Commented Mar 17, 2017 at 5:01
  • 1
    You can use, GridView, Repeater or asp:table to render mulitple rows in a table format. Refer here, stackoverflow.com/a/139308/405317 Commented Mar 17, 2017 at 5:05

3 Answers 3

1

In Aspx page add placeholder apart from table like below.

<asp:PlaceHolder ID = "tableClientSearch" runat="server"/>

In Aspx.cs file.

 StringBuilder html = new StringBuilder();
                html.Append("<table border = '1'><thead><tr><td>Client Account 

Number</td><td>Client Name</td></tr></thead>");

        SqlCommand command = new SqlCommand(@"EXEC [dbo].[CTLC_SearchClient] '" + SearchVal + "';");
        SqlConnection conn = new SqlConnection(connString);
        conn.Open();
        command.Connection = conn;
        SqlDataReader readers = command.ExecuteReader();

        if (readers.HasRows)
        {
            while (readers.Read())
            {
                html.Append("<tr>");
                html.Append("<td>");
                html.AppendFormat("<a href='ClientInfo.aspx?ClientNumber={0}'>{0}</a>", readers.GetString(0));
                html.Append("</td>");
                html.Append("<td>");
                html.Append(readers.GetString(1));
                html.Append("</td>");
                html.Append("</tr>");
            }
            conn.Close();
        }

        html.Append("</table>");
        tableClientSearch.Controls.Add(new Literal { Text = html.ToString() });
Sign up to request clarification or add additional context in comments.

4 Comments

This will work. However, it is not a particularly great way to do it. Using a databound control would be a better option.
I agreed, i.e right this is not the great way to do it, but when the requirement is just to render table without any other functionality which provides by the databound controls i would prefer simple html controls which will definitely gain some small amount of performance.
just one more favor. how can I do the pagination of the table here? :) let's say 10 records per page
that you can implement by two ways., 1. client side:- if the table data is not going larger size and 2.Server side :- if your are not sure about data and there might be a chances to huge number of rows in table. based on that you have to decide approach.
1

Delcare a protected table in aspx.cs file at class level

protected DataTable dt = new DataTable()

Fill this data table from sql query (by using DataAdapter), I guess you know how to do that, or just google it.

In aspx, replace

<tr>                  
    <td><asp:Literal ID="CliNox" runat="server"></asp:Literal></td>      
    <td><asp:Literal ID="CliNamex" runat="server"></asp:Literal></td>            
</tr>

by

foreach (DataRow dr in dt)
{
    <tr>                  
        <td><%= dr["columnName1"] %></td>    
        <td><%= dr["columnName2"] %></td>            
    </tr>
}

Comments

0

I am not familiar with asp.net, however this situation looks similar to some that could occur in php, in which case I would try generating the entire table in asp. something along the lines of,

for row in tables
    append to html <tr><td>data</td><td>moredata</td></tr>

insert html into <table></table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.