Saturday, May 31, 2008

Send Gridview through Email

Create aspx page and put Gridview and Bind it using SqlDataSource and add button control
to send Email with Gridview

In Code behind

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
private string GridViewToHtml(GridView gv)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(sw);
gv.RenderControl(hw);
return sb.ToString();
}
private bool SendMail(string body, string subject)
{
//string mailServerName = "SMTP.ewebbers.com";
string mailServerName = "SMTP.ewebbers.com";
MailMessage message = new MailMessage("noor3rb@hotmail.com", "y.ahmed@ewebbers.com", subject, GridViewToHtml(GridView1));

message.IsBodyHtml = true;
SmtpClient mailClient = new SmtpClient();
mailClient.Host = mailServerName;
mailClient.Send(message);
message.Dispose();
return true;
}

protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
bool x = SendMail(GridViewToHtml(GridView1), "Send Gridview");
if (x == true)
{
Response.Redirect("~/MailSent.aspx");
}
else
{
Label1.Text = "Try again";
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}

}

Friday, May 30, 2008

Solve Access denied to your own ASP.NET application Problem

Every have a bad day with your ASP.NET app? Ever get the yellow screen of death with this on it:
Access to the path "'c:\inetpub\wwwroot\Car_Image " is denied.
If you go googling for the problem, you'll find a few suggestions, solutions, ideas, and rants. We had the same problem happen at our shop a few times in the same week. In fact, it got to the point where we (the users in the local Administrator group) couldn't even access the folders themselves.
After putting our proverbial minds to work, we did something fairly simple. We reclaimed ownership of the folders. You can do this in the security tab of the ASP.NET Temporary files directory.
1) Right click and select "Properties"
2) Select the "Security" tab
3) Click on the "Advanced" button
4) Select the "Owner" tab
5) Select the owner (MACHINENAME\Administrators), check the "Replace owner on subcontainers and objects" and click OK

Friday, May 23, 2008

How to Refresh Page after time

In Page_Load of page write this code :

HtmlMeta RedirectMetaTag = new HtmlMeta();
RedirectMetaTag.HttpEquiv = "Refresh";
Response.AppendHeader("Refresh", "5");