How to create an Html page dynamically in Console Application without using for loop

Today i will discuss an article how to Create dynamically an Html page with use of Dataset or result set. But Conditions apply that we do not use any for loop in that Code.Inorder to increase the performance of the Application.The Article will be very interesting and will be Useful who wants to create an Html page Dynamically when using Console Application.

Generally when you work with Console Applications you will have an requirement that you need to generate an dynamically Html page based on the Dataset or result set.But Many programs chose to iterate the result set and they will generate Html page for that.But Consider a situation there are more than 1000 rows in your result set and looping all the or result set will definitely a tedious or cumbersome process and also a time taking one.

So for that we need to work the Datagrid class. But in Console Application you will not see any Datagrid class why because console application do not posses web Controls.So for that we need to Use WebUI Controls in our Console application for that we need to import two important namespaces those are

using System.Web.UI;
using System.Web.UI.WebControls;

After Using these two namespaces we will have Datagrid Class will be visible to our page.See the below Image

DataGrid Class

After getting Datagrid Class Now our task is very easy that we establish a connection and get the Dataset or Resultset and bind the dataset to datagrid.

Binding Data

Now our Next task is to render the Datagrid Control with htmlwriter and string writer and render it to the Grid Control.They are in the Name space of System.IO.Below the Snippet Code will generate an html page without using for loop.

SqlConnection consql = null;
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htmlwrter = new HtmlTextWriter(sw);
DataSet dst = new DataSet();
string strconsql = "Data Source=VM-HYDSERVER01;Initial Catalog=HostingAdmin;User ID=sa;Password=syn123$$";
consql = new SqlConnection(strconsql);
SqlCommand cmdsql = new SqlCommand("select USR_SINT_STATUS from MBX_MST_USERS_HISTORY where USR_INT_USERID='" + 50 + "' ", consql);
SqlDataAdapter adaptsql = new SqlDataAdapter(cmdsql);
DataGrid dgrd = new DataGrid();
dgrd.DataSource = dst;
System.IO.StreamWriter swsgf = new System.IO.StreamWriter("D:\\users\\Covert.html");

NOTE:Make sure and ensure that when you use stream writer after using it for file Creation you make it Close.Otherwise your file will be Created but no contents will be seen.

Whole Code

The Output is place in my D drive and Users with Convert.html.See the below screen or Output...


When you open the html it will be the resultset that you had seen earlier in the Dataset Visualizer.


Note:You can apply Styles to Datagrid dynamically like we use in tables(with using of for Loops).


Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: