开发者

Send gridview in email basics C#

开发者 https://www.devze.com 2023-02-24 06:51 出处:网络
I would like to create a mail in which a gridview is included. I\'v开发者_如何学Goe been searching the internet and tried multiple solutions. But somehow I\'m missunderstanding or doing something wron

I would like to create a mail in which a gridview is included. I'v开发者_如何学Goe been searching the internet and tried multiple solutions. But somehow I'm missunderstanding or doing something wrong. With my current code I get the error:

System.InvalidOperationException: Data source is an invalid type. It must be either 
an IListSource, IEnumerable, or IDataSource. 

I read about using StringBuilder, StringWriter, HtmlTextWriter, RenderControl and so on, but still can't figure out how to make this work.

Here is my code:

        public void Email()
    {            
        string conn = "Data Source=pc-..";
        System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
        sqlConn.Open();
        SqlCommand sendGrid = new SqlCommand("SELECT * FROM tblSomething", sqlConn);

        GridView grd = new GridView();

        if (sendGrid != null)
        {
            grd.DataSource = sendGrid.ExecuteReader();
            grd.DataBind();
        }

        StringBuilder sb = new StringBuilder();

        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grd.RenderControl(htw);

        MailMessage mail = new MailMessage();

        mail.To.Add("ToWhoEmail");
        mail.From = new MailAddress("FromWho");
        mail.Subject = "SubjectSomething";
        mail.IsBodyHtml = true;
        mail.BodyFormat = System.Net.Mail.MailMessage.IsBodyHtml.Html;
        // At this line above i get an error. He doesn't recognize BodyFormat = ...;
        // System.Web.Mail.MailFormat is obsolete.
        // The recommended alternative is: System.Net.Mail.MailMessage.IsBodyHtml
        mail.Body = sb.ToString();
        SmtpClient smtp = new SmtpClient("...");
        smtp.Send(mail);}

I have also tried making the gridview in a different cs file and call it with: SendGrid.MakeGrid() in the mail.Body = "..."; But all i get then is: System.Web.UI.WebControls.GridView in my email. All help is appreciated, thank you in advance.

Regards, Mati


The problem is on this line:

grd.DataSource = sendGrid;

You are trying to use a SqlCommand as data source to the grid view which is not possible. You could use a data reader or a data set:

grd.DataSource = sendGrid.ExecuteReader();

Also make sure you dispose those connections and commands properly by wrapping them in using statements.


in your above code i didn't see the smtp settings for the mail sending ....set the port no and the sever access credential like u'r username and password of gmail and port

smtp.gmail.com (use authentication)

Use Authentication: Yes

Port for TLS/STARTTLS: 587

Port for SSL: 465


and for the grid view datasource u'r using sqlcommanad as data source is not right use dataadapter....

0

精彩评论

暂无评论...
验证码 换一张
取 消