This is a migrated thread and some comments may be shown as answers.

Problem with exporting grid in loop

4 Answers 126 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 19 May 2011, 10:58 AM
I have a sql-view which contains some simple data and also an column with an emailadres. When i click a button i want to send a mail to every distinct emailadres with an export of the view containing only the rows with that specific emailadres.

Everthing is going well except that the mails always get the same attachment! I guess it has something to do with the way I subcribe to the grid exporting event?

This is my code:

protected void rbTestMailVersturen_Click(object sender, EventArgs e) {
    string query = "";
    teller = 0;
    SaveMailingForm();
    RadGrid emailgrid = new RadGrid();
    PlaceHolder1.Controls.Add(emailgrid);
    //Fill a list with emailadresses
    FillEmailadressen();
    for (int i = 0; i < emailadressen.Count; i++) {
        {
            using (conn = new SqlConnection(ConnectionString)) {
                conn.Open();
                emailgrid.DataSource = null;
                emailgrid.Rebind();
                string ID = Request.QueryString["id"].PadLeft(2, '0');
                query = "SELECT * FROM A_MAN_" + ID + " WHERE EMAILCOLUMN ='" + emailadressen[i] + "'";
                SqlDataAdapter da = new SqlDataAdapter(query, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                emailgrid.DataSource = dt;
                emailgrid.Rebind();
                emailgrid.GridExporting += new OnGridExportingEventHandler(emailgrid_GridExporting);
                switch (rcbExportType.SelectedItem.Text) {
                    case "CSV":
                        emailgrid.MasterTableView.ExportToCSV();
                        break;
                    case "Excel":
                        emailgrid.MasterTableView.ExportToExcel();
                        break;
                    case "PDF":
                        emailgrid.MasterTableView.ExportToPdf();
                        break;
                    case "Word":
                        emailgrid.MasterTableView.ExportToWord();
                        break;
                }
            }
        }
    }
}
protected void emailgrid_GridExporting(object source, GridExportingArgs e) {
    string filename = DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + rtbOnderwerp.Text + teller + ".";
    switch (rcbExportType.SelectedItem.Text) {
        case "CSV":
            filename += "csv";
            break;
        case "Excel":
            filename += "xls";
            break;
        case "PDF":
            filename += "pdf";
            break;
        case "Word":
            filename += "doc";
            break;
    }
    string path = Server.MapPath("~/" + filename);
    using (FileStream fs = File.Create(path)) {
        Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
        fs.Write(info, 0, info.Length);
    }
    MailMessage msg = new MailMessage();
    MailAddress From = new MailAddress("info@xxxxxxxxxxxx.nl", "info@xxxxxxxxxxxx.nl");
    msg.From = From;
    msg.Subject = rtbOnderwerp.Text;
    msg.Body = rtbMailtekst.Text;
    msg.To.Add(new MailAddress(emailadressen[teller]));
    teller++;
    Attachment at = new Attachment(Server.MapPath("~/" + filename));
    msg.Attachments.Add(at);
    SmtpClient client = new SmtpClient("localhost");
    client.Send(msg);
    msg.Dispose();
    FileInfo fi = new FileInfo(Server.MapPath("~/" + filename));
    fi.Delete();
    if (teller == emailadressen.Count)
        Response.Redirect(Request.Url.ToString());
}

Hopefully anyone can help, that would be a lifesaver!

Thanks

4 Answers, 1 is accepted

Sort by
0
Frank
Top achievements
Rank 1
answered on 23 May 2011, 08:17 AM
Anyone has an idea or should I open a support ticket?
0
Daniel
Telerik team
answered on 25 May 2011, 09:32 AM
Hello Frank,

There is no easy way to export the control more than once in a single request due to the fact that the generated file content is streamed to the response object.
You can try the following:
- create a session variable to serve as counter
- export the control
- save the file to the server
- refresh the page (response.redirect)
- at some page event check the session variable to see if it reached the max value (emailadressen.Count)
- increment the session counter value
- export the control again

I hope this helps.

Regards,
Daniel
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Frank
Top achievements
Rank 1
answered on 31 May 2011, 12:37 PM
Hi Daniel,

Too bad there is no other way to do this. I will give this a shot next week and I will let you know if it worked.

Thanks,

Frank
0
Frank
Top achievements
Rank 1
answered on 11 Jul 2011, 10:53 AM
A little bit late reaction but i've tried it and it's working! I did it exactly like you suggested.
Tags
Grid
Asked by
Frank
Top achievements
Rank 1
Answers by
Frank
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or