Showing posts with label ASP.Net with Sql Server 2005. Show all posts
Showing posts with label ASP.Net with Sql Server 2005. Show all posts

Tuesday, February 14, 2012

Delete data from Sql Server database using ASP.Net

Delete data from Sql Server 2005 Database
Before dalete:-
 

After Delete:-
 
Step 1) Install and configure Sqlserver 2005
Step 2)Write following code into code behind of your ASP.NET page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 sqlserver : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rd;
    public string query;

    protected void connection()
    {
        con.ConnectionString = "server=.\\SQLEXPRESS; integrated security=SSPI; initial catalog=sourav;";
        con.Open();
        cmd.Connection = con;

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Calling connection function
        connection();

       //Change the tablename here

        cmd.CommandText = "delete from loginfo where username='aaa'";
        cmd.ExecuteReader();
       

    }
}

Update Sql Server data using ASP.NET


Update date of Sql Server 2005 database

Before update:-


After update:-




Step 1) Install and configure Sqlserver 2005

Step 2)Write following code into code behind of your ASP.NET page

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 sqlserver : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rd;
    public string query;

    protected void connection()
    {
        con.ConnectionString = "server=.\\SQLEXPRESS; integrated security=SSPI; initial catalog=sourav;";
        con.Open();
        cmd.Connection = con;

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Calling connection function
        connection();

        cmd.CommandText = "update loginfo set loginfo.username='aaa' where loginfo.password='xyz'";
        cmd.ExecuteReader();
       

    }
}

Display Sql Server data in Gridview

Displaying Sql Server data in Gridview


Step 1) Install and configure Sqlserver 2005

Step 2)Write following code to your ASP.NET page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sqlserver.aspx.cs" Inherits="sqlserver" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
            BorderStyle="None" BorderWidth="1px" CellPadding="4">
            <RowStyle BackColor="White" ForeColor="#003399" />
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

Step 4) Add following code in the code behind of your ASP.NET page
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 sqlserver : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rd;
    public string query;

    protected void connection()
    {
        con.ConnectionString = "server=.\\SQLEXPRESS; integrated security=SSPI; initial catalog=sourav;";
        con.Open();
        cmd.Connection = con;

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Calling connection function
        connection();

        cmd.CommandText = "select * from loginfo";
        rd = cmd.ExecuteReader();
        this.GridView1.DataSource = rd;
        this.GridView1.DataBind();


    }
}

Insert data in Sql Server 2005 using ASP.Net

Insert data in Sql server 2005

After insert:-


Step 1) Install and configure Sqlserver 2005
Step 2)Write following code to fetch data from database
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 sqlserver : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    public string query;

    protected void connection()
    {
        con.ConnectionString = "server=.\\SQLEXPRESS; integrated security=SSPI; initial catalog=sourav;";
        con.Open();
        cmd.Connection = con;

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Calling connection function
         connection();

        //Change below line according to your table  and data
    
        cmd.CommandText = "insert into loginfo values('abc','xyz')";
        cmd.ExecuteReader();
       
    }
}

Data read from Sql Server 2005 using ASP.Net

Read data from Sql server 2005 Database



Step 1) Install and configure Sqlserver 2005

Step 2)Write following code to fetch data from database

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 sqlserver : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rd;
    public string query;

    protected void connection()
    {
        con.ConnectionString = "server=.\\SQLEXPRESS; integrated security=SSPI; initial  catalog=sourav;";
      //Initial catalog =sourav  is my database name.

        con.Open();
        cmd.Connection = con;

    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        //Calling connection function
        connection();

        cmd.CommandText = "select * from tablename";
        // Change table name according to your setting
        rd = cmd.ExecuteReader();
        if (rd.Read())
        {
            Response.Write(rd.GetSqlValue(0).ToString());
            Response.Write(rd.GetSqlValue(1).ToString());
        }
        rd.Close();
    }
}


N.B:- don’t forget to change server name , database name and table name according to your setting.