Skip to main content

Practical-21

Perform insert and update operation in Database

In this practical, we will learn how to perform insert and update operations in a database using ASP.NET.

Step 1: Create a new ASP.NET Web Application project

  1. Open Visual Studio and create a new ASP.NET Web Application project.
  2. Choose the Empty template and click OK.

Step 2: Add a SQL Server Database to the project

  1. Right-click on the project in the Solution Explorer and select Add -> New Item.
  2. Choose the SQL Server Database template and click Add.
  3. Name the database file as "Databasef" and click OK.
  4. The database file will be added to the project.

Step 3: Design the web form

  1. Open the default.aspx file in the Solution Explorer.
  2. Switch to the Design view.
  3. Design the layout of the web form by adding HTML elements, CSS styles, and form controls such as textboxes, buttons, etc.

Step 4: Configure the database connection

  1. In the code-behind file (default.aspx.cs), write the necessary code to establish a connection to the database.
  2. Use the SqlConnection class to create a connection object and set the connection string to the database file.

Step 5: Perform insert operation

  1. Write the necessary code to insert data into the database.
  2. Use the SqlCommand class to execute an INSERT statement.
  3. Retrieve the values from the form controls and pass them as parameters to the INSERT statement.

Step 6: Perform update operation

  1. Write the necessary code to update data in the database.
  2. Use the SqlCommand class to execute an UPDATE statement.
  3. Retrieve the values from the form controls and pass them as parameters to the UPDATE statement.

Step 7: Run the application

  1. Press F5 or click the Start Debugging button to run the application.
  2. Fill in the form with the required data and click the Insert or Update button.
  3. The data will be inserted or updated in the database.

That's it! You have successfully performed insert and update operations in a database using ASP.NET.

Make sure to save your changes and submit this practical in your notebook.