Update data in Database in ASP.NET using C#


Posted in : ASP.Net Posted on : December 9, 2010 at 6:28 PM Comments : [ 0 ]
Advertisements
In this article we will introduce How can we Update data in Database in ASP.NET using C#

Update data in Database in ASP.NET using C#

In this example we select the employee name from Dropdown List. All the Textboxes will be fill according the record. Now you can change the Textboxes value. After that click update button. The data will be Updated successfully.

UpdateDatabase.aspx (Design page):

UpdateDatabase.aspx (Design page):

View source code Click Here

UpdateDatabase.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class UpdateDataBase : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
private string s;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropDownList();
}
}
// fill dropdownlist
public void FillDropDownList()
{
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
con = new SqlConnection(s);
con.Open();
cmd = new SqlCommand("Select employee_name from Employee", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr[0].ToString());
}
DropDownList1.Items.Insert(0, new ListItem("Select name for update record"));
dr.Close();
con.Close();
}
//fill all textboxes by selection of dropdownlist
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
con = new SqlConnection(s);
con.Open();
cmd = new SqlCommand("Select * from Employee where employee_name ='" + DropDownList1.SelectedItem.ToString() + "'", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
employee_id_Txt.Text = dr["employee_id"].ToString();
employeeName_Txt.Text = dr["employee_name"].ToString();
dob_Txt.Text = dr["dob"].ToString();
gender_Txt.Text = dr["gender"].ToString();
post_Txt.Text = dr["post"].ToString();
city_Txt.Text = dr["city"].ToString();
country_Txt.Text = dr["country"].ToString();
mobileno_Txt.Text = dr["mobileno"].ToString();
}
dr.Close();
con.Close();
}
// clear textboxes
public void clearData()
{
employee_id_Txt.Text = "";
employeeName_Txt.Text = "";
dob_Txt.Text = "";
gender_Txt.Text = "";
post_Txt.Text = "";
city_Txt.Text = "";
country_Txt.Text = "";
mobileno_Txt.Text = "";
DropDownList1.Items.Clear();
FillDropDownList();
}
// clear all the fields
protected void clear_Button_Click(object sender, EventArgs e)
{
employee_id_Txt.Text = "";
employeeName_Txt.Text = "";
dob_Txt.Text = "";
gender_Txt.Text = "";
post_Txt.Text = "";
city_Txt.Text = "";
country_Txt.Text = "";
mobileno_Txt.Text = "";
DropDownList1.SelectedIndex = 0;
Label1.Text = "";
}
//update record
protected void update_Button_Click(object sender, EventArgs e)
{
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
con = new SqlConnection(s);
con.Open();
cmd = new SqlCommand("update Employee set employee_name='" + employeeName_Txt.Text + "', dob='" + dob_Txt.Text + "', gender='" + gender_Txt.Text + "', post='" + post_Txt.Text + "', city='" + city_Txt.Text + "', country='" + country_Txt.Text + "', mobileno='" + mobileno_Txt.Text + "' where employee_id='" + employee_id_Txt.Text + "'", con);
cmd.ExecuteNonQuery();
Label1.Text = " Record Updated successfully";
clearData();
}
}

Output:

Select Employee Name from Dropdown List. All the Textboxes will be filled according to selected name.

Now mage change the field value and click update button. The data will be updated successfully.

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Tutorial Topics