Saturday, December 12, 2009

ASPX - DataGrid dalam Email

<% @Import Namespace="System.Web.Mail" %>
<% @Import Namespace="System.IO" %>
<% @Import Namespace="System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="vb" runat="server" Debug="true">

Sub Page_Load()
Dim myConnection as MySqlConnection
Dim strSQL as String
Dim myDataAdapter as MySqlDataAdapter
Dim myDataSet as DataSet
Dim myData as DataSet
Dim strNum as String
Dim strHist as String
Dim i as Integer
Dim area as String
Dim hist as String
Dim strTo as String
Dim strFrom as String
Dim strSubject as String
Dim strBody as String

myConnection= New MySqlConnection("server=localhost; user id=test; password=password; database=database; pooling=false;")

strSQL = "select * from database"
myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)
myDataSet = New Dataset()
myDataAdapter.Fill(myDataSet, "filtered_events")

MySQLDataGrid.DataSource = myDataSet
MySQLDataGrid.DataBind()

Dim SB as New StringBuilder()
Dim SW as New StringWriter(SB)
Dim htmlTW as New HtmlTextWriter(SW)
htmlTW.write("<font size=2pt>Datanya</font>")
MySQLDataGrid.RenderControl(htmlTW)

Dim dataGridHTML as String = SB.ToString()
Dim objMM as New MailMessage()

objMM.To = "me@me.com"
objMM.From = "Test"
objMM.BodyFormat = MailFormat.Html
objMM.Subject = "DataGrid Emailing"

objMM.Body = dataGridHTML
SmtpMail.SmtpServer = "smtp.xxx.com"
SmtpMail.Send(objMM)

End Sub
</script>

<form runat="server">
<asp:DataGrid id="MySQLDataGrid1" runat="server" >
</asp:DataGrid>
</form>
</body>
</html>
digubah dari sini

No comments: