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


Top 5 Contributors of the Month
david stephan

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.
 

Step1:

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)
       {
           try
           {
               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() + ",");
                   }

                   str.Append("\r\n");
               }
               response.Clear();
               response.AddHeader("Content-Disposition", "attachment;filename=\"" + strName + "\"");
               response.Charset = "";
               response.Cache.SetCacheability(HttpCacheability.NoCache);
               response.ContentType = "application/vnd.text";
               System.IO.StringWriter stringWrite = new System.IO.StringWriter();
               System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

               response.Write(str.ToString());
               response.End();
               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();

ds=databasecontent();

// then pass this ds to exporttotext method like below

ExportToTextFile(ds,"Sample");

 Subscribe to Articles

     

Further Readings:

Responses

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