25 views
asked in Asp.net C# by

How to Insert and Retrieve data from Oracle Database using ASP.NET C#

Please log in or register to answer this question.

answered by

inchirags@gmail.com     Chirag's ASP.NET C# Tutorial               https://www.chirags.in

***********************************************************************************************

How to Insert and Retrieve data from Oracle Database using ASP.NET C# 

***********************************************************************************************

Steps:

1. Create a new ASP.NET Web Application in Visual Studio.

2. Add the Oracle NuGet package to your project by right-clicking on the project in Solution Explorer and selecting "Manage NuGet Packages."

3. In your Web Form, create a form with input fields for the data you want to insert into your Oracle database.

4. In the code-behind file (e.g., Default.aspx.cs), add the following using statement to include the Oracle.ManagedDataAccess.Client namespace.

5. In the code-behind file, add the following code to connect to your Oracle database and insert data into a table.

6. Call the Insert Data method when the user submits the form. For example, you could add a button to your form and call the method in the button's click event handler.

******************************************************************

CRUDOracle.aspx page

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="margin-left:auto;margin-right:auto;">
            <tr>
                <td>
                    <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label><br />
                    <h1>User Information</h1><br />
                    First Name : <asp:TextBox ID="first_name" runat="server" Placeholder="Enter your First Name" ></asp:TextBox><br /><br />
                    Last Name : <asp:TextBox ID="last_name" runat="server" Placeholder="Enter your Last Name" ></asp:TextBox><br /><br />
                    Email  : <asp:TextBox ID="email" runat="server" Placeholder="Enter your Email Address" ></asp:TextBox><br /><br />
                    <asp:Button ID="sbtBtn" runat="server" Text="Submit" OnClick="sbtBtn_Click" />
                </td>
            </tr>
        </table>
        <table style="margin-left:auto;margin-right:auto;">
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

***********************************************************

CRUDOracle.aspx.cs page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.Configuration;
public partial class CRUDOracle : System.Web.UI.Page
{
    //string TNSCon = "Data Source=(DESCRIPTION =" +
    //    "(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))" +
    //    "(CONNECT_DATA =" +
    //    "(SERVER = DEDICATED)" +
    //    "(SERVICE_NAME = ORCL)));" +
    //    "User Id= chirag;Password=chirag";


    OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        //OracleConnection connection = new OracleConnection(TNSCon);
        con.Open();
        OracleDataAdapter mySQLAdp = new OracleDataAdapter("Select * from userinfo", con);
        DataTable dt = new DataTable();
        mySQLAdp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }

    protected void sbtBtn_Click(object sender, EventArgs e)
    {
        //OracleConnection con = new OracleConnection(TNSCon);
        con.Open();
        string query = "INSERT INTO USERINFO(first_name,last_name,email) values(:1, :2, :3)";
        OracleCommand cmd = new OracleCommand(query, con);

        OracleParameter firstname = new OracleParameter();
        firstname.OracleDbType = OracleDbType.Varchar2;
        firstname.Value = first_name.Text;

        OracleParameter lastname = new OracleParameter();
        lastname.OracleDbType = OracleDbType.Varchar2;
        lastname.Value = last_name.Text;

        OracleParameter email_id = new OracleParameter();
        email_id.OracleDbType = OracleDbType.Varchar2;
        email_id.Value = email.Text;

        cmd.Parameters.Add(firstname);
        cmd.Parameters.Add(lastname);
        cmd.Parameters.Add(email_id);
        cmd.ExecuteNonQuery();
        con.Close();
        lblMsg.Text = "Data inserted Successfully.";
        lblMsg.ForeColor = System.Drawing.Color.Green;
    }
}

************************************************

web.config

<connectionStrings>
    <add name="OracleConString" connectionString="Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = ORCL)));User Id= chirag;Password=chirag;" providerName="Oracle.ManagedDataAccess.Client"/>
</connectionStrings>

Note: Flow the Process shown in video.

Subscribe and like for more videos:

https://www.youtube.com/@chiragstutorial

Don't forget to, Follow, Like, Share & Comment

Thanks & Regards,

Chitt Ranjan Mahto "Chirag"

_____________________________________________________________________

Note: All scripts used in this demo will be available in our website.

Link will be available in description.

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...