Search This Blog

Saturday, November 5, 2011

Grid View .aspx.cs Page


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;

namespace demo_databaseconnection
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill_grid();
            }
           
        }
        void fill_grid()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from tbl_reg",con1);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void btn_insert_Click(object sender, EventArgs e)
        {
            SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values(@user,@pass)",con1);
            cmd1.Parameters.AddWithValue("@user",txtusername.Text);
            cmd1.Parameters.AddWithValue("@pass",txtpassword.Text);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            lblmessage.Text = "Record inserted successfully !!!";
            txtusername.Text = "";
            txtpassword.Text = "";
            fill_grid();
        }

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

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

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id1=int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            SqlCommand cmd1 = new SqlCommand("delete from tbl_reg where id = @id1",con1);
            cmd1.Parameters.AddWithValue("@id1",id1);          
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            lblmessage.Text = "The record successfully deleted from Database.";
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox t_username = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_username");
            TextBox t_password = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_password");
            int id1 = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set                                  username=@username,password=@password where id=@id1",con1);
            cmd1.Parameters.AddWithValue("@username",t_username.Text);
            cmd1.Parameters.AddWithValue("@password",t_password.Text);
            cmd1.Parameters.AddWithValue("@id1",id1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            GridView1.EditIndex = -1;
            fill_grid();
            lblmessage.Text = "The record successfully Updated in the Database.";

        }

        protected void insert_Click(object sender, EventArgs e)
        {
            TextBox t_user = (TextBox)GridView1.HeaderRow.FindControl("txt_user");
            TextBox t_pass = (TextBox)GridView1.HeaderRow.FindControl("txt_pass");
            SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values(@username,@password)",con1);
            cmd1.Parameters.AddWithValue("@username",t_user.Text);
            cmd1.Parameters.AddWithValue("@password",t_pass.Text);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            lblmessage.Text = "Record inserted successfully";
        }

        protected void update_Click(object sender, EventArgs e)
        {
            TextBox t_username = (TextBox)GridView1.FooterRow.FindControl("t_user");
            TextBox t_password = (TextBox)GridView1.FooterRow.FindControl("t_pass");
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set username=@username,password=@password where id=@id1");
           
        }
    }
}

No comments:

Post a Comment