Categories: Uncategorized
Tags:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

 

Once you have created the gridview and connected with the Database, click on the arrow in gridview and select edit columns,

As shown in the image select Comandfield and Add >> Edit, Update, Cancel

Make Buttontype = Button to make it button

Now Right Click on the same arrow and click properties

In properties >> Datakeynames >> Write >> user_id or whatever is your primary key in the table

data_key_properties

Now Click on Events

Click_events

In Events Double Click on Row Editing

events_row_editing

Write GridView1.EditIndex = e.NewEditIndex;

Save

using System.Data.SqlClient;

string cs = @”Data Source=DESKTOP-N\SQLEXPRESS;Initial Catalog=doctopsdb;Integrated Security=True”; using (SqlConnection con = new SqlConnection(cs)) { con.Open(); // Perform database operations here }


Method 2 :

<%@ Page Title=”” Language=”C#” MasterPageFile=”~/Site.Master” AutoEventWireup=”true” CodeBehind=”ViewUsers.aspx.cs” Inherits=”DoctorPS.WebForm1″ %>
<asp:Content ID=”Content1″ ContentPlaceHolderID=”MainContent” runat=”server”>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataKeyNames=”user_id”
OnRowEditing=”GridView1_RowEditing” OnRowUpdating=”GridView1_RowUpdating” OnRowCancelingEdit=”GridView1_RowCancelingEdit”>
<Columns>

<asp:TemplateField HeaderText=”Username”>
<EditItemTemplate>
<asp:TextBox ID=”usernameTextBox” runat=”server” Text='<%# Bind(“username”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”usernameLabel” runat=”server” Text='<%# Bind(“username”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Email”>
<EditItemTemplate>
<asp:TextBox ID=”emailTextBox” runat=”server” Text='<%# Bind(“email”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”emailLabel” runat=”server” Text='<%# Bind(“email”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Phone”>
<EditItemTemplate>
<asp:TextBox ID=”phoneTextBox” runat=”server” Text='<%# Bind(“phone”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”phoneLabel” runat=”server” Text='<%# Bind(“phone”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Area”>
<EditItemTemplate>
<asp:TextBox ID=”areaTextBox” runat=”server” Text='<%# Bind(“area”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”areaLabel” runat=”server” Text='<%# Bind(“area”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”City”>
<EditItemTemplate>
<asp:TextBox ID=”cityTextBox” runat=”server” Text='<%# Bind(“city”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”cityLabel” runat=”server” Text='<%# Bind(“city”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Country”>
<EditItemTemplate>
<asp:TextBox ID=”countryTextBox” runat=”server” Text='<%# Bind(“country”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”countryLabel” runat=”server” Text='<%# Bind(“country”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Postal Code”>
<EditItemTemplate>
<asp:TextBox ID=”postalcodeTextBox” runat=”server” Text='<%# Bind(“postalcode”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”postalcodeLabel” runat=”server” Text='<%# Bind(“postalcode”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Full Name”>
<EditItemTemplate>
<asp:TextBox ID=”fullnameTextBox” runat=”server” Text='<%# Bind(“fullname”) %>’ />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID=”fullnameLabel” runat=”server” Text='<%# Bind(“fullname”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField ShowEditButton=”True” />
</Columns>
</asp:GridView>

</asp:Content>

 

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DoctorPS
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}

}

private void BindGridView()
{
string cs = “Data Source=DESKTOP-R22567N\\SQLEXPRESS;Initial Catalog=doctopsdb;Integrated Security=True”;

using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(“SELECT * FROM user_master”, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; // Set the row to edit mode
BindGridView(); // Rebind the GridView to refresh the view
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; // Cancel edit mode
BindGridView(); // Rebind the GridView
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Get the primary key (user_id)
int user_id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

// Use FindControl to find the controls in the GridView row
TextBox usernameTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“usernameTextBox”);
TextBox emailTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“emailTextBox”);
TextBox phoneTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“phoneTextBox”);
TextBox areaTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“areaTextBox”);
TextBox cityTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“cityTextBox”);
TextBox countryTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“countryTextBox”);
TextBox postalcodeTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“postalcodeTextBox”);
TextBox fullnameTextBox = (TextBox)GridView1.Rows[e.RowIndex].FindControl(“fullnameTextBox”);

string username = usernameTextBox.Text;
string email = emailTextBox.Text;
string phone = phoneTextBox.Text;
string area = areaTextBox.Text;
string city = cityTextBox.Text;
string country = countryTextBox.Text;
string postalcode = postalcodeTextBox.Text;
string fullname = fullnameTextBox.Text;

string cs = “Data Source=DESKTOP-R22567N\\SQLEXPRESS;Initial Catalog=doctopsdb;Integrated Security=True”;

using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand(“UPDATE user_master SET username=@username, email=@Email, phone=@Phone, area=@Area, city=@City, country=@Country, postalcode=@PostalCode, fullname=@FullName WHERE user_id=@UserId”, con);

cmd.Parameters.AddWithValue(“@username”, username);
cmd.Parameters.AddWithValue(“@Email”, email);
cmd.Parameters.AddWithValue(“@Phone”, phone);
cmd.Parameters.AddWithValue(“@Area”, area);
cmd.Parameters.AddWithValue(“@City”, city);
cmd.Parameters.AddWithValue(“@Country”, country);
cmd.Parameters.AddWithValue(“@PostalCode”, postalcode);
cmd.Parameters.AddWithValue(“@FullName”, fullname);
cmd.Parameters.AddWithValue(“@UserId”, user_id);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

// Exit edit mode
GridView1.EditIndex = -1;
BindGridView(); // Rebind the GridView to show updated data
}

 

}
}