Updation of records in asp.net using C#

In this tutorial we will learn how to perform the updation of records in asp.net using C#. First of all we will show all records coming from database in gridview and then using gridview’s built-in event RowEditing we will perform updation of records in asp.net using C#. To perform editing/updation of records in asp.net using C# is quite easy.

 

editing_records_in_gridview.aspx

<asp:gridview id="GridView1" runat="server" width="100%">

<Columns>

<asp:TemplateField HeaderText="ID" >
<ItemTemplate>
<asp:Label ID="lbl_UserID" Text='<%# Bind("User_ID")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" CssClass="aLinkButton" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Delete" >                       

<ItemTemplate>

<asp:Label ID="lbl_delete" Visible="false"  Text='<%# Bind("User_ID")%>' runat="server"></asp:Label>                        

<asp:ImageButton runat="server" ID="img" ImageUrl="images/b_drop.png" CommandName="Delete"  />

</ItemTemplate>                        

</asp:TemplateField>
</Columns>
</asp:gridview>

editing_records_in_gridview.aspx.cs

using System;

using System.Collections;
using System.Configuration;
using System.Data;

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.Data.SqlClient;


public partial class Tutorials_editing_records_in_gridview : System.Web.UI.Page
{
string strcon = "server=localhost;database=tutorials-db; uid=adeel;password=pakistan; Connect Timeout=200; Max Pool Size=5000"; 

SqlConnection con = new SqlConnection(); 

SqlCommand com = new SqlCommand(); 

DataSet ds = new DataSet(); 

SqlDataAdapter da; 

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
} 

private void bindgrid()
{
ds.Clear();
con.ConnectionString = strcon; 

com.Connection = con;

con.Open();

string strQuery = "select * from users"; 

da = new SqlDataAdapter(strQuery,con);

da.Fill(ds); 

GridView1.DataSource = ds;

GridView1.DataBind();

con.Close();
}

 

//Editing of records in asp.net using C#

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        int index = e.NewEditIndex;

        GridViewRow row = GridView1.Rows[index];

        Label lbl = (Label)row.FindControl("lbl_edit");

        string id = lbl.Text;

        Response.Redirect("edit_records.aspx?UserID="+id+"");

 

    }

// Editing of records function ends here

}

 

edit_records.aspx

   <table>

        <tr>

            <td>

                Email

            </td>

            <td>

                <asp:TextBox ID="txt_Email" runat="server" EnableTheming="False"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                First Name

            </td>

            <td>

                <asp:TextBox ID="txt_FirstName" runat="server" EnableTheming="False"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Last Name

            </td>

            <td>

                <asp:TextBox ID="txt_LastName" runat="server" EnableTheming="False"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

               

            </td>

            <td>

                <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />

            </td>

        </tr>

    </table>

 

edit_records.aspx.cs

 

public partial class Tutorials_edit_records : System.Web.UI.Page

{

string strcon = "server=localhost;database=tutorials-db; uid=adeel;password=pakistan; Connect Timeout=200; Max Pool Size=5000";     SqlConnection con = new SqlConnection();

    SqlCommand com = new SqlCommand();

    DataSet ds = new DataSet();

    SqlDataAdapter da;

    string UserID;

 

    protected void Page_Load(object sender, EventArgs e)

    {

        UserID = Request.QueryString["UserID"].ToString();

 

        if (!IsPostBack)

        {

            string strQuery = "select User_Email,User_First_Name,User_Last_Name from users where User_ID='" + UserID + "'";

            con.ConnectionString = strcon;

            com.Connection = con;

            com.CommandText = strQuery;

            con.Open();

            SqlDataReader rdr = com.ExecuteReader();

            while (rdr.Read())

            {

                txt_Email.Text = rdr.GetValue(0).ToString();

                txt_FirstName.Text = rdr.GetValue(1).ToString();

                txt_LastName.Text = rdr.GetValue(2).ToString();

            }

            con.Close();

        }

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        UserID = Request.QueryString["UserID"].ToString();

        string strUpdateQuery = "update users SET User_Email='" + txt_Email.Text + "', User_First_Name='" + txt_FirstName.Text + "', User_Last_Name='" + txt_LastName.Text WHERE User_ID='" + UserID + "'";

        con.ConnectionString = strcon;

        com.Connection = con;

        com.CommandText = strUpdateQuery;

        con.Open();

        com.ExecuteNonQuery();

        con.Close();

 

    }

}

So this is the way to perform editing of data/records in asp.net using C#

Happy Coding, Keep Coding…!

 

 

0 comments: