Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.Drawing;
namespace logindemo
{
    public partial class Welcometo : System.Web.UI.Page
    {
        private SqlConnection conn = new SqlConnection("Data Source=INFND-DTP-1111 Initial Catalog=Test_DB;User ID=alok;Password=abcd@1234");

        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();

        }
        protected void BindData()
        {

            conn.Open();
            SqlCommand cmd = new SqlCommand("Select * from login", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            conn.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }



        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
            conn.Open();

            SqlCommand cmd = new SqlCommand("delete FROM login where User_id=" + Id, conn);

            cmd.ExecuteNonQuery();

            conn.Close();
            BindData();
        }

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

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int user_id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            TextBox txtUser_ID = (TextBox)row.Cells[0].Controls[0];
            TextBox txtUserName = (TextBox)row.Cells[1].Controls[0];
            TextBox txtPassword = (TextBox)row.Cells[2].Controls[0];
            TextBox txtAddress = (TextBox)row.Cells[3].Controls[0];
            TextBox txtCity = (TextBox)row.Cells[4].Controls[0];
            TextBox txtState = (TextBox)row.Cells[5].Controls[0];
            TextBox txtPIN= (TextBox)row.Cells[6].Controls[0];
            TextBox txtPhoneNo = (TextBox)row.Cells[7].Controls[0];
            TextBox txtEmailID = (TextBox)row.Cells[8].Controls[0];
            GridView1.EditIndex = -1;
            conn.Open();
            SqlCommand cmd = new SqlCommand("update login set User_name='" + txtUserName.Text + "',Password='" + txtPassword.Text + "',Address='"
                + txtAddress.Text + "',City='"+txtCity.Text + "',State='"+ txtState.Text + "',PIN="+ Convert.ToDouble(txtPIN.Text) +",Phone_no="
                + Convert.ToDouble(txtPhoneNo.Text) + ", Email_ID='" + txtEmailID.Text + "' Where User_id='" + txtUser_ID + "'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            BindData();
         }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        
    }
}
Posted
Updated 17-Mar-15 23:04pm
v2
Comments
Reshma Babu 18-Mar-15 5:28am    
Try this:
GridView1.DataSource = ds.Tables[0];
.net bigner 18-Mar-15 5:31am    
Where will i use that?
Reshma Babu 18-Mar-15 5:32am    
In BindData()
.net bigner 18-Mar-15 8:21am    
thanx a lot...
Reshma Babu 18-Mar-15 8:37am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900