VisualBuilder
  Home > Aspnet > Tutorials > Updating Records in Database - ASP.NET Database Tutorial
Tell a friend
Link to us
Total Members
      Members: 84657
     
Sitemap Forum Chat
Home
ASP.NET Database Tutorial Home
1 . Introduction to Asp.Net 2.0
2 . Introduction to ADO.Net
3 . Connecting Access Database with AccessDataSource and GridView Control
4 . Accessing SQL SERVER Database [Using SqlDataSource]
5 . Storing Connection String in Web.Config File
6 . Inserting Records in Database
7 . Updating Records in Database
8 . Updating Records in Database
9 . Deleting Records from Database
10 . Execute Scalar: Getting Single Value from the Database
11 . Execute Reader: Fetching Records from Database
12 . Data Adapter
13 . Data Adapter and Database Connection
14 . Performing Batch Updates: Using SqlCommandBuilder
15 . Improving Performance using Stored Procedure
16 . Filtering and Sorting Data Using DataView/DataTable
17 . Data Binding and Data Synchronization
18 . SQL Transaction and Locking
19 . SQL Joins and .Net
20 . Automatically Generation of SQL Statement
 
Aspnet Group Home
Aspnet Discussion (10)
Aspnet Members (2382)
Aspnet Resources
Aspnet Source Code (388)
Aspnet Articles (1)
Aspnet Blogs
Aspnet Jobs
Aspnet Components (201)
Aspnet Books
Aspnet Websites (21)
Aspnet News (105)
Aspnet Q & A (114)
- Aspnet Ask Question
- Aspnet Questions
- Aspnet Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (434 )
Source Code (3275 )
Articles (11 )
Components (1595 )
News (888 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Aspnet database Tutorial
 Updating Records in Database
  << Prev: Updating Records in Database Next: Deleting Records from Database >>

ASP.NET 2.0 makes it easy to create pages that allow a user to modify values in a database. The ease derives from the capabilities that are built into the data source and data-bound controls.




  • Data source controls have a built-in capability to modify data. You've already read about some built-in capabilities, such as sending a SELECT command to the database. ASP.NET 2.0 data source controls also have three capabilities to modify data: update, insert, and delete.




  • Data source controls have properties to enable the capability to modify data. The modification capabilities listed in the last point can be turned on or off.




  • Data-bound controls can automatically use capabilities that have been turned on in the data source control. Smart data-bound controls “know” which capabilities are turned on for their data source control. However, this sensing is only for smart controls: GridView , DetailsView , and FormView .




  • Data-bound controls have properties that enable use of their source control capability. The modification interface can be turned on or off by enabling smart data-bound controls to display options such as Edit or Delete.




 


Example: Demonstrate Updating record in the database.


 


UpdatingRecords.aspx


 


<% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="UpdatingRecords.aspx.cs" Inherits ="UpdatingRecords" %>


 


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 


<html xmlns ="http://www.w3.org/1999/xhtml" >


<head runat ="server">


</head >


<body >


<form id ="form1" runat ="server">


<div >


<strong >< em > Updating Record in the Database < br />


<br />Product Name : &nbsp;


<asp:DropDownList ID ="ddlProductName" runat ="server" AutoPostBack ="True">


</asp:DropDownList>


<br />


<br />Updated Product Name:


<asp:TextBox ID ="txtUpdatedPName" runat ="server"></asp:TextBox>


<br />


<br />


<asp:Button ID ="btnUpdate" runat ="server" OnClick ="btnUpdate_Click" Text ="Update Record !!!" />


<br/>


</em></strong>


</div >


</form >


</body >


</html >


 


 


UpdatingRecords.aspx.cs


 


using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


using System.Data.SqlClient;


 


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


{


  protected void Page_Load( object sender, EventArgs e)


    {


    if (!IsPostBack) {


    //Binding the data with the drop down list.


        String strConn = "Data Source=localhost;Initial Catalog=northwind;user id=sa;pwd=sa" ;


    //Instantiating Command Object.


        SqlConnection conn = new SqlConnection (strConn);


        SqlDataAdapter ada = new SqlDataAdapter ( "select * from products" , conn);


        DataSet dstProducts = new DataSet ();


        ada.Fill(dstProducts);


        ddlProductName.DataSource =dstProducts.Tables[0].DefaultView;


        ddlProductName.DataTextField = "productName" ;


        ddlProductName.DataValueField = "productId" ;


        ddlProductName.DataBind();


        }    


    }


 


  protected void btnUpdate_Click( object sender, EventArgs e)


    { //Update Record in the Database.


        String strConn = "Data Source=localhost;Initial Catalog=northwind;user id=sa;pwd=sa" ;


    //Instantiating Command Object.


        SqlConnection conn = new SqlConnection (strConn);


        SqlCommand cmd = new SqlCommand ();


        cmd.Connection = conn;


    string strQuery = "Update products set ProductName = @ProductName where ProductId= @ProductId" ;


        cmd.CommandText = strQuery;


        cmd.CommandType = CommandType .Text;


    //Adding Parameters to the Command Object.


        cmd.Parameters.AddWithValue( "@ProductName" ,


        txtUpdatedPName.Text);


        cmd.Parameters.AddWithValue( "@ProductId" ,ddlProductName.SelectedValue);


    //Connection Object Open


        conn.Open();


        cmd.ExecuteNonQuery();


    //Connection Object Closed


        conn.Close();


        Response.Write( "Updated One Record in the Database Successfully !!!!" );


    }


}


 


  << Prev: Updating Records in Database Next: Deleting Records from Database >>
Aspnet Database Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
mask for halloween
Video Surveillance
VoIP Internettelefonie DE
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising