0

Here is my example code on what I am trying to do, can you give an example syntax to make this possible?

The query select will run to show data on datalist and after that if button is click the results will be written to an HTML file.

Page Load

  protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataList();
                }
            }

Data List

public void DataList()
               {
                 String testcon = System.Configuration.ConfigurationManager.ConnectionStrings["TestConnection"].ToString();
                    MySqlConnection con = new MySqlConnection(testcon );
                    con.Open();
                    MySqlCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT * From ExampleTable";
                    cmd.ExecuteNonQuery();
                    DataSet ds = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                    da.Fill(ds);
                    DataList1.DataSource = ds;
                    DataList1.DataBind();

                    con.Close();
            }

Button Click

 protected void Button1_Click(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            using (FileStream fs = new FileStream((path + "\\Sample.html"), FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine("<html>");
                    w.WriteLine("</head>");
                    w.WriteLine("<body>");
                    w.WriteLine("DataList(ItemTemplate)");
                    w.WriteLine("</body>");
                    w.WriteLine("</html>");
                }
                LabelDisplay.Text = "File Successfully Created @ " + ((path + "\\Sample.html"));
            }

        }
Raniel Quirante
  • 315
  • 2
  • 15

1 Answers1

2

Session["DataList"] = ds.tables[0];

You can copy paste the below method which will convert the datatable to the html string.

protected string ExportDatatableToHtml(DataTable dt)  
{  
    StringBuilder strHTMLBuilder = new StringBuilder();  
    strHTMLBuilder.Append("<html >");  
    strHTMLBuilder.Append("<head>");  
    strHTMLBuilder.Append("</head>");  
    strHTMLBuilder.Append("<body>");  
    strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>");  

    strHTMLBuilder.Append("<tr >");  
    foreach (DataColumn myColumn in dt.Columns)  
    {  
    strHTMLBuilder.Append("<td >");  
    strHTMLBuilder.Append(myColumn.ColumnName);  
    strHTMLBuilder.Append("</td>");  

    }  
    strHTMLBuilder.Append("</tr>");  


    foreach (DataRow myRow in dt.Rows)  
    {  

    strHTMLBuilder.Append("<tr >");  
    foreach (DataColumn myColumn in dt.Columns)  
    {  
    strHTMLBuilder.Append("<td >");  
    strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());  
    strHTMLBuilder.Append("</td>");  

    }  
    strHTMLBuilder.Append("</tr>");  
    }  

    //Close tags.  
    strHTMLBuilder.Append("</table>");  
    strHTMLBuilder.Append("</body>");  
    strHTMLBuilder.Append("</html>");  

    string Htmltext = strHTMLBuilder.ToString();  

    return Htmltext;  
}  

After this method, You can call this method and convert to html file as below

 string content = ExportDatatableToHtml((DataTable)Session["DataList"]);
 System.IO.File.WriteAllText(@"C:\sample.html", contents);

If my post helps to solve your question, kindly check the green tick and upward this answer

  • You are assigning the dataset to the datalist1.datasource right. In that place, you can assign the dataset table in the session variable like in the my updated answer. – vivek ramasamy Apr 03 '18 at 10:59
  • Check and revert if need assistance. if solves your question , check the green tick :-) – vivek ramasamy Apr 03 '18 at 11:03
  • I got this error... when I put the Session["DataList"] below da.Fill(ds) 'System.Data.DataSet' does not contain a definition for 'tables' and no extension method 'tables' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?) – Raniel Quirante Apr 03 '18 at 11:05
  • you check the condition like this if(ds != null) { If(ds.Tables.count > 0) { Session["DataList"] = ds.Tables[0]; } } – vivek ramasamy Apr 03 '18 at 11:08
  • 1
    tables - T should be in capital letter (Tables). I updated my previous answer – vivek ramasamy Apr 03 '18 at 11:12
  • it says 'System.Data.DataSet' does not contain a definition for 'tables' – Raniel Quirante Apr 03 '18 at 11:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168110/discussion-between-vivek-ramasamy-and-christian-quirante). – vivek ramasamy Apr 03 '18 at 11:17