Showing posts with label Append Setting. Show all posts
Showing posts with label Append Setting. Show all posts

Thursday, 9 February 2012

USE OF GRID VIEW IN ASP.NET (GRID VIEW EVENTS



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
        GridDataBind();
     }

    public void GridDataBind()
     {


    String constr = ConfigurationSettings.AppSettings["constr"];
    SqlConnection con = new SqlConnection(constr);
    SqlCommand com = new SqlCommand("select * from Table_Name", con);

    con.Open();
    SqlDataReader dr = com.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();
    con.Close();
      }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
    GridViewRow row = GridView1.Rows[e.RowIndex];
    DataKey key;
     if (row != null)
      {
     key = GridView1.DataKeys[row.RowIndex];
     TextBox txtname = (TextBox)row.FindControl("txtname");
     TextBox txtpass = (TextBox)row.FindControl("txtpass");
     string strname = txtname.Text;
     string strpass = txtpass.Text;
     string constr = ConfigurationSettings.AppSettings["constr"];
     SqlConnection conn = new SqlConnection(constr);
     SqlCommand com = new SqlCommand("update Table_Name set               
     name='"+strname+"', pass='"+strpass+"' where ID='"+key.Value+"'", conn);
     conn.Open();
     com.ExecuteNonQuery();
     conn.Close();
     GridView1.EditIndex = -1;
     GridDataBind();
      }
      }
     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
     {
     GridView1.EditIndex = e.NewEditIndex;
     GridDataBind();
      }
   protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgse)
     {
     GridView1.EditIndex = -1;
     GridDataBind();
      }
      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
      GridViewRow row = GridView1.Rows[e.RowIndex];
      DataKey key ;
      key = GridView1.DataKeys[row.RowIndex];
      if (row != null)
      {
      String constr = ConfigurationSettings.AppSettings["constr"];
      SqlConnection conn = new SqlConnection(constr);
      SqlCommand com = new SqlCommand();
      conn.Open();
      com.CommandText = "delete from Table_Name where ID='" + key.Value + "'";
      com.Connection = conn;
      com.ExecuteNonQuery();
      conn.Close();
      }
      GridDataBind();
      }
      }
Table_Name, Append Setting, Execute Query, Newedit Index, Web.Security, Grid View


Wednesday, 8 February 2012

HOW TO SEND A MAIL IN ASP.NET



public partial class mailer : System.Web.UI.Page
{
MailMessage mailmsg;
SqlConnection con;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] == null)
{
Response.Redirect("index.aspx");
}
//if (Request.QueryString["id"] != null)
//{
//    txtToAddress.Text = Request.QueryString["id"];
//}
if(!IsPostBack)
{
String user = Session["admin"].ToString();
con = new SqlConnection(ConfigurationManager.ConnectionStrings["name"].ConnectionString);
con.Open();
cmd = new SqlCommand("select email from registration where username='" + user + "'",con );
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
txtFromAddress.Text = dr.GetString(0);
}
dr.Close();
}
}
protected void btnSendmail_Click(object sender, ImageClickEventArgs e)
{
try
{
String to = txtToAddress.Text;
String from = txtFromAddress.Text;
String cc = txtCCAddress.Text;
String bcc = txtBCC.Text;
String subject = txtSubject.Text;
String body = txtMessage.Text;
mailmsg = new MailMessage();
mailmsg.From =new MailAddress( from );
mailmsg.To.Add ( new MailAddress(to));
if ((cc != null) && (cc != string.Empty))
{
mailmsg.CC.Add(new MailAddress(cc));
}  
if ((bcc != null) && (bcc != string.Empty))
{
mailmsg.Bcc.Add(new MailAddress(bcc));
}
mailmsg.Subject = subject;
mailmsg.Body = body;
if (fileAttachment.HasFile)
{
mailmsg.Attachments.Add(new Attachment(fileAttachment.PostedFile.FileName));
}
// Set the format of the mail message body as HTML
mailmsg.IsBodyHtml = true;
// Set the priority of the mail message to normal
mailmsg.Priority = MailPriority.Normal;
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = ConfigurationManager.AppSettings["hostname"];
smtpclient.Send(mailmsg);
}
catch (Exception ex)
{
lblmsg.Text = "Your mail has been sent..";
}
}
}