ASP.NET - SPLessons

ASP.NET CRUD Database

Home > Lesson > Chapter 22
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.NET CRUD Database

ASP.NET CRUD Database

shape Description

There are two types of architectures exist for database connection.
  1. Connected Data Access Architecture
  2. Disconnected Data Access Architecture
If one need to show the data, go for 'SELECT' SQL statement. For this statement mostly, one can use SqlDataAdapter class. See sample code below for this operation. [csharp]protected void getGridData() { SqlConnection spCon = new SqlConnection("Data Source=abc;Integrated Security=true;Initial Catalog=Test"); spCon.Open(); SqlCommand spCmd = new SqlCommand("Select UserName, First Name,LastName,Location FROM Students", spCon); SqlDataAdapter spDa = new SqlDataAdapter(spCmd); DataSet spDs = new DataSet(); spDa.Fill(spDs); spGridView.DataSource = spDs; spGridView.DataBind(); }[/csharp] In the above code, used DataSet as data source for gridview and one can use DataTable instead of DataSet. If one take DataTable, there will be a little bit code change after creation of SqlDataAdapter object like below. [csharp]DataTable spDt = new DataTable(); spDa.Fill(spDt); spGridView.DataSource = spDt; spGridView.DataBind(); [/csharp] There are also few cases, where one can update the data to in the database using buttons or other controls from front end of an application. One may insert, a new record to the database or can delete some data. SqlCommand, SqlDataReader classes are used for this type of operations.

How to write INSERT, SELECT, UPDATE and DELETE queries in ASP.NET.

shape Example

Let's look into the below sample codes.
  • How to INSERT a record in a Database in ASP.NET [csharp]protected void Submit_Click() {Sqlconnection con=new Sqlconnection(Connectionstring); string insertSQL="INSERT INTO User(UserName, FirstName, LastName, Location) VALUES('" + sptxtUserName.Text + "','" + sptxtFirstName.Text + "','" + sptxtLastName.Text + "','" + sptxtLocation.Text + "')" SqlCommand cmd=new SqlCommand(insertSQL,con); con.open(); cmd.ExecuteNonQuery(); con.close(); }[/csharp] In the above example, inserted the new data in database with which is coming from front end of the application.
  • How to SELECT a record in a Database in ASP.NET [csharp]protected void Submit_Click() {Sqlconnection con=new Sqlconnection(Connectionstring); string selectSQL="SELECT * FROM User"; SqlCommand cmd=new SqlCommand(selectSQL,con); con.open(); cmd.ExecuteNonQuery(); con.close(); }[/csharp] In the above example, have fetched the data which is coming from database for that application.
  • How to UPDATE a record in a Database in ASP.NET [csharp]protected void Submit_Click() { string updateSQL; updateSQL = "UPDATE Students SET "; updateSQL += "UserName=@UserName, FirstName=@FirstName, LastName=@LastName, Location=@Location"; updateSQL += "WHERE Student_id=@Student_id_Registered"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(updateSQL, con); cmd.Parameters.AddWithValue("@UserName", sptxtUserName.Text); cmd.Parameters.AddWithValue("@FirstName", sptxtFirstName.Text); cmd.Parameters.AddWithValue("@LastName", sptxtLastName.Text); cmd.Parameters.AddWithValue("@Location", sptxtLocation.Text); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } [/csharp] In the above example, have updated the data in database with the new data coming from front end of the application.
  • How to DELETE a record in a Database in ASP.NET [csharp]protected void Submit_Click() {Sqlconnection con=new Sqlconnection(Connectionstring); string deleteSQL="DELETE FROM User WHERE UserName='" + sptxtUserName.Text + "'"; SqlCommand cmd=new SqlCommand(deleteSQL,con); con.open(); cmd.ExecuteNonQuery(); con.close(); }[/csharp] In the above example, have deleted the data from database.