ExecuteScalar not functioning on server - oracle

The application uses Oracle DataAccess ver. 1.1. , VS 2008, .Net Framework 3.5 w/SP1
OracleConnection connection = new OracleConnection(ConnectionStringLogon);
connection.Open();
OracleParameter selectParam = new OracleParameter(":applicationName", OracleDbType.Varchar2, 256);
selectParam.Value = applicationName;
if (connection.State != ConnectionState.Open)
connection.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = connection;
cmd.CommandText = "Select ApplicationId from Applications where AppName = 'appName'";
cmd.CommandType = CommandType.Text;
if (selectParam != null)
{
cmd.Parameters.Add(selectParam);
}
object lookupResult = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if (lookupResult != null)
The procedure fails on object lookupResult = cmd.ExecuteScalar(); with this error:
Event Type: Error
Event Source: App Log
Event Category: None
Event ID: 9961
Date: 9/30/2008
Time: 4:42:11 PM
User: N/A
Computer: Server15
Description:
System.NullReferenceException: Object reference not set to an instance of an object.
at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
at Oracle.DataAccess.Client.OracleCommand.ExecuteReader()
at Oracle.DataAccess.Client.OracleCommand.ExecuteScalar()
at Membership.OracleMembershipProvider.GetApplicationId(String applicationName, Boolean createIfNeeded) in OracleMembershipProvider.cs:line 1626
I've looked at this from every angle that I can conceive of... basically, no matter how I wrap it, the Execute fails.

I notice your CommandText doesn't contain the specified parameter ":applicationName"

It's not an error in "ExecuteReader". It's an error in the execution of the query... is applicationName null?

Related

C# Core, OracleDataReader with hasRows as false while select all rows in a table that has data

