SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How do I display SQL query data in tabular format in HTML?

I have a SQL query which retrieves two columns from a table based on a 'location'.

The following table:

content_id     content_title
234            Fink, James
90             Merylou, Jane
45             Marcy, Kim
112            Bower, John
34             Alset, Mike

was generated by the following query:

DECLARE @strLocation varchar(200)
SET @strLocation = 'Ridge'

SELECT [content_id], [content_title]
FROM [content]
WHERE [folder_id] = '188'
AND (content_html LIKE '%'+@strLocation+'%')

The query looks into the content_html column and looks for the variable.

My HTML looks like this:

<input type=text size=50 id="txtPhysByLoc" runat="server" /><input type=button value="Go" />
<br />
<div>
    <table border=0>
        <span id="writeTable"></span>
    </table>
</div>

My C# code-behind so far looks like this:

protected void Page_Load(object sender, EventArgs e)
{    
   string cString = "Provider=sqloledb;Data Source=myServer;Initial Catalog=Dbase;User Id=efv;Password=st@tl;";
   SqlConnection Conn = new SqlConnection(ConnectionString);
   Conn.Open();
}

How can I use the query along with my C# code to generate a table? Or use another ASP.net control to show the tabular data?

Upvotes: 0

Views: 1152

Answers (1)

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11910

   <asp:SqlDataSource runat="server" ID="sdsContent" ConnectionString="<%$ connectionStrings.connectionString %>" SelectCommandType="text" SelectCommand="your query here">
   </asp:SqlDataSource>
   <asp:Repeater runat="server" ID="rptContent" DataSourceID="sdsContent">
   <ItemTemplate>
   <%# Eval("content_title").ToString() %>
   <br/>
   <div style="clear:both;"></div>
   </ItemTemplate>
   </asp:Repeater>

This should write something like:

     Fink, James
     Merylou, Jane
     Marcy, Kim
     Bower, John
     Alset, Mike

The

     <%$ connectionStrings.connectionString %>

part selects the conn property from webconfig (if you have already set-up that.)

Upvotes: 1

Related Questions