Grid View Commands Select, Edit, Update, Cancel Example

0 comments

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;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Initial Catalog=DB;Data Source=JAIMATARANI;User ID=sa;Password=123;Min Pool Size=5;Max Pool Size=60;Connect Timeout=2");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    //Bind Grid View
    protected void BindGrid()
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * from Student", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "T1");
        GridView1.DataSource = ds.Tables["T1"];
        GridView1.DataBind();
    }
  //Edit
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
     
        BindGrid();
    }
    //Cancel
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }
    //Update
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlCommand cmd = new SqlCommand(@"Update Student Set
EnrollmentNo='" + (((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text) + @"',
Name='" + (((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text) + @"',
Programme='" + (((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text) + @"',
Address='" + (((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text) + @"',
Mobile='" + (((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text) + @"'
where SL_No='" + (((TextBox)GridView1.Rows[e.RowIndex].Cells [0].Controls[0]).Text) + @"'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        BindGrid();
    }
    //Delete
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("Delete from Student where SL_No='"+GridView1 .Rows [e.RowIndex ].Cells [0].Text +"'", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindGrid();
    }
    //Select
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * from Student where SL_No='" + GridView1 .SelectedRow .Cells [0].Text  + "' ", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
    }
}

Send Mail in Asp.Net using GMAIL server

0 comments

using System;
using System.Configuration;
using System.Data;
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 ;
using System.Net .Mail ;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         SmtpClient client = new SmtpClient();

       client.DeliveryMethod = SmtpDeliveryMethod.Network;

       client.EnableSsl = true;

       client.Host = "smtp.gmail.com";

       client.Port = 587;



       // setup Smtp authentication

       System.Net.NetworkCredential credentials =
           new System.Net.NetworkCredential("email id",
"password");

       client.UseDefaultCredentials = false;

       client.Credentials = credentials;



       MailMessage msg = new MailMessage();

       msg.From = new MailAddress("email id");

       msg.To.Add(new MailAddress("email id"));



       msg.Subject = "This is a test Email subject";

       msg.IsBodyHtml = true;

       msg.Body = string.Format("Test HTML Email");



       try
       {

           client.Send(msg);

           lblMsg.Text = "Your message has been successfully sent.";

       }

       catch (Exception ex)
       {

           // lblMsg.ForeColor = Color.Red;

           lblMsg.Text = "Error occured while sending your message." +
ex.Message;

       }
    }
}

Sql query

0 comments

--To create a database
Create database DB
--To select database
Use DB

--To drop(delete) database
Drop database DB

--To create table
Create table Student
(
EnrollmentNo int,
Name varchar(30),
Programme varchar(15),
Address varchar(50),
Mobile varchar(10)
)


--To drop(delete) table
Drop table Student

--To truncate table
Truncate table Student

--To insert record into table
Insert into Student(EnrollmentNo,Name,Programme,Address,Mobile)values(073550123,'Praveen','MCA','WZ-16-C Possangipur Janakpuri New Delhi -58','9540530654')
Insert into Student(EnrollmentNo,Name,Programme,Address,Mobile)values(073550116,'Rakesh','MCA','WZ-16-C Possangipur Janakpuri New Delhi -58','921234567')
Insert into Student(EnrollmentNo,Name,Programme,Address,Mobile)values(073550123,'Pankaj','BCA','A2 A/82 Janakpuri New Delhi -58','9540530654')
Insert into Student(EnrollmentNo,Name,Programme,Address,Mobile)values(059550123,'Jatindar','BCA','Uttam Nagar New Delhi -45','9540530654')



--To display record
Select * from Student

--To display specified field
Select EnrollmentNo,Name,Programme,Address,Mobile from Student

--Where Clause
-------------
Select EnrollmentNo,Name,Programme,Address,Mobile from Student where EnrollmentNo='73550123'

Logical Operator
---------------
AND
Or
Not

