Wednesday, 18 July 2012

How to send mail in asp.Net Using c#

sing System.Net.Mail;

public partial class SendingMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        try
        {
            string textmag = "";
            textmag = "Sangram Singh<admin@iition.com>";
            MailMessage mgs = new MailMessage(textmag, txtEmail.Text, txtSubject.Text, txtMessage.Text);
            SmtpClient client = default(SmtpClient);
            mgs.IsBodyHtml = true;
            client = new SmtpClient();
            client.Host = "mail.gmail.com";
            client.Credentials = new System.Net.NetworkCredential("admin@gmail.com", "8717574491);
            client.Send(mgs);
            Response.Write("<script>alert('Mail send successfully...')</script>");
        }
        catch
        {
        }
        finally
        {
        }

How to convert data in binary to image format in asp.Net using c#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using System.Web;
public class HandlerStuImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        SqlConnection con = new SqlConnection(@"data source=.\sqlexpress; initial catalog=AdminUse; integrated security=SSPI;");
        con.Open();
        string sql = "select imagepath from binaryimg where id=@id";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = context.Request.QueryString["Eid"];
        cmd.Prepare();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((byte[])dr["imagepath"]);
        dr.Close();
        con.Close();
    }

    public bool IsReusable
    {
        get { return false; }
    }
 try
        {
            string Sid = "";


            cmd = new SqlCommand("InsertBinary", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@imagetype", "Student");
            cmd.Parameters.Add("@id", SqlDbType.Int);
            cmd.Parameters["@id"].Direction = ParameterDirection.Output;
            con.Open();
            if (cmd.ExecuteNonQuery() > 0)
            {
                Sid = cmd.Parameters["@id"].Value.ToString();
                if (FileUploadBinary.HasFile)
                {
                   
                    int filelength = FileUploadBinary.PostedFile.ContentLength;
                    byte[] stuImage = new byte[filelength + 1];                  
                    FileUploadBinary.PostedFile.InputStream.Read(stuImage, 0, filelength);
                    string str = "Update [binaryimg] set imagepath=@StuImage where id=" + Sid + "";
                    SqlCommand cmd1 = new SqlCommand(str, con);
                    cmd1.Parameters.AddWithValue("@StuImage", stuImage);
                    cmd1.ExecuteNonQuery();
                    //SqlCommand cmd1 = new SqlCommand(str, con);
                    //cmd1.ExecuteNonQuery();
                    Image1.ImageUrl = "~/HandlerStuImage.ashx?Eid=" + Sid;
                }
            }

Monday, 16 July 2012

Sunday, 15 July 2012

what is differences between ExecuteReader and ExecuteNonQuery and ExecuteScalar.?