C# .NET - Backup - Asked By suresh kotte on 25-Jan-12 05:31 AM

Hai every one,

            I have made  a school project. In project i take a datagridview,  backup button, import button. when the form is loaded retrive the data from database in gridview. so I want to , while iam click on backup button then the data export in wordpad format and save it. And click on import button, already saved file will be import in datagridview.  How can write that code i don't know.  kindly help me.

Thanks in advance
kalpana aparnathi replied to suresh kotte on 25-Jan-12 05:36 AM

I expect you should be reading the filename after the user has used the dialog:

System.IO.File.WriteAllText(save.FileName, textBox1.Text);

Also - SaveFileDialog is IDisposable, so you should be "using" it...

using (SaveFileDialog save = new SaveFileDialog())
    // your code that involves "save"
Suchit shah replied to suresh kotte on 25-Jan-12 05:40 AM
you can do some thing like below to export the data in word

protected void btnExportToDoc_Click(object sender, EventArgs e)
     Response.Buffer = true;
     Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
     Response.ContentEncoding = System.Text.Encoding.UTF7;
     Response.ContentType = "application/vnd.word";
     System.IO.StringWriter strWriter = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter HtmltxtWriter = new System.Web.UI.HtmlTextWriter(strWriter);

Suchit shah replied to suresh kotte on 25-Jan-12 05:40 AM
see below example of it

// page load event u can use other events
protected void Page_Load(object sender, EventArgs e)
    SqlConnection con = new SqlConnection("Data Source=;Initial Catalog=;User ID=;Password=");
     SqlCommand sqlCmd1 = new SqlCommand();
     sqlCmd1.Connection = con;
     sqlCmd1.CommandType = CommandType.Text;
     sqlCmd1.CommandText = "select * from  Table";
     DataSet ds = new DataSet();
     SqlDataAdapter da = new SqlDataAdapter(sqlCmd1);
     GridView1.DataSource = ds.Tables[0];
     if (ds.Tables[0].Rows.Count > 0)
      Export ex = new Export();
       ex.Convert(ds, Response);
// create a class say  Export.cs
public class Export
 public Export()
   // TODO: Add constructor logic here
public void Convert(DataSet ds, HttpResponse response)
    //*********Export to Excel/Doc**************
  //first let's clean up the response.object
    response.Charset = "";
     //set the response mime type for excel
    //  response.ContentType = "application/vnd.ms-excel"; // excel export
     response.ContentType = "application/msword"; // doc export
     //create a string writer
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
     //create an htmltextwriter which uses the stringwriter
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
     //instantiate a datagrid
    DataGrid dg = new DataGrid();
    //set the datagrid datasource to the dataset passed in
    dg.DataSource = ds.Tables[0];
    //bind the datagrid
    //tell the datagrid to render itself to our htmltextwriter
    //all that's left is to output the html
smr replied to suresh kotte on 25-Jan-12 05:43 AM

try this

protected void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  if (GridView1.Rows.Count.ToString + 1 < 65536) {
    GridView1.AllowPaging = false;
    GridView1.AllowSorting = false;
    StringWriter tw = new StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
    HtmlForm frm = new HtmlForm();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("content-disposition", "attachment;filename=" + "COP_Review_" + Strings.FormatDateTime(DateAndTime.Now, DateFormat.ShortDate).Replace("/", "_") + ".xls");
    Response.Charset = "";
    EnableViewState = false;
    GridView1.AllowPaging = true;
    GridView1.AllowSorting = true;
  } else {
    Interaction.MsgBox("Too many rows - Export to Excel not possible");