Hello.
I am a learner of ASP.Net technology and currently I am exprementing with it. So I would need your guidence here.

I want to ask that waht is the best way to display data from a database. I want ideas, not the code.

  1. I want to display text data in a tabulour form. Very similar to a traditional html table.
  2. Also I want to display media + text data in grid form. Very similar to a shopping website. Something like this:

    45sdf12

Now, I know that we have plently of controls provided there in Visual Studio, but still I want more traditional way for it. I don't want any of the DATA control provided, instead I want to make my own system to display random data from Data base. (Like using some text boxex or Image controls or something like that.)

So again, I want ideas. Do we have to use DATA controls provided. Can't we generate our own HTML based system for it? So that I have more control on it.

Thanks in Advance.

Yes, of course you can provide your own HTML custom code to display the results. Rather than using the controls, you would connect to the database as you normally would, then collect the response and for example, build the table as you cycle through the results. Same approach as if you were doing so in a classic ASP page.

You can't use purely HTML as you are obviously connecting to a database.
I haven't used ASP, I'm a PHP and MySQL person but the principals should be the same.

I would store a link in the database for each of the images, and a description to go with them. From that I would create a while do script to cycle through and then a piece inside a HTML table to display them.
You said you wanted it random, not sure what you mean by this but if you mean randomly select the order then just simple add an mt_rand() to it to randomly select the Primary Key (ID).

Not sure if this is what you meant, but it's my small piece. It's rather simple to do (if this is what you mean) and could probably be done with less than twenty lines.

to clarify... i wasnt suggesting that this can be done with HTML alone. What I meant is that without an asp.net control, you could connect to the database and while you read through the results from your query, you inject the HTML code there.

It is surely a lot easier to use the asp.net controls. just my opinion. You can use template fields and customize it 100%.

Yes, of course you can provide your own HTML custom code to display the results. Rather than using the controls, you would connect to the database as you normally would, then collect the response and for example, build the table as you cycle through the results. Same approach as if you were doing so in a classic ASP page.

OK. Thanks for your reply.
Although I don't have any idea about classic ASP but as far as I could understand with your reply is that to create an html table according to the records I get from the database query. For example if I get 5 recodrs with 3 fields each, then I should create a table with 5x3 structure.

But I am still not sure how to place the results into the HTML cells I just created.

Should I place a Label control in each cell and change the text property of it? Or do we have any other possibilities.

Thanks in Advance.

You can't use purely HTML as you are obviously connecting to a database.

Oh. I am sorry I could not make you understand what I wanted.
Actually I know we can't just use HTML to get results from database. I already know the way to fetch results from database without actually using Data Controls. What I was asking for was about the best practice to display the data aquired from a database query.

but anyways, thanks for the idea.

you could connect to the database and while you read through the results from your query, you inject the HTML code there.

OK. I thought of this idea before. But I write my database connectivity code into a VB file. So when I am getting my results how to write HTML code in that VB file. I dont write my database code into my source ASPX file as script. Do I have to write it there?

Here is another thread that discusses the same concept.
http://www.daniweb.com/web-development/aspnet/threads/424274/display-data-in-html-table

You can either write your code in the aspx or aspx.vb file. The web server puts it all together during processing. The purpose of the code behind is really for organizational purposes and it allows for a web designer and web developer to work independently.

There are several ways to accompish what you are asking. I prefer just using a gridview control when wanting to display data in a table form. You also have the datalist view which is very easy to customize template items.

There are a lot of examples on line.

In any case, just to give you a better idea...what you can do is in your aspx page, just call a subroutine where you want to display the information..

<% displayData() %>

Then in your code behind, create a subrouting called displayData(), and in that sub connect to your datasource and create the output. You can use response.write() method. There are many ways to accomplish this. plenty of online examples depending on your approach.

In any case, just to give you a better idea...what you can do is in your aspx page, just call a subroutine where you want to display the information..

<% displayData() %>

Then in your code behind, create a subrouting called displayData(), and in that sub connect to your datasource and create the output. You can use response.write() method. There are many ways to accomplish this. plenty of online examples depending on your approach.

Well Thanks a lot for this great idea. I am going to use this method for sure. But the last confusion, as u suggested to use response.write() method, but I am not sure about it.

I think I can write the outputs from my database directly using this method, but can I use this method to write html codes as well?

Yes.. I'll just say again that this approach is not one that i would implement any longer, but that is just my style of coding.

In any event.. in the response.write(), you could include HTML syntax. For example.

Response.Write("<table>")
Start Loop
Response.Write("<tr><td>" & variable & "</td></tr>")
End Loop
Response.Write("</table>")

Hope that helps...

But why you are trying to create your own code? There are repeater, listview, datalist, gridview ...etc. Best way is to use these controls.
But if you want to code your own control it's simple again. Just write foreach or for loop and create dynamic table or div.

StringBuilder sb = new StringBuilder();
sb.AppendLine("<table>");
foreach(Item item in datatable.items)
{
    sb.AppendLine("<tr>");
    sb.AppendLine("<td>");
    ... fooo ...
    sb.AppendLine("</tr>");
    sb.AppendLine("</td>");
}
sb.AppendLine("</table>");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.