Thursday 27 October 2011

How To Edit,Update, Cancel, and Mail in Your GridView
















<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            onrowcancelingedit="GridView1_RowCancelingEdit"

            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"

            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"

            CellPadding="3" GridLines="Vertical"

            onselectedindexchanged="GridView1_SelectedIndexChanged"

            onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting">

            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />

            <Columns>

                <asp:TemplateField HeaderText="Emp_id">

                <ItemTemplate>

                <asp:Label runat="server" Text='<% #Eval("Emp_id") %>' ID = "id"></asp:Label>

                </ItemTemplate>

               

                </asp:TemplateField>

               

                <asp:TemplateField HeaderText="Emp_Name">

                    <ItemTemplate>

                        <asp:Label ID="Label1" runat="server" Text='<% #Eval("Emp_Name") %>'></asp:Label>

                    </ItemTemplate>

                    <EditItemTemplate>

                    <asp:TextBox ID="TextBox2" runat="server" Text='<% #Eval("Emp_Name") %>'></asp:TextBox>

                    </EditItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Emp_Salary">

                    <ItemTemplate>

                        <asp:Label ID="Label2" runat="server" Text='<% #Eval("Emp_Salary") %>'></asp:Label>

                    </ItemTemplate>

                    <EditItemTemplate>

                        <asp:TextBox ID="TextBox3" runat="server" Text='<% #Eval("Emp_Salary") %>'></asp:TextBox>

                    </EditItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField HeaderText="Action">

                    <EditItemTemplate>

                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>

                        &nbsp;

                        <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>

                        &nbsp;

                        <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Delete"

                            onclick="LinkButton4_Click">Delete</asp:LinkButton>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />

            <AlternatingRowStyle BackColor="#DCDCDC" />

        </asp:GridView>


using System;

using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

namespace GridViewEditUpdateMail
{
    public partial class _Default : System.Web.UI.Page
    {
SqlConnection cn = new SqlConnection("server = JEET-PC\\JEET; user = sa; password = dhakad; database = jeet");
        SqlDataAdapter da; 
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_data();
            }
        }
        public void fill_data()
        {
            String sql = "select * from Emp";
            da = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            fill_data();
        }

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            fill_data();
        }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            String sql;

string id ((Label)GridView1.Rows[e.RowIndex].FindControl("id")).Text;
string tx = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string tx2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;

sql = "update Emp set emp_name =@a,emp_salary = @b where emp_id =@c";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.AddWithValue("@a", tx);
            cmd.Parameters.AddWithValue("@b", tx2);
            cmd.Parameters.AddWithValue("@c", id);
            cn.Open();
            cmd.ExecuteNonQuery();
            fill_data(); 

Send_Mail("This is Test Mail", "Hi jeetu Your Udated Name is ="+tx+" and your updated Salary is ="+tx2, "jeetu_choudhary13@gmail.com", "vishal_verma54@yahoo.com");
}


public void Send_Mail(String sub, String Body, String From, String To)
        {
            try
            {
                MailMessage mail = new MailMessage();
                //mail.To.Add("vishal_verma54@yahoo.com");
                mail.To.Add(To);
               // mail.From = new MailAddress("jeetu.choudhary13@gmail.com");
                mail.From = new MailAddress(From);
                //mail.Subject = "test mail";
                mail.Subject = sub;
                //mail.Body = "hi i am jeetu";
                mail.Body = Body;
                SmtpClient smtp = new SmtpClient();
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.UseDefaultCredentials = false;
                smtp.Host = "smtp.gmail.com";
                smtp.Credentials = new System.Net.NetworkCredential("jeetu.choudhary13@gmail.com", "*******");
                smtp.EnableSsl = true;
                smtp.Send(mail);
            }
            catch (Exception ex)
            {
                Response.Write("this is exception " + ex.Message);
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}

How to Mail mail From Gmail

NameSpaces:
using System.Net.Mail; 



public void Send_Mail()
    {
        try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add("vishal_verma54@yahoo.com");
            mail.From = new MailAddress("jeetu.choudhary13@gmail.com");
            mail.Subject = "test mail";
            mail.Body = "hi i am jeetu";
            SmtpClient smtp = new SmtpClient();
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Host = "smtp.gmail.com";
            smtp.Credentials = new System.Net.NetworkCredential("jeetu.choudhary13@gmail.com", "*******");
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            Response.Write("this is exception " + ex.Message);
        }
    }

What is Difference Between ExecuteReader, ExecuteScaller annd ExecuteNonQuery ?

ExecuteReader : Use for accessing data. It provides a forward-only, read-only, connected recordset.

ExecuteNonQuery : Use for data manipulation, such as Insert, Update, Delete.

ExecuteScalar : Use for retriving 1 row 1 col. value., i.e. Single value. eg: for retriving aggregate function. It is faster than other ways of retriving a single value from DB.

Saturday 8 October 2011

How to Clear text boxes using JQuery(Client Side Script) on buuton Click

 $('input[type="text"]').each(function() { this.value = ''; });
 $('textarea').each(function() { this.value = ''; });

How to Clear all Text Boxes on button Click in windows Application


public void clearAlltextbox(Form fr)
{
foreach(control c in fr.controls)
{
if(c.gettype()==typeof(textbox))
{
((textbox)c).clear();
}
}
}

How to set a button for defult submission of a page on server when user Press Enter Key


<form  style="margin-top:0; margin-right:0; margin-left:0;"  
id="form1"runat=server defaultbutton ="cmdLogin" >

How to Disable a Data Grid Column button from Code Behind


Public Sub DisableButton()
For totalrow = 0 To dgWelStatus.Items.Count - 1

If (CType(dgWelStatus.Items(totalrow).FindControl("lblStatusid"), Label).Text) = "790" Or (CType(dgWelStatus.Items(totalrow).FindControl("lblStatusid"), Label).Text) = "795" Or (CType(dgWelStatus.Items(totalrow).FindControl("lblStatusid"), Label).Text) = "797" Then
                    CType(dgWelStatus.Items(totalrow).FindControl("BtnAr_no"), Button).Enabled = False
                End If
            Next
End Sub

How to Remove Arrow Image from Asp Menu Control



<asp:Menu ID="IndexMenu" runat="server" BackColor="#0072BB" Font-Names="Verdana"
Font-Size="11px" ForeColor ="white" Height="10px" 
Orientation="Horizontal" Width="100%" Font-Bold =true  
staticEnableDefaultPopOutImage="false">

How to disable menus and Sub Menus in Asp Menus Controls

IndexMenu.Items(3).ChildItems(0).Enabled = False
IndexMenu.Items(3).ChildItems(1).Enabled = False
IndexMenu.Items(3).ChildItems(2).Enabled = False

Sunday 2 October 2011

The query has been canceled because the estimated cost of this query (302) : Solution

-- Some Times We feels that the query take more time to 
--to execute, and gives user error like given below:

The query has been canceled because the estimated cost of this query (302)

Solution:

CREATE procedure [dbo].[myProc]   
@email varchar(50),   
@password varchar(50)
as
-------------------------------------------------------------------------------------------------------
--when we get Error
--The query has been canceled because the estimated cost of this query (302),Contact your Administrator
SET QUERY_GOVERNOR_COST_LIMIT 0 -- is the important line
--------------------------------------------------------------------------------------------------------
select