Popular Posts

Jul 26, 2013

Create dll file to send mail in asp.net




-          Create a library project named it EmailManager

 

 
-          Write this code to the class
-          public class EmailSender
-              {
-                  public bool EmailSend(MailMessage msg)
-                  {
-                      SmtpClient smtp = new SmtpClient();
-           
-                      smtp.Host = "smtp.gmail.com";
-                      smtp.Credentials = new System.Net.NetworkCredential("emailid@apsissolutions.com", "password");
-                      smtp.Port = 587;
-                      smtp.EnableSsl = true;
-                      try
-                      {
-                          smtp.Send(msg);
-                          return true;
-                      }
-                      catch
-                      {
-                          return false;
-                      }
-           
-                  }
-           
-              }

 
-Now build the project. A dll file created. Right click on the project select open project in windows explorer.
-Now we will send mail with this dll file we created.create a asp.net project add Reference -> select the dll file.

 
 
public EmailSender emailObj = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        emailObj = new EmailSender();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string to = "email@host.com";
        string subject = "Subject Goes Here";
        //string mailBody = "This E-mail From Egy Mall </br> The User Code For Your Employee Is :";

        MailMessage msg = new MailMessage();
        msg.To.Add(to);
        msg.CC.Add("email@host.com");
        msg.CC.Add("email@host.com");
        msg.From = new MailAddress(to);
       
        msg.Subject = subject;
        msg.Body = "<html><body>";
        msg.Body += "<p> This is an automatic email ";
        msg.Body += "indicating that we have received your request for ";
        msg.Body += "more information about really cool web designs.</p>";
        msg.Body += "Someone will be contacting you shortly.</p>";
        msg.Body += "<p>&nbsp;";
        msg.Body += "<p>Website Design Guide<br>";
        msg.Body += "http://www.website-designguide.com<br>";
        msg.Body += "<p>&nbsp;";
        msg.Body += "</body></html>";

        /**
         * StringBuilder body = new StringBuilder();
            body.Append("<html><body><table><tr><td>Name</td>");
            body.Append(txtSubject.Text);
            body.Append("</td></tr><tr><td>Contact" + txtSubject.Text);
            body.Append("</td><tr></table></body></html>");



            MailMessage mail = new MailMessage();
            mail.To.Add(toEmailAddress);
            
            mail.From = new MailAddress(GmailId);
            mail.Subject = txtSubject.Text;
            mail.Body = body.ToString();
            mail.IsBodyHtml = true;

          */



        //msg.Body = mailBody;
        msg.IsBodyHtml = true;
        emailObj.EmailSend(msg);
    }

Write this code in an button click event.




Jul 25, 2013

Select data using Stored Procedures with Parameters




StoreProcedure to select data:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmployeeDetailsByID]
      @EmployeeID int = 0
AS
BEGIN
      SET NOCOUNT ON;
      SELECT FirstName, LastName, BirthDate, City, Country
      FROM Employees WHERE EmployeeID=@EmployeeID
END



 Code Behind:
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetEmployeeDetailsByID";
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = txtID.Text.Trim();       
cmd.Connection = con;
try
{
    con.Open();
    GridView1.EmptyDataText = "No Records Found";
    GridView1.DataSource = cmd.ExecuteReader();
    GridView1.DataBind();
}
catch (Exception ex)
{
    throw ex;
}
finally
{
    con.Close();
    con.Dispose();
}

 you can also make some changes:

 using (SqlDataReader dr = cmd.ExecuteReader())
            {
                if (dr.Read())
                {
                    Label1.Text = dr["FirstName"].ToString();
                    Label2.Text = dr["LastName"].ToString();
                    Label3.Text = dr[3].ToString();
                    Label4.Text = dr["Email"].ToString();
                }
            }


You can also try:

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);



Get data to crystal report using storeprocedure (ASP.NET)



Create Storeprocedure:


    ALTER PROCEDURE billDetails
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    */
    @billno varchar(10)
AS
BEGIN

select orderid,bookid,bookname,quantity,price,totalprice,sc.billnumber,name,contact,shipaddress,amount FROM ShoppingCart as sc Inner Join PaymentDetails as pd ON sc.billnumber = pd.billnumber WHERE billnumber=@billno

END
    /* SET NOCOUNT ON */
    RETURN



Code Behind:

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\Proj\BookStore.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    DataSet ds = new DataSet();
    SqlDataAdapter adp;
    public static string billnum;
    store ss1 = new store();

    protected void Page_Load(object sender, EventArgs e)
    {
        ss1 = Session["data2"] as store;
        billnum = ss1.bino;
        LoginName ln = (LoginName)Master.FindControl("LoginName1");
        adp = new SqlDataAdapter("EXEC billDetailsOne @billno= " + billnum, conn);
        adp.Fill(ds, "billDetailsOne");
        ReportDocument report = new ReportDocument();
        string reportpath = Server.MapPath("CrystalReport.rpt");
        report.Load(reportpath);
        report.SetDataSource(ds.Tables["billDetailsOne"]);
        CrystalReportViewer1.ReportSource = report;
    }