Trying to make connection to oracle 10g using plsql - c#

I am trying to make a simple connection to oracle db with asp .net website to populate a gridview. but error is showing like this "Additional information: ORA-12154: TNS:could not resolve the connect identifier specified"
The same is working properly in vs 2008 but in vs 2015 it is giving error.Error appears at con.open();.Any Idea would be appreciated.
c#
public partial class _Default : System.Web.UI.Page
{
//connection file for data base connection
String connectionString = ConfigurationManager.ConnectionStrings["conndbprodnew"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OracleConnection Con = new OracleConnection(connectionString);
Con.Open();
DataTable tab = new DataTable();
OracleDataAdapter da = new OracleDataAdapter("select * from Par_conn", Con);
da.Fill(tab);
GridView1.DataSource = tab;
GridView1.DataBind();
Con.Close();
Con.Dispose();
}
}
web config
<connectionStrings>
<add name="conndbprodnew" connectionString="Data Source=Dev;User ID=dew;Password=mes;Unicode=True" providerName="System.Data.OracleClient"/>
</connectionStrings>

Related

Please tell me what's wrong in my code.Trying to connect .mdf database with C#

I am a begginer and want to use .mdf database (Service based Database) in Visual Studio.
I have a very less to NO knowledge about using SQL SERVER. I have once used MySQL.
The app on a different PC. So, I shifted on SQL SERVER DATABASE FILE which will be in the project itself.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\databse.mdf;Integrated Security=True;Connect Timeout=30");
SqlCommand cmd;
SqlDataAdapter da;
DataTable dtbl=new DataTable();
private void button1_Click(object sender, EventArgs e)
{
try
{
con.Open();
cmd = new SqlCommand("SELECT*FROM tbl",con);
da = new SqlDataAdapter(cmd);
dtbl = new DataTable();
da.Fill(dtbl);
dataGridView1.DataSource = dtbl;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
This code is supposed to show data from the database when button1 is clicked.

I get this error when running my program i debug: System.Data.SqlClient.SqlException: 'Invalid object name 'Login'.'

I'm trying to construct a log in using the SqlServer which I'm not so familiar with. I don't ask for a very precise answer but more of a point in a general direction where the error may be. Should I learn more about the SqlServer to solve this problem or is the error somewhere else?
namespace Log_in
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogIn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Joel\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM Login WHERE Username='" + tbxLogIn.Text + "'AND Password ='"+tbxPassword.Text+"'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
Main ss = new Main();
ss.Show();
}
else
{
MessageBox.Show("Incorrect log in details.");
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
That error means it's not seeing Login. Either the table isn't there or you need to access it via schema.
This code is vulnerable to SQL Injection attacks. Please learn about parameterized queries or use something like Entity Framework. More Info on how parameterized queries solves the problem.

Getting error at the statement sqlCon.open "System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code"

public partial class Webpages_Default : System.Web.UI.Page
{
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
lblnorecordfound.Visible = false;
gvRCATracker.Visible = false;
if (!IsPostBack) {
//Binding control
sqlCon.Open();
BindPriority();
BindProductCategory();
BindPortfolio();
BindRCAResponseTeam();
BindTSMReview();
BindRCAStatus();
sqlCon.Close();
SetReadOnlyForDateControls();
}
}
}
Please make sure that if your Sql server is running as the default instance,then you need to use "." in the DataSource, eg.
connectionString="Data Source=.;Initial Catalog=LMS;User ID=sa;Password=password"
Otherwise, you need to specify the name of the instance of the server.
connectionString="Data Source=.\INSTANCENAME;Initial Catalog=LMS;User ID=sa;Password=password"

OleDbException was Unhandled - Not a valid file name

I've tried to establish a connection between my MS Access database and my c# application and I am not understanding why I am getting this error when the filename is correct.
Any
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'sQTDBDataSet.tblEquipment' table. You can move, or remove it, as needed.
this.tblEquipmentTableAdapter.Fill(this.sQTDBDataSet.tblEquipment);
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪C:\Users\Owner\Documents\Visual Studio 2015\Projects\Application\Application\bin\Debug\SQTDB.accdb;Persist Security Info=False;";
connection.Open();
checkConnectionLabel.Text = "Connected.";
connection.Close();
}

Unable to insert row into table using an Oracle Connection

I would like to know what's the problem, or your advice to connect to Oracle. I'm trying with this but when I try to insert values, it doesn't work.
I'm using the Visual Web Developer 2008 and when I add a database on Database Explorer connections, it's working. But when I try to connect via this connection string, it doesn't work. What am I missing?
I don't get an exception, so apparently it's working well. But this code apparently doesn't insert! The id column is a varchar(45). I created the table "test" just for testing purposes.
using System.Data;
using System.Data.OracleClient;
public partial class _Default : System.Web.UI.Page
{
//string oradb = "Data Source=localhost;User ID=root;Password=jesua;Unicode=True;";
String oracle = "User ID=root;Password=jesua;Unicode=True;Data Source=localhost;";
OracleConnection con = new OracleConnection();
public void Conectar() {
try
{
con.Close();
con.ConnectionString = oracle;
con.Open();
}
catch(Exception ex){
throw new Exception("No Conecto " + ex);
}
}
public void desconectar() {
// con.ConnectionString = oracle;
con.Close();
}
public void agregar() {
this.Conectar();
OracleCommand query = new OracleCommand("INSERT INTO testing (id) VALUES ('testing')");
query.ExecuteNonQuery();
desconectar();
}
protected void Button1_Click(object sender, EventArgs e)
{
try {
agregar();
}
catch(Exception ex){
Console.Write("No agrego " + ex);
}
TextBox1.Text = "Conected";
}
}
--------------------------UPDATE------------------
So,
i found the way to do that,
i hope anyone here can use this code in a future...
This code creates the connection betwen Oracle and asp.net C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client; // ODP.NET Oracle managed provider
using Oracle.DataAccess.Types;
namespace proyecto
{
public partial class WebForm1 : System.Web.UI.Page
{
public void dbconnect() {
string oradb = "Data Source=localhost;User ID={Yoir ID};Password={Your Password};";
OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO TESTING(id) VALUES ('valor')";
int rowsUpdated = cmd.ExecuteNonQuery();
if (rowsUpdated == 0)
Console.Write("Record not inserted");
else
Console.Write("Success!");
conn.Dispose();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.dbconnect();
}
}
}
Good Luck!
The connection string that works for me is
connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1522))
(CONNECT_DATA=(SERVICE_NAME=XE)));User Id=system;Password=pass;"
providerName="Oracle.DataAccess.Client"/>
It seems that you are missing the Service Name and Provider Name. You can find the service name in tnsnames.ora file which will be in your installation directory. Also make sure you have installed ODP.NET for Oracle 11g correctly, added reference to the Oracle.DataAccess.dll into your project and add the Provider Name in the connection string.

Categories