Monday, April 30, 2012

Dataset to XML

In this Article, we are exporting data from DataSet to XMl file. The below code uses dataset to take data from the database and populate the grid on the .aspx page and creates a xml file in the solution directory.

Add the connection string in the WEB.CONFIG file.

<connectionStrings>
<add name="cnStrMain" connectionString="CONNECTION STGRING" providerName="System.Data.Sqlclient"/>
</connectionStrings>

Code behind:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection sqlcn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnStrMain"].ConnectionString.ToString());
        
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn2XML_Click(object sender, EventArgs e)
    {
        
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT * FROM MEMBERS where COMPANY_ID is not null";

        command.CommandType = CommandType.Text;
        command.Connection = sqlcn;
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds, "MEMBERS");

        if (ds.Tables[0].Rows.Count > 0)
        {
            grdXML.DataSource = ds;
            grdXML.DataBind();
        }

        // Get a StreamWriter object
        StreamWriter xmlDoc = new StreamWriter(Server.MapPath("~/Testdo.xml"), false);
       
        // Apply the WriteXml method to write an XML document
         ds.WriteXml(xmlDoc);
         xmlDoc.Close();
    }
}

Tuesday, April 10, 2012

Send Mail using VB.NET

In VB.NET you use SMTP protocol for sending email . SMTP stands for Simple Mail Transfer Protocol . VB.NET uses System.Net.Mail namespace for sending mail . You need to instantiate SmtpClient class and assign values to the Host and Port. For SMTP default port is 25 and it can vary for different Mail Servers. In the following example I have used Gmail address to send mail

The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587.

Design:

Code:

Imports System.Net.Mail

Public Class Form1
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("username@gmail.com")
            mail.To.Add(txtTo.Text)
            mail.Subject = txtsubject.Text
            mail.Body = txtmail.Text
            SmtpServer.Send(mail)
            MsgBox("Mail send!")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

Send Mail using ASP.NET

Sending mail using ASP.NET/C# - using Gmail SMTP.

In ASP.NET you use SMTP protocol for sending email . SMTP stands for Simple Mail Transfer Protocol . ASP.NET uses System.Net.Mail namespace for sending mail . You need to instantiate SmtpClient class and assign values to the Host and Port. For SMTP default port is 25 and it can vary for different Mail Servers. In the following example I have used Gmail address to send mail

The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587.

Following is code to send mail using ASP.NET/C#:

Design:


Code:

You need to include the following namespaces:
using System.Net.Mail;
using System.Net;

protected void btnSend_Click(object sender, EventArgs e)
    {
        const string FromAddress = "gmail id";//For Example:hello@gmail.com
        string Subject = txtsubject.Text;
        String Body = txtMail.Text;
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);
        mail.From = new MailAddress(FromAddress);
        mail.Subject = Subject;
        mail.Body = Body;
        mail.Priority = System.Net.Mail.MailPriority.High;
        SmtpClient mclient = new SmtpClient();
        mclient.Credentials = new System.Net.NetworkCredential(FromAddress,
        "gmail id password");
        mclient.Port = 587; // Gmail works on this port
        mclient.Host = "smtp.gmail.com";
        mclient.EnableSsl = true; //Gmail works on Server Secured Layer

        try
        {
            mclient.Send(mail);
        }
        catch (Exception error)
        {
            throw error;
        }

    }