--AND
Select EnrollmentNo,Name,Programme,Address,Mobile from Student where Name='Praveen' and Programme='MCA'
The above statement display records whose name is praveen and programme is MCA

--OR
Select EnrollmentNo,Name,Programme,Address,Mobile from Student where Name='Praveen' or Programme='MCA'
The above statement display records whose name is praveen or either programme is MCA

--Not
Select EnrollmentNo,Name,Programme,Address,Mobile from Student where Name !='Praveen'
The above statement display records whose name is not praveen

--To Create table

Create table StudentDetails
(
EnrollmentNo int,
CourseFee decimal(18,2),
Duration varchar(10)
)
-- To insert record
Insert into StudentDetails(EnrollmentNo,CourseFee,Duration) values(073550123,'50000.00','3 Years')
Insert into StudentDetails(EnrollmentNo,CourseFee,Duration) values(073550116,'50000.00','3 Years')
Insert into StudentDetails(EnrollmentNo,CourseFee,Duration) values(073550123,'30000.00','3 Years')
Insert into StudentDetails(EnrollmentNo,CourseFee,Duration) values(073551111,'50000.00','2 Years')
Insert into StudentDetails(EnrollmentNo,CourseFee,Duration) values(073550222,'100000.00','2 Years')
--To update record
Update Student set EnrollmentNo='603550123',Mobile='9811574400' where Name='Pankaj'
Update StudentDetails Set EnrollmentNo='603550123' where CourseFee='30000.00'

--Join
--Inner Join

Select S.EnrollmentNo,S.Name,S.Address,S.Programme,S.Mobile,SD.CourseFee,SD.Duration
from Student S
Inner Join StudentDetails SD on S.EnrollmentNo=SD.EnrollmentNo

-- Left Join
Select S.EnrollmentNo,S.Name,S.Address,S.Programme,S.Mobile,SD.CourseFee,SD.Duration
from Student S
Left Join StudentDetails SD on S.EnrollmentNo=SD.EnrollmentNo

--Right Join
Select S.EnrollmentNo,S.Name,S.Address,S.Programme,S.Mobile,SD.CourseFee,SD.Duration
from Student S
Right Join StudentDetails SD on S.EnrollmentNo=SD.EnrollmentNo

--Full Join
Select S.EnrollmentNo,S.Name,S.Address,S.Programme,S.Mobile,SD.CourseFee,SD.Duration
from Student S
Full Join StudentDetails SD on S.EnrollmentNo=SD.EnrollmentNo

--Aggregate Function
--Sum()
Select Sum(CourseFee)CourseFee from StudentDetails
--Avg()
Select Avg(CourseFee)CourseFee from StudentDetails
--Count()
Select Count(CourseFee)CourseFee from StudentDetails
To delete record from table

Select * from Student
Select * from StudentDetails

-- To insert record
--To Create table
Create Table SqlTutorial
(
Id int identity,
CommandName varchar(50),
Query Varchar(2000),
Description varchar(3000)
)


Multiple selection in Check box list

0 comments

CodeFile
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;

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


    protected void Button1_Click(object sender, EventArgs e)
    {
        String str = "";

        if (CheckBoxList1.Items[0].Selected == true)
            str = str + CheckBoxList1.Items[0];
        if (CheckBoxList1.Items[1].Selected == true)
            str = str + CheckBoxList1.Items[1];
        if (CheckBoxList1.Items[2].Selected == true)
            str = str + CheckBoxList1.Items[2];
        if (CheckBoxList1.Items[3].Selected == true)
            str = str + CheckBoxList1.Items[3];
        if (CheckBoxList1.Items[4].Selected == true)
            str = str + CheckBoxList1.Items[4];
        if (CheckBoxList1.Items[5].Selected == true)
            str = str + CheckBoxList1.Items[5];

        Response.Write("You have selected" + " " + str);
    }
}

