create a file in server using asp.net and c#

Joined
May 7, 2008
Messages
1
Reaction score
0
Hi all,

i have searched the net and found some code to create files in the server.i have given below the code, i am able to run the code well in my test machine but it doesnt run well from the production server. i get the error "illegal characters in path" or "The given path's format is not supported." can some one please help me on this as i am struck with this issue for a long time and has to be closed very urgent.

in this i am trying to pass the grid values to excel. This excel path is passed as attachment to mail to send to users.

//************** Code *************
private string CreateExcelFile(DataTable dt, string filename)
{
try
{
GridView Exportgrid = new GridView();
Exportgrid.HeaderStyle.BackColor = System.Drawing.Color.Blue;
Exportgrid.HeaderStyle.ForeColor = System.Drawing.Color.White;
Exportgrid.DataSource = dt;
Exportgrid.RowDataBound += new GridViewRowEventHandler(Exportgrid_RowDataBound);
Exportgrid.DataBind();
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(Exportgrid);
Exportgrid.RenderControl(oHtmlTextWriter);
string HtmlInfo = oStringWriter.ToString().Trim();
string strPath = Server.MapPath("~/Excel/") + "Excel_Sheet.xls";
if (File.Exists(strPath))
File.Delete(strPath);
System.IO.StreamWriter oExcelWriter = System.IO.File.CreateText(strPath);
oExcelWriter.WriteLine(HtmlInfo);
oExcelWriter.Close();
oExcelWriter = null;
oStringWriter.Close();
oHtmlTextWriter.Close();
Exportgrid.DataSource = null;
return strPath;

}
catch (Exception e)
{
showMessage = e.Message;
return showMessage;
}
}

private void SendMailWithAttach(string sPath, string Receiver)
{
try
{
MailMessage message = new MailMessage("(e-mail address removed)", "(e-mail address removed)");
SmtpClient client = new SmtpClient("smtp.gmail.com");
message.Subject = "Report";
message.Body = "Test Mail";
message.IsBodyHtml = true;
Attachment sAttach = new Attachment(sPath);
message.Attachments.Add(sAttach);
client.Send(message);
message.Dispose();
}
catch (Exception e)
{
Response.Write(e.Message);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,115
Messages
2,570,698
Members
47,273
Latest member
mikkamuertio

Latest Threads

Top