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"));
            }

        }
7
  • Is RenderControl what you are looking for? Commented Apr 3, 2018 at 10:46
  • if RenderControl can get the HTML string of DataList, then that is what I am looking for. Commented Apr 3, 2018 at 10:51
  • Yes, that would do it Commented Apr 3, 2018 at 10:52
  • but, will this work if I StreamWrite the DataList to become an HTML file? Commented Apr 3, 2018 at 10:53
  • You can get the html in a string like in the link I gave you. Commented Apr 3, 2018 at 10:55

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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.
Check and revert if need assistance. if solves your question , check the green tick :-)
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?)
you check the condition like this if(ds != null) { If(ds.Tables.count > 0) { Session["DataList"] = ds.Tables[0]; } }
tables - T should be in capital letter (Tables). I updated my previous answer
|

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.