CHECK BOX LIST BINDING FROM DATA BASE

0 comments

CodeFile

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;

public partial class CheckBoxList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Trusted_Connection=true;DataBase=Praveen_DataBase");
        SqlDataAdapter da = new SqlDataAdapter("Select * from Table1", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        CheckBoxList1.DataSource = ds;
        CheckBoxList1.DataTextField = "Name";
        CheckBoxList1.DataBind();
    }
}

What is a Class?

0 comments

A class is simply a representation of a type of object; think of it as the object's blueprint. Just as a single blueprint can be used to build multiple buildings, a single negative of photographs can be used to print many photos. So a class can be used to create multiple copies of an object. It is the base for the all object oriented programming concepts.


 In everyday programming life, we use this concept throughout our Asp.Net applications. Obviously we are not realizing that we are using these classes from creating a simple Label control to complex WebServices. For example, the TextBox control is defined by a TextBox class, which defines its appearance and its capabilities. Each time you drag a TextBox control onto a webpage, you are actually creating a new instance of the TextBox class. Sometimes you need to create a TextBox from your code-behind, which can be achieved only by creating an object from the TextBox Class as follows 

TextBox TextBox1 = new TextBox();
TextBox1.ID="TextBox1";

When you create an object to the class, all the properties and methods can be accessed by the object or in other word we can say as instance. On the other hand, you can use a class only if you create an object to it. For example, if you consider a Car as a Class, then you need a driver to drive the Car. So the driver can access all the properties, functionalities and methods of the Car.

Asp.net DropDownList Server Control

0 comments

DropDownList Control

A DropDownList is also commonly known as combo box. It can contain multiple data members, but unlike a normal list box the users can choose only one value from this control. Though the functionality of this DropDownList is much like a Single Row Select List Box, a DropDownList can save a lot of GUI space as it is rendered on a Single line and is expanded only when the user clicks on the Control.
   This DropDownList is provided as a Server Control in ASP .Net like many other controls. This DropDownList can be used to add data manually or even for dynamic binding with data base.

Example first

Example 1st:
Adding data in DropDownList using property(Items collection)

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        <asp:ListItem>---Select city---asp:ListItem>
        <asp:ListItem>Madhubaniasp:ListItem>
        <asp:ListItem>Darbhangaasp:ListItem>
        <asp:ListItem>New Delhiasp:ListItem>
    asp:DropDownList>

Example 2nd :

Adding data in DropDownList using DataBind(database)
Source code
<asp:DropDownList ID="DropDownList2" runat="server">
        asp:DropDownList>
Code File
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Trusted_connection=true;database=pubs");
        SqlDataAdapter da = new SqlDataAdapter("Select * from authors",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DropDownList2.DataSource = ds;
        DropDownList2.DataTextField = "city";
        DropDownList2.DataBind();
    }
Example 3rd :
Source code]

<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
            <asp:ListItem>---Select city---asp:ListItem>
            <asp:ListItem>Madhubaniasp:ListItem>
            <asp:ListItem>Darbhangaasp:ListItem>
            <asp:ListItem>New Delhiasp:ListItem>
        asp:DropDownList>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Ok"
            Width="96px" />
        <asp:Label ID="Label1" runat="server">asp:Label>

Code File
protected void Button1_Click(object sender, EventArgs e)
    {
       Label1 .Text ="You have selected "+ DropDownList3.SelectedItem.Text;
    }

Example 4th :

Source code
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList4_SelectedIndexChanged">
            <asp:ListItem>---Select city---asp:ListItem>
            <asp:ListItem>Madhubaniasp:ListItem>
            <asp:ListItem>Darbhangaasp:ListItem>
            <asp:ListItem>New Delhiasp:ListItem>
        asp:DropDownList>
        <asp:Label ID="Label2" runat="server">asp:Label>

Code File

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label2.Text = "You have selected " + DropDownList4.SelectedItem.Text;
    }