.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Export dataset data into text file.

Posted By:Dhivakaran       Posted Date: November 29, 2010    Points: 50    Category: C#    URL: http://www.dotnetspark.com  

Here I m going to show how we can export to datset datas into text file.


Create one class into your project and add below methods within your class.the below method I have passed two parameters. one is "ds"in this argument we need to pass the dataset and another parameter name is "strName" in this argument we can specify(pass) the text name.

 public static bool ExportToTextFile(DataSet ds, string strName)
               HttpResponse response = HttpContext.Current.Response;
               StringBuilder str = new StringBuilder();
               for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                   for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                       str.Append(ds.Tables[0].Rows[i][j].ToString() + ",");

               response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + "\"");
               response.Charset = "";
               response.ContentType = "application/vnd.text";
               System.IO.StringWriter stringWrite = new System.IO.StringWriter();
               System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

               return true;
           catch (Exception ex)
               return false;

Steps 2:

Place button in your form "Export to to text" and within button click eventCreate one dataset object and assign some data into dataset as below code.

dataset ds=new dataset();


// then pass this ds to exporttotext method like below


 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend