Saturday, January 22, 2011

Mail Sending Code in C#


protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage msg1 = new MailMessage();

        msg1.To = "kunalchakraborty@gmail.com";
        msg1.From = "info@sncindia.org";
        msg1.Subject = "mailchk";
        msg1.BodyFormat = MailFormat.Text;
        msg1.Body = "Dear Sir,";
        SmtpMail.SmtpServer = "127.0.0.1";
        SmtpMail.Send(msg1);
    }

Thursday, January 13, 2011

Set dorpdown list 0 index position-------- "Select"----------

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ToString());
  protected void Page_Load(object sender, EventArgs e)
  {
      con.Open();
      string query = "select * from FilterGridViewBySection ";
      SqlCommand cmd = new SqlCommand(query, con);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      DropDownList1.DataSource = ds;
      DropDownList1.DataTextField = "bid";
      DropDownList1.DataBind();
      //this line create a new item and set it at 0 index position
      DropDownList1.Items.Insert(0, new ListItem("Select", "select"));

  }

dynaic html in main page and also in same page using method using HTML generic Control

 private void getTopSolutions()
    {
        ContentPlaceHolder mpContentPlaceHolder;

        mpContentPlaceHolder =
          (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        string query;

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
      
        if (BlogType == "NULL")
        {
             query = "select top 6 * from blog order by bid";
        }
        else
        {
            query = "select top 6 * from blog where type='" + BlogType + "' order by bid";
        }
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            int iii = i + 1;
            HtmlGenericControl dv = (HtmlGenericControl)mpContentPlaceHolder.FindControl("newsdiv" + iii);
            dv.InnerText = ds.Tables[0].Rows[i]["header"].ToString();
        }

        ////newsdiv1.InnerText = "1 )" + ds.Tables[0].Rows[0]["header"].ToString();
        ////newsdiv2.InnerText = "2 )" + ds.Tables[0].Rows[1]["header"].ToString();
        ////newsdiv3.InnerText = "3 )" + ds.Tables[0].Rows[2]["header"].ToString();
        ////newsdiv4.InnerText = "4 )" + ds.Tables[0].Rows[3]["header"].ToString();
        ////newsdiv5.InnerText = "5 )" + ds.Tables[0].Rows[4]["header"].ToString();
        ////newsdiv6.InnerText = "6 )" + ds.Tables[0].Rows[5]["header"].ToString();

    }

    private void getretedSolutions()
    {
        string query;

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();

        if (BlogType == "NULL")
        {
            query = "select top 6 * from blog order by bid";
        }
        else
        {
            query = "select top 6 * from blog where type='" + BlogType + "' order by bid";
        }
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader da = cmd.ExecuteReader();

    
      while (da.Read())
        {
            HtmlGenericControl dv = new HtmlGenericControl("div");
            dv.Attributes.Add("class", "cs_article");

            HtmlGenericControl h2 = new HtmlGenericControl("h2");

            HtmlAnchor a1 = new HtmlAnchor();
            a1.InnerText = da["header"].ToString();
            a1.HRef = "Solution.aspx?TopicId=" + da["bid"].ToString();
            h2.Controls.Add(a1);

            HtmlGenericControl p = new HtmlGenericControl("p");
            p.InnerText = da["matter"].ToString();


            HtmlAnchor a11 = new HtmlAnchor();
            a11.InnerText = "read more";
            a11.HRef = "Solution.aspx?TopicId=" + da["bid"].ToString();
        

            dv.Controls.Add(h2);
            dv.Controls.Add(p);
            dv.Controls.Add(a11);
            bddv.Controls.Add(dv);

Monday, January 10, 2011

dynanic formview

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.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
    Class1 c = new Class1();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            string sid = Request.QueryString[""].ToString();
            string type = Request.QueryString[""].ToString();

            SqlConnection cn = new SqlConnection(c.connectionString);
            SqlCommand cmd = new SqlCommand("dbo.usp_get_blog_info", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@sid", sid);
            cmd.Parameters.AddWithValue("@type", type);
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.Fill(ds);
        }
        catch
        {
            SqlConnection cn = new SqlConnection(c.connectionString);
            SqlCommand cmd = new SqlCommand("dbo.usp_get_blog_info", cn);
            cmd.CommandType = CommandType.StoredProcedure;
           //cmd.Parameters.Add(new SqlParameter("@sid","10000"));
          // cmd.Parameters.Add(new SqlParameter("type", "NULL"));
           cmd.Parameters.AddWithValue("@sid", "100000");
           cmd.Parameters.AddWithValue("@type", "NULL");
           SqlDataAdapter da = new SqlDataAdapter(cmd);
          
            da.Fill(ds);
            FormView1.DataSource = ds;
            FormView1.DataBind();
         Label l=(Label)FormView1.FindControl("Header");
           l.Text =ds.Tables[0].Rows[0]["header"].ToString();
             TextBox l1=(TextBox)FormView1.FindControl("Label1");
           l1.Text =ds.Tables[0].Rows[0]["matter"].ToString();
              
        }


    }

    protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {
        FormView1.PageIndex = e.NewPageIndex;
        int i = e.NewPageIndex;
        SqlConnection cn = new SqlConnection(c.connectionString);
        SqlCommand cmd = new SqlCommand("dbo.usp_get_blog_info", cn);
        cmd.CommandType = CommandType.StoredProcedure;
        //cmd.Parameters.Add(new SqlParameter("@sid","10000"));
        // cmd.Parameters.Add(new SqlParameter("type", "NULL"));
        cmd.Parameters.AddWithValue("@sid", "100000");
        cmd.Parameters.AddWithValue("@type", "NULL");
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds1 = new DataSet();
        da.Fill(ds1);
        FormView1.DataSource = ds;
        FormView1.DataBind();
//this new lable l initialize the header label in formview;

        Label l = (Label)FormView1.FindControl("Header");
        l.Text = ds1.Tables[0].Rows[i]["header"].ToString();
        TextBox l1 = (TextBox)FormView1.FindControl("Label1");
        l1.Text = ds1.Tables[0].Rows[i]["matter"].ToString();
        //FormView1.DataBind();
    }
    protected void Label1_TextChanged(object sender, EventArgs e)
    {

    }
 
          
          
          
          
          
          
          
          
          
          
          
          
          
                                                                                                                                                                                                              
      



        
    }

Sunday, January 9, 2011

Formview PageIndexChanging event c# coding

protected void fv1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
fv1.PageIndex = e.NewPageIndex;
fv1.DataBind();
}

SQL Optimization

  SQL Optimization  1. Add where on your query  2. If you remove some data after the data return then remove the remove condition in the sel...