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

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
}
}

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.

Sys_Refcursor is null

I have this c# code
public static DataSet Get_Workflow_Def()
{
OracleConnection conn = DatabaseHelper.getOracleConnection();
DataSet dataset = new DataSet();
Log.Info("Opening connection");
conn.Open();
OracleCommand cmd = new OracleCommand("CTS.GET_WORKFLOW_DEF", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_Dataset", OracleDbType.RefCursor).Direction =
ParameterDirection.Output;
Log.Info("Executing query");
cmd.ExecuteNonQuery();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dataset);
conn.Close();
return dataset;
}
Calling this procedure
create or replace procedure CTS.GET_WORKFLOW_DEF(p_Dataset OUT
Sys_Refcursor)
as
begin
Open p_Dataset For
Select WKF_ID,
WKF_WORKFLOW_ID,
WKF_WORKFLOW_NAME,
WKF_WORKFLOW_VERSION,
WKF_WORKFLOW_SCHEMA
FROM CTS.WKF_WORKFLOW_MASTER
WHERE WKF_WORKFLOW_ACTIVE = '1'
ORDER BY WKF_ID DESC;
end GET_WORKFLOW_DEF;
If I execute the query I get the information I am looking for but the RefCursor is null. I'm an Oracle newb. Can anyone see where I am going wrong?
Found my problem I needed to add this line before the da.fill(dataset); line and after I instantiated the OracleDataAdapter in my c# code
da.TableMappings.Add("Table",
ConfigurationManager.AppSettings["DBPullTable"].ToString());

how to execute multiple oracle query c#

i am trying to execute multiple oracle select query as explain at that post answer here but I am getting exception as show at image
the same way explained at oracle site here
btw is ther anyway to handle the no rows found from one of these queries ?
string cmdstr = #"begin open :1 for
SELECT a.column1,
a.olumn2
b.column3
FROM table1 A,table2 B
WHERE A.column1=B.column1
AND A.column2 = NVL(:P_para, 0)
AND B.column3='1';
open :2 for select column1,
column2,
column3,
From dual; end;";
using (OracleConnection conn = new OracleConnection(connstr))
using (OracleCommand cmd = new OracleCommand(cmdstr, conn))
{
try
{
cmd.Parameters.Add(new OracleParameter(":P_para", OracleDbType.Int64)).Value = Convert.ToInt64(Textbox.Text);
cmd.Parameters.Add("p_cr1", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
cmd.Parameters.Add("p_cr2", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
cmd.CommandText = cmdstr;
conn.Open();
OracleTransaction trans = conn.BeginTransaction();
OracleDataReader oraReder;
oraReder = cmd.ExecuteReader();
while (oraReder.Read())
{
textbox1.Text = oraReder.GetString(0).ToString();
textbox2.Text = oraReder.GetValue(1).ToString();
textbox3.Text = oraReder.GetValue(2).ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Erorr Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Although you're using names for your parameters, your driver is treating them positionally. You can kind of tell because it's (almost) matching :1 with the name p_cr1 - '1' isn't a valid name. It doesn't complain since it matches positionally - but that means it's trying to use the P_para for :1, and as the type of that is wrong, that explains the error you see.
There may well be a way to change the driver's behaviour, but for now you can just swap the order you bind them - so the binds occur in the same order (position) the variables appear in the query. So:
cmd.Parameters.Add("p_cr1", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
cmd.Parameters.Add(new OracleParameter(":P_para", OracleDbType.Int64)).Value = Convert.ToInt64(Textbox.Text);
cmd.Parameters.Add("p_cr2", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);

my code gets this error:One or more errors occurred during processing of command

I have problem with running this:
i captured picture from my win
OleDbConnection con = new OleDbConnection();
DataTable dt = new DataTable();
con.ConnectionString = "Provider=MSDAORA;Data Source=DATA;Password=ss8_pakhsh;User ID=SHIFTS_N";
con.Open();
string sqlcommand = "INSERT INTO GROUPS(GROUP_ID, GROUP_NAME,DEPT_ID) " +
"VALUES(SEQ_MAX_GROUP_ID_NO.NEXTVAL,#groupName,SELECT
DEPT_ID FROM PERSONNEL_TEMP.DEPARTMENT WHERE
DEPARTMENT_NAME=#depName)";
OleDbCommand command = new OleDbCommand(sqlcommand, con);
command.Parameters.AddWithValue("#groupName", textBox1.Text);
command.Parameters.AddWithValue("#depName", comboBox1.SelectedItem);
OleDbDataAdapter oda = new OleDbDataAdapter(command);
oda.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
Change the sqlcommand string as shown below,
string sqlcommand = "INSERT INTO GROUPS(GROUP_ID, GROUP_NAME,DEPT_ID) " +
"VALUES(SEQ_MAX_GROUP_ID_NO.NEXTVAL,#groupName,SELECT "+
"DEPT_ID FROM PERSONNEL_TEMP.DEPARTMENT WHERE "+
"DEPARTMENT_NAME=#depName)";
This should solve the issue.
Observation: It is not a good practice to directly set the textBox1.Text value in parameter.

ExecuteScalar not functioning on server

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?

Resources