Asp.net C# Tutorial 11 – How to Insert and Retrieve data from Oracle Database using ASP.NET C#



Asp.net C# Tutorial 11 – How to Insert and Retrieve data from Oracle Database using ASP.NET C#

Asp.net C# Tutorial 11 - How to Insert and Retrieve data from Oracle Database using ASP.NET C#

[email protected] 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.

******************************************************************
.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>

***********************************************************
.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
{

OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings[“OracleConString”].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
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)
{
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>