I am testing Oracle database. I wrote some code but my datareader has no rows, why? My "carros" table has data and I am selecting all of them but I am getting an empty result set it seems.
string constr = "Data Source=localhost:1521/XE;User Id=System;Password=password;";
OracleConnection con = new OracleConnection(constr);
OracleCommand oracleCommand = new OracleCommand();
oracleCommand.Connection = con;
oracleCommand.CommandText = "select preco from carro";
con.Open();
OracleDataReader oracleDataReader = oracleCommand.ExecuteReader();
string resultado = String.Empty;
//My test, I got hasRows as false
if (oracleDataReader.HasRows == false)
{
resultado = "no results";
}
//never enters this loop.
while (oracleDataReader.Read())
{
resultado += (string)oracleDataReader["preco"];
}
// Close and Dispose OracleConnection
con.Close();
con.Dispose();
return resultado;
If HasRows is false after ExecuteReader, the problem is simply that the query is returning no rows so Read will return false as well. Perhaps the someValue variable is not set correctly.
From you description, it seems that your table is named carros while your query has used carro. Try to use
oracleCommand.CommandText = "select preco from carros";
A path towards solving this can include a scattering of logging commands that write to the file system.
System.IO.File.AppendAllText(#"\\server\\C\\log.txt", "put error message here" + "\r\n");
Or for printing a variable:
System.IO.File.AppendAllText(#"\\server\\C\\log.txt", ex.ToString() + "\r\n");
Pepper variations of this command throughout your script to see where failures are happening and to validate variable values.

Cannot retrieve out parameters from oracle stored procedure using OracleManagedDataAccess

I m writing my issue here after lot of struggle and trying all the available options online.
Here is my issue. I am using ODP.Net, oracleManagedDataAccess library to connect to the oracle database.I have a stored procedure with in and out parameters which works fine when I test it from pl/sql but when I am trying to execute it from my .net code and retrieve the out parameters they return null and if I see the status of each out parameter, its false and value is "Null Fetched" and also the size of the parameter is showing up as 0, though I set the size of the parameter to 4000 for a string type. Please see my code below. Please help. As I told you earlier, my stored proc works just fine from pl/sql.
connection = new OracleConnection(DBHelper.ConnectionString);
connection.Open();
command = new OracleCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = DBConstants.PROC_GETBORROWERSEQNO;
//Input
command.Parameters.Add("password", OracleDbType.Varchar2, 4000, password, ParameterDirection.Input);
command.Parameters.Add("userid", OracleDbType.Varchar2, 4000, userID, ParameterDirection.Input);
command.Parameters.Add("ipaddress", OracleDbType.Varchar2,4000, iPAddress, ParameterDirection.Input);
//Output
command.Parameters.Add("shawcustno", OracleDbType.Decimal).Direction = ParameterDirection.Output;
command.Parameters.Add("emailid", OracleDbType.Varchar2,4000).Direction = ParameterDirection.Output;
command.Parameters.Add("contr_phase", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
command.Parameters.Add("source_seqno", OracleDbType.Decimal,15).Direction = ParameterDirection.Output;
command.Parameters.Add("borrower_seqno", OracleDbType.Decimal,15).Direction = ParameterDirection.Output;
command.Parameters.Add("pag_phone", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
command.Parameters.Add("pag", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
//InputOutput
command.Parameters.Add("sessionseqno", OracleDbType.Decimal).Direction = ParameterDirection.InputOutput;
sequenceNumber = command.ExecuteNonQuery();
customer = new Customer();
customer.Company = command.Parameters["company"].Value.ToString();
customer.Pag = command.Parameters["pag"].GetString();
customer.PagPhone = command.Parameters["pag_phone"].GetString();
customer.BorrowerSeqNo = command.Parameters["borrower_seqno"].IsDBNull() ? 0 : command.Parameters["borrower_seqno"].GetInt32();
customer.SourceSeqNo = command.Parameters["source_seqno"].IsDBNull() ? 0 : command.Parameters["source_seqno"].GetInt32();
customer.EmailID = command.Parameters["emailid"].GetString();
customer.ShawCustNo = command.Parameters["shawcustno"].GetString();
customer.SessionSeqNo = command.Parameters["sessionseqno"].IsDBNull() ? 0 : command.Parameters["sessionseqno"].GetInt32();
}
finally found the answer, the order of the arguments has to be exactly the same as that declared in the stored procedure. I missed that order.

ORA-01840: input value not long enough for date format when executing a query with a timestamp parameter

I am trying to execute a query on Oracle database table with a TimeStamp(6) column, using odp.net
I am using below code, which throws this exception: "ORA-01840: input value not long enough for date format".
OracleConnection con = new OracleConnection(connStr");
using (con)
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandText = "SELECT * FROM Logs WHERE LOGDATE > :logDate";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("logDate", OracleDbType.TimeStamp, DateTime.Now.AddMonths(-1), ParameterDirection.Input));
using (command)
{
OracleDataReader rdr = command.ExecuteReader(); //ORA-01840 exception is thrown here
}
}
What is wrong about this query and it's parameters? I also tried OracleDbType.Date, instead of OracleDbType.TimeStamp but i got the same error.
I solved the problem by passing an OracleTimeStamp object instead of DateTime object. Below code works good:
OracleConnection con = new OracleConnection(connStr");
using (con)
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandText = "SELECT * FROM Logs WHERE LOGDATE > :logDate";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("logDate", OracleDbType.TimeStamp, new OracleTimeStamp(DateTime.Now.AddMonths(-1)), ParameterDirection.Input));
using (command)
{
OracleDataReader rdr = command.ExecuteReader(); //ORA-01840 exception was thrown here
}
}

net: unknown error 1 - C# .Net 2.0

Im getting error: net: unknown error 1
This is my code:
private void btnLogin_Click_1(object sender, EventArgs e)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandTimeout = 0;
cmd.CommandText = "hhrcv_logon_validation";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("pv_emp_username", OracleDbType.VarChar).Value = txtUsername.Text;
cmd.Parameters.Add("pv_emp_password", OracleDbType.VarChar).Value = txtPassword.Text;
cmd.Parameters.Add(new OracleParameter("pv_return_message", OracleDbType.VarChar));
cmd.Parameters["pv_return_message"].Direction = ParameterDirection.Output;
string valid;
conn.Open();
**cmd.ExecuteNonQuery();**
valid = cmd.Parameters["pv_return_message"].Value.ToString();
if (valid.ToString() == "")
{
frmHome main = new frmHome(lblEmp_id_no.Text);
main.Show();
this.Hide();
}
else
{
MessageBox.Show("" + valid, "Error");
}
conn.Close();
}
Unhandled exception on cmd.ExecuteNonQuery();
Everything was working fine till today. First time that I'm getting this.
Its not really descriptive. Can someone please assist.
Our DBAs implemented password expiry dates on our user accounts. The one we were using for the app was expired. Although I could still use the account to make the connection to DB and actually query data in the DB it didn't want to work from the app.
So issue is solved.

Problem with cmd.ExecuteNonQuery()

I am inserting values in to Database from a Webform using ADO.NET, C#. DB I am using is Oracle Database. Values are not being inserted and the program gets struck at the cmd.ExecuteNonquery()
Here is my Code below, Please let me know If I am doing any mistake.. I am using some Static Methods will that be any problem ?..
public Boolean AddDivCo(Int32 UserNo,String ID, String Role, String DivName )
{
Boolean ret = false;
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection();
int i = 0;
try
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_RIS"].ConnectionString;
conn.Open();
cmd.Connection = conn;
String mySQL = "INSERT INTO R4CAD_ADMIN (AdminUserNo, AdminID, AdminRole, AdminDivName)VALUES(:AdminUserNo,:AdminID,:AdminRole,:DivName)";
OracleParameter p1 = new OracleParameter("AdminUserNo", OracleType.Number);
p1.Value = UserNo;
cmd.Parameters.Add(p1);
OracleParameter p2 = new OracleParameter("AdminID", OracleType.VarChar);
p2.Value = ID;
cmd.Parameters.Add(p2);
OracleParameter p3 = new OracleParameter("AdminRole", OracleType.VarChar);
p3.Value = Role;
cmd.Parameters.Add(p3);
OracleParameter p4 = new OracleParameter("DivName", OracleType.VarChar);
p4.Value = DivName;
cmd.Parameters.Add(p4);
cmd.CommandText = mySQL;
i = cmd.ExecuteNonQuery();
if (i != 0)
{
ret = true;
}
else
{
ret = false;
}
}
catch (Exception err)
{
Console.WriteLine(err.Message.ToString());
}
finally
{
cmd.Dispose();
//cmd = null;
//conn = null;
conn.Close();
}
return ret;
}
Is there a primary key defined on this table? If so, then my guess is that you have another session that already has inserted a record with this key, but has not yet terminated the transaction with a commit or rollback. I don't see a commit as part of your code - I assume you're doing that somewhere else?
Execute your code above once more, and while it's hung run the following query from another session:
SELECT
(SELECT username FROM v$session WHERE sid=a.sid) blocker,
a.sid,
' is blocking ',
(SELECT username FROM v$session WHERE sid=b.sid) blockee,
b.sid
FROM v$lock a JOIN v$lock b ON (a.id1 = b.id1 AND a.id2 = b.id2)
WHERE a.block = 1
AND b.request > 0;
This should tell you if you're being blocked by another session and what the SID is of that session.

Resources