GridView CRUD Operations using Oracle DB in ASP.NET WebForms (C#)

Overview

This documentation explains how to implement CRUD operations (Create, Read, Update, Delete) using a GridView control in ASP.NET WebForms integrated with an Oracle Database. The example uses Oracle.ManagedDataAccess.Client for database interaction and displays client information in a Bootstrap-styled GridView.

Technologies Used

Technology Description
ASP.NET WebForms For building UI and handling events
C# Server-side logic
Oracle DB Backend relational database
Oracle.ManagedDataAccess Oracle .NET driver for DB connectivity
Bootstrap 5 Styling the GridView for better UI

Project Structure

  • Gridview.aspx: Frontend HTML + GridView definition.
  • Gridview.aspx.cs: Code-behind C# file containing event handlers and DB operations.
  • Web.config: Contains Oracle connection string.
Connection String (Web.config)

Ensure you have the connection string in your Web.config.

<connectionStrings>
  <add name="OracleCon" 
       connectionString="User Id=your_user;Password=your_password;Data Source=your_data_source" 
       providerName="System.Data.OracleClient" />
</connectionStrings>

GridView.aspx (UI Design)

Features

  • Binds data from the ucc_master table
  • Supports Edit, Update, and Delete operations
  • Displays: UCC, NAME, PAN, Email, Family

GridView Markup

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
              CssClass="table table-bordered table-striped table-hover"
              HeaderStyle-CssClass="table-primary"
              DataKeyNames="UCC"
              OnRowEditing="GridView1_RowEditing"
              OnRowUpdating="GridView1_RowUpdating"
              OnRowCancelingEdit="GridView1_RowCancelingEdit"
              OnRowDeleting="GridView1_RowDeleting">

    <Columns>
        <asp:BoundField DataField="UCC" HeaderText="Client ID" ReadOnly="True" />
        <asp:BoundField DataField="NAME" HeaderText="Name" />
        <asp:BoundField DataField="pan" HeaderText="PAN NO" />
        <asp:BoundField DataField="Email" HeaderText="Email ID" />
        <asp:BoundField DataField="Family" HeaderText="Family" />
        <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
    </Columns>

</asp:GridView>
GridView

Gridview.aspx.cs (Backend Logic)

Backend Logic

Grid

Gridview

Gridview5

Page_Load

Loads data initially on non-postback.

// Loads data initially on non-postback.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}

BindGrid

Fetches records from ucc_master and binds to GridView.

private void BindGrid()
{
    using (OracleConnection conn = new OracleConnection(connStr))
    {
        OracleDataAdapter da = new OracleDataAdapter(
            "SELECT UCC, NAME, pan, Email, Family FROM ucc_master", conn);
        
        DataTable dt = new DataTable();
        da.Fill(dt);
        
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Editing and Updating

RowEditing

Sets edit mode for the selected row.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindGrid();
}

RowUpdating

Updates edited row in Oracle DB.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string ucc = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
    string pan = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();
    string email = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
    string family = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();

    using (OracleConnection conn = new OracleConnection(connStr))
    {
        conn.Open();
        string query = "UPDATE ucc_master SET NAME = :name, PAN = :pan, EMAIL = :email, FAMILY = :family WHERE UCC = :ucc";

        using (OracleCommand cmd = new OracleCommand(query, conn))
        {
            cmd.Parameters.Add("name", name);
            cmd.Parameters.Add("pan", pan);
            cmd.Parameters.Add("email", email);
            cmd.Parameters.Add("family", family);
            cmd.Parameters.Add("ucc", ucc);

            cmd.ExecuteNonQuery();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "success", "alert('Row Updated Successfully.');", true);
        }
    }

    GridView1.EditIndex = -1;
    BindGrid();
}

Cancel Edit

Cancels edit mode and reloads original data.

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindGrid();
}

Row Deleting

Deletes a row using the srno field (you should clarify if UCC or srno is the primary key).

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int srno = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());

    using (OracleConnection conn = new OracleConnection(connStr))
    {
        conn.Open();
        OracleCommand cmd = new OracleCommand("DELETE FROM ucc_master WHERE srno = :srno", conn);
        cmd.Parameters.Add("srno", srno);
        cmd.ExecuteNonQuery();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "success", "alert('Row Deleted Successfully.');", true);
    }

    BindGrid();
}

Note. If UCC is the actual primary key, update this logic accordingly.

Test Cases

Test Case Expected Outcome
Load page GridView shows all clients
Click Edit Row becomes editable
Click Update The row is updated and a message appears
Click Delete The row is removed and confirmation is shown
Click Cancel Edit mode exits with the original data

Result

Result