how to send gridview containing image via email using asp.net c# - asp.net

I have a gridview that contain image . I want to email that grid containing image. I have a code that send the gridview via email but the image in gridview are not visible at the receiver email id. The image in gridview are displayed from the database. can anyone help me to send gridview with its formatting via email.
public string gridData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}

Related

how can i send an image to stimulsoft web report from database

i am new in stimulsoft report
i wanna to send and show an image from database sql to stimulsoft report.
how can i do it?
i use asp.net C#
I create a datatable in behind code and set data into datatable to bind to report
a field of this datatable is Image datatype that i fill it by System.Drowing.Image object.
string normalPath = Server.MapPath("Pics\\Other\\PrintPage.gif");
FileStream fsN = new FileStream(normalPath, FileMode.Open, FileAccess.Read);
System.Drawing.Image normalImage = new System.Drawing.Image.FromStream(fsN);
fsN.Close();
DataTable result = new DataTable();
result.Columns.AddRange(new DataColumn[5]{
new DataColumn("LineNumber",typeof(int)),
new DataColumn("SerialNumber",typeof(int)),
new DataColumn("ActivationCode",typeof(string)),
new DataColumn("DetectorTypeId",typeof(int)),
new DataColumn("Image",typeof(System.Drawing.Image))
});
Stimulsoft.Report.StiReport stiReport = new Stimulsoft.Report.StiReport();
ds.Tables.Add(result);
stiReport.Load(Server.MapPath("CustomerActivator.mrt"));
stiReport.Render();
stiReport.RegData("Data", result);
StiWebViewer1.Report = stiReport;
StiWebViewer1.Visible = true;
You should set the Data Column property of the Image in the report template.

How to remove a Gridview from an ASPX page?

I have an Aspx page that displays a GridView. The gridview would appear with data that was depending on which catergory was selected from a drop down menu. The user has the option to then download this as a CSV file (export to CSV). I now want the Gridview not to appear (beacuse it is so large it often just hangs) but instead to be able to download a CSV file with the data from the gridview. I have successfully built the button allowing for the download of this data by clicking the button. However, I now cannot open the page without the gridView appearing. I tried to comment out the Gridview code - this just broke the page. I tried to set the gridview as Visible="False" but this did not work either. What else can I do to prevent the gridview from appearing and the user to be taken straight to a download dialogue box?
wrap your Gridview to a div and set it to display:none in asp.net code –
Ex: <div id="divhidegrid" runat="server" style="display:none;" > <asp:GridView ID="gvtest" runat="server" > </div> like this
You can load your data in DataSet or DataTable from Code Behind and then export it directly to Excel/CSV without assigning it to GridView such as in the following example to export it to Excel:
Public Sub ExportToExcel(dt As DataTable)
If dt.Rows.Count > 0 Then
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim dgGrid As New DataGrid()
dgGrid.DataSource = dt
dgGrid.DataBind()
'Get the HTML for the control.
dgGrid.RenderControl(hw)
'Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Disposition", "attachment; filename=Data.xls")
Me.EnableViewState = False
Response.Write(tw.ToString())
Response.[End]()
End If
End Sub
If you get errors like control must be placed in inside of form tag you may also be required put the following code in your backend (Reference: export-data-to-excel-from-datatable-gridview-aspnet-csharp)
Public Sub VerifyRenderingInServerForm(control As Control)
' Verifies that the control is rendered
End Sub
C# Version of above code:
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0) {
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=Data.xls");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
public void VerifyRenderingInServerForm(Control control)
{
// Verifies that the control is rendered
}

send rendered GridView in an email

I have a GridView whose HTML I need to put into an email.
How can this be done?
use an HTMLtext writer to render the gridview control.You will get an HTML output as string.use this as your message body.
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
Use a stringbuilder to create a HTML table to resample your gridview like so:
var output = new StringBuilder("<table cellpadding='5' style='border: solid 1px black;'>");
foreach (GridViewRow row in MyGridView.Rows)
{
output.Append("<tr>");
output.Append("<td>" + Title + "</td>");
output.Append("<td>" + Price + "</td>");
output.Append("<td>" + Quantity + "</td>");
output.Append("</tr>");
}
output.Append("</table>");
Ideally you should not be concerned about the GridView control. Take the data that is being displayed in the control, format it in what ever manner you want and pass it as your message body.
I would treat the logic to display Grid in my application and generating grid for message body as two separate functionality. What will be common in between the two, is the source of data. For formatting of data(generating the Grid), you can use the HTMLTextWriter class(as mentioned by others) and format the data in what ever way you want.
Two thing you can do for this
1) take screen shot of the current page by using any utility and send in mail
2) you can export grid as excel and than sent it in mail.
Export grid to Excel
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx

Concatenate text on 3 different textboxes to make bulleted list in email using VB.NET

i have 3 textboxes on an asp.net page and i would like to send the text on all those textboxes as an email message body when a button is clicked. I want each text on each textbox to appear on a new line(this i can do), but i also want the lines to be bulleted. Something like
Text on Textbox1
Text on Textbox2
Text on Textbox3
Thank you in advance.
If you are happy with HTML in the message body then create a HTML unordered list as follows:
C#:
StringBuilder body = new StringBuilder()
.Append("<ul><li>")
.Append(txtBoxOne.Text)
.Append("</li><li>")
.Append(txtBoxTwo.Text)
.Append("</li><li>")
.Append(txtBoxThree.Text)
.Append("</li></ul>");
MailMessage mMailMessage = new MailMessage();
mMailMessage.Body = body.ToString();
mMailMessage.IsBodyHtml = true;
VB.NET:
Dim body = New StringBuilder()
body.Append("<ul><li>")
body.Append(txtBoxOne.Text)
body.Append("</li><li>")
body.Append(txtBoxTwo.Text)
body.Append("</li><li>")
body.Append(txtBoxThree.Text)
body.Append("</li></ul>")
Dim mMailMessage = New MailMessage()
mMailMessage.Body = body.ToString()
mMailMessage.IsBodyHtml = True

LoadControl from windows application

Is it possible to LoadControl in windows application?
I have email generation as web, but I want to move it to windows service for monthly newsletter.
Emails now are implemented as UserControls, in this way html person can easily modify look & feel.
Current rendering implementation looks like:
StringBuilder sb = new StringBuilder(4000);
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
Page page = new Page();
EmailTemplateBase emailCtrl = (EmailTemplateBase)page.LoadControl(
"Controls/EmailTempaltes/Template.ascx");
// Exception here
emailCtrl.DataContext = dataContext;
emailCtrl.Parameter = parameter;
emailCtrl.RenderMode = renderMode;
emailCtrl.DataBind();
emailCtrl.RenderControl(htw);
subject = emailCtrl.Subject;
string MessageText = sb.ToString().Replace("\t", "").Replace(Environment.NewLine, "");
return MessageText;
Solution for me was to call web service, which was able to generate Html from .ascx.
Pass necessary parameters to
webservice
in Webservice, Build
new Page, than LoadControl
Set all parameters
Do rendering to
StringBuilder.
You can see code in question.
instead of the LoadControl method used in asp.net, you can use Controls.Add method of the parent control. Just add a panel to be the parent then:
UserControl uc1 = new UserControl //this is your usercontrol
Panel1.Controls.Add(uc1);

Resources