System Oracle -> Oracle Managed - c#

I swapped out the references but I am getting an error that it can't cast System.Oracle to Oracle.ManagedDataAccess
public static DataSet ExecuteDataSetWithTimeOut(string spName, object[] parameterValues)
{
OracleCommand cmd = new OracleCommand();
cmd.CommandTimeout = 7200;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
cmd.Parameters.Add("o_msg", OracleDbType.Varchar2, 300).Direction = ParameterDirection.Output;
cmd.Parameters.Add("o_shipper_inv", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
cmd.Parameters.Add("i_start_dt", OracleDbType.Date).Value = parameterValues[2].ToString();
cmd.Parameters.Add("i_end_dt", OracleDbType.Date).Value = parameterValues[3].ToString();
cmd.Parameters.Add("i_user_id", OracleDbType.Varchar2).Value = parameterValues[4].ToString();
cmd.Parameters.Add("i_facility_cd", OracleDbType.Varchar2).Value = null;
cmd.Parameters.Add("i_shipper_cd", OracleDbType.Varchar2).Value = null;
return Db.ExecuteDataSet(cmd);
}

Related

DataReader is not getting values

DateTime dtExpiry = DateTime.Now.AddDays(1);
CommonFunctionDAL objCommDAL = new CommonFunctionDAL();
SqlCommand cmd = new SqlCommand("sp_LoginCheck", sqlconnection);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#UserName", emailid);
SqlParameter p2 = new SqlParameter("#Password", password);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
SqlParameter sEMPID = cmd.Parameters.Add("#status", SqlDbType.Int, 5);
sEMPID.Direction = ParameterDirection.Output;
sqlconnection.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
HttpCookie AppCookies = new HttpCookie("AppCookies");
AppCookies["EMPID"] = rd["EMPID"].ToString();
AppCookies["AEMPID"] = rd["AEMPID"].ToString();
AppCookies["EmpName"] = rd["EmpName"].ToString();
AppCookies["EmpDepartment"] = rd["EmpDepartment"].ToString();
AppCookies.Expires = dtExpiry;
HttpContext.Current.Response.Cookies.Add(AppCookies);
int Result = cmd.ExecuteNonQuery();
return sEMPID.Value.ToString();
}
I am getting enumeration yielded no results.

Executing Oracle procedure

I connected my oracle database to visual studio and now I'm trying to execute procedure I created in my database.
I tried this:
OracleCommand cmd = new OracleCommand("BEGIN ADD_USER('"+txtName.Text+"','"+txtName2.Text+"',"+txtID.Text+"); END;" );
cmd.ExecuteNonQuery();
My procedure has 3 parameters : name, 2name, id. It works fine when I use this command in sqldeveloper, but I get error when I try it in my project.
using (OracleConnection cn = new OracleConnection("con string"))
{
cn.Open();
OracleCommand cmd = new OracleCommand("ADD_USER");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cn;
cmd.Parameters.Add("YourSPParamName1", OracleDbType.{YourFieldTypeInDB}).Value = txtName.Text;
cmd.Parameters.Add("YourSPParamName2", OracleDbType.{YourFieldTypeInDB}).Value = txtName2.Text;
cmd.Parameters.Add("YourSPParamName3", OracleDbType.{YourFieldTypeInDB}).Value = txtID.Text;
cmd.ExecuteNonQuery();
}
Something like this should work.
Here's how It works for me:
OracleConnectionStringBuilder sb = new OracleConnectionStringBuilder();
sb.DataSource = "localhost";
sb.UserID = "something";
sb.Password = "pass";
OracleConnection conn = new OracleConnection(sb.ToString());
conn.Open();
OracleCommand cmd = new OracleCommand("ADD_USER");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.Parameters.Add("NAME", OracleDbType.Varchar2).Value = txtName.Text;
cmd.Parameters.Add("NAME2", OracleDbType.Varchar2).Value = txtName2.Text;
cmd.Parameters.Add("ID", OracleDbType.Int32).Value = txtID.Text;
cmd.ExecuteNonQuery();

Work with foreign key

In my application has been conflicts with the primary key constraint and a foreign key. I use stored procedures in a programming language c # and SqlDataReader. How to add data to the foreign keys in the database, that there was not primary key constraint. As the example of a way to organize this?
That my code for firstTable:
cmd = new SqlCommand(STORED_PREFIX + STORED_NAME2, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#io", SqlDbType.Int);
cmd.Parameters["#io"].Direction = ParameterDirection.Input;
cmd.Parameters["#io"].Value = 1;
cmd.Parameters.Add("#idstud", SqlDbType.BigInt);
cmd.Parameters["#idstud"].Direction = ParameterDirection.Input;
cmd.Parameters["#idstud"].Value = id_studtextBox.Text;
cmd.Parameters.Add("#numb_stud", SqlDbType.Int);
cmd.Parameters["#numb_stud"].Direction = ParameterDirection.Input;
cmd.Parameters["#numb_stud"].Value = numberstudtextbox.Text;
cmd.Parameters.Add("#naim_corpus", SqlDbType.NVarChar);
cmd.Parameters["#naim_corpus"].Direction = ParameterDirection.Input;
cmd.Parameters["#naim_corpus"].Value = naimcorpuscombobox.Text;
cmd.Parameters.Add("#place", SqlDbType.Int);
cmd.Parameters["#place"].Direction = ParameterDirection.Input;
cmd.Parameters["#place"].Value = placetextbox.Text;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dataGridView2.Rows.Add((string)rdr[_id_stud].ToString(),
(string)rdr[_nomer_stud].ToString(),
(string)rdr[_naim_corpus].ToString(),
(string)rdr[_mesto].ToString());
}
That code for secondTable
cmd = new SqlCommand(STORED_PREFIX + STORED_NAME2, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#daw", SqlDbType.Int);
cmd.Parameters["#daw"].Direction = ParameterDirection.Input;
cmd.Parameters["#daw"].Value = 3;
cmd.Parameters.Add("#teachid", SqlDbType.BigInt);
cmd.Parameters["#teachid"].Direction = ParameterDirection.Input;
cmd.Parameters["#teachid"].Value = id_cabtextBox.Text;
cmd.Parameters.Add("#numb_teacher", SqlDbType.Int);
cmd.Parameters["#numb_teacher"].Direction = ParameterDirection.Input;
cmd.Parameters["#numb_teacher"].Value = numbercabtextbox.Text;
cmd.Parameters.Add("#naim_corpus", SqlDbType.NVarChar);
cmd.Parameters["#naim_corpus"].Direction = ParameterDirection.Input;
cmd.Parameters["#naim_corpus"].Value = naimcorpuscombobox.Text;
cmd.Parameters.Add("#studid", SqlDbType.Int);
cmd.Parameters["#studid"].Direction = ParameterDirection.Input;
cmd.Parameters["#studid"].Value = studidtextbox.Text;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dataGridView2.Rows.Add((string)rdr[_teacher_id].ToString(),
(string)rdr[_nomer_teacher].ToString(),
(string)rdr[_naim_corpus].ToString(),
(string)rdr[_studid].ToString());
}

Return Value from a Stored Procedure

I am trying to get data returned from my stored procedure. This is what I have tried and absolutely nothing is coming back.
Stored Proc:
Declare #Submit_ID as int
Set #Submit_ID = (Select (Max(Sub_ID) + 1) from Sub_Details);
INSERT INTO.....
Return #Submit_ID
C# (2.0)
SqlConnection conn = null;
int subid;
try
{
conn = new SqlConnection(conn_string);
SqlCommand cmd = new SqlCommand("INSERT_INTO_MYDB", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#FirstName", SqlDbType.NVarChar, 255);
p1.Direction = ParameterDirection.Input;
p1.Value = txtFirstName.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("#LastName", SqlDbType.NVarChar, 255);
p2.Direction = ParameterDirection.Input;
p2.Value = txtLastName.Text;
cmd.Parameters.Add(p2);
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub);
}
You're not adding the return parameter to the command:
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pSub); // <------------- add to command
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(cmd.Parameters["pSub"].Value);
First add parameter to cmd and use .Value to its value
cmd.Parameters.Add(pSub); // add parameters to command
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub.Value); // Use .Value

Send new exception if a SELECT value returned is NULL or does not exist in the Table

So i am using a stored procedure to select a registry from a DB, the only thing the stored procedure does is SELECT ... The thing is that i use that stored procedure to fill a DataSet wich i return in my WebService. The problem comes when i want to send an exception instead of the DataSet, since the stored procedure checks on the DB and returns an empty row the DataSet fills with nothing and does not send me to an Exception ... Now, i save all my exceptions in a log table in the same DB ... My question is, can i go to the Exception block if the SELECT values are empty??
Here is my Code
[WebMethod(Description = "Private", EnableSession = false)]
public DataSet M812(string p_transaction)
{
string extran, enclosure, eDate;
try
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_M812";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Entradas");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
return ds;
}
catch(SqlException Ex)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_reqdataerrorlog";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd.Parameters.Add("#vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
enclosure = "" + cmd.Parameters["#vo_enclosure"].Value;
extran = "" + cmd.Parameters["#vo_trans"].Value;
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
cmd2.CommandText = "dbo.sp_errorlog";
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd2.Parameters.Add("#p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
cmd2.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = extran;
cmd2.Parameters.Add("#p_method", SqlDbType.NChar, 6).Value = "812";
cmd2.Parameters.Add("#p_message", SqlDbType.NVarChar, 250).Value = "SQL Error: " + Ex.Message;
cmd2.Parameters.Add("#vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd2.Parameters.Add("#vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
cmd2.Connection.Close();
eDate = "" + cmd2.Parameters["#vo_errorDate"].Value;
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con3;
cmd3.CommandText = "dbo.sp_selecterrorlog";
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
cmd3.Parameters.Add("#p_date", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds, "ErrorLog");
con3.Open();
cmd3.ExecuteNonQuery();
con3.Close();
cmd3.Connection.Close();
return ds;
}
catch (Exception Ex)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_reqdataerrorlog";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd.Parameters.Add("#vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
enclosure = "" + cmd.Parameters["#vo_enclosure"].Value;
extran = "" + cmd.Parameters["#vo_trans"].Value;
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
cmd2.CommandText = "dbo.sp_errorlog";
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd2.Parameters.Add("#p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
cmd2.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = extran;
cmd2.Parameters.Add("#p_method", SqlDbType.NChar, 6).Value = "812";
cmd2.Parameters.Add("#p_message", SqlDbType.NVarChar, 250).Value = "WEB Error: " + Ex.Message;
cmd2.Parameters.Add("#vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd2.Parameters.Add("#vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
cmd2.Connection.Close();
eDate = "" + cmd2.Parameters["#vo_errorDate"].Value;
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con3;
cmd3.CommandText = "dbo.sp_selecterrorlog";
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
cmd3.Parameters.Add("#p_date", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds, "ErrorLog");
con3.Open();
cmd3.ExecuteNonQuery();
con3.Close();
cmd3.Connection.Close();
return ds;
}
}
Here is how you can check if the DataSet has any rows in any table:
bool hasRows = ds.Tables.Cast<DataTable>().Any(table => table.Rows.Count != 0);
Now you have a choice as to how to get the error data returned to the caller:
Pull out the error building logic into a separate method that your exception catch handler block and your if condition can both call, like this:
private void BuildAndReturnErrorDataSet()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "dbo.sp_reqdataerrorlog";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd.Parameters.Add("#vo_enclosure", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#vo_trans", SqlDbType.VarChar, 10).Direction = ParameterDirection.Output;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
enclosure = "" + cmd.Parameters["#vo_enclosure"].Value;
extran = "" + cmd.Parameters["#vo_trans"].Value;
SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = con2;
cmd2.CommandText = "dbo.sp_errorlog";
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.Add("#p_inTrans", SqlDbType.NChar, 12).Value = p_transaction;
cmd2.Parameters.Add("#p_enclosure", SqlDbType.NChar, 6).Value = enclosure;
cmd2.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = extran;
cmd2.Parameters.Add("#p_method", SqlDbType.NChar, 6).Value = "812";
cmd2.Parameters.Add("#p_message", SqlDbType.NVarChar, 250).Value = "WEB Error: " + Ex.Message;
cmd2.Parameters.Add("#vo_message", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;
cmd2.Parameters.Add("#vo_errorDate", SqlDbType.DateTime).Direction = ParameterDirection.Output;
con2.Open();
cmd2.ExecuteNonQuery();
con2.Close();
cmd2.Connection.Close();
eDate = "" + cmd2.Parameters["#vo_errorDate"].Value;
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CTSSQL"].ConnectionString);
SqlCommand cmd3 = new SqlCommand();
cmd3.Connection = con3;
cmd3.CommandText = "dbo.sp_selecterrorlog";
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.Add("#p_trans", SqlDbType.NChar, 18).Value = p_transaction;
cmd3.Parameters.Add("#p_date", SqlDbType.DateTime).Value = eDate;
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds, "ErrorLog");
con3.Open();
cmd3.ExecuteNonQuery();
con3.Close();
cmd3.Connection.Close();
return ds;
}
if(!hasRows)
{
// Call error data set building logic
BuildAndReturnErrorDataSet();
}
catch (Exception Ex)
{
// Call error data set building logic
BuildAndReturnErrorDataSet();
}
Raise exception if hasRows is false - not recommended as this is using exception handling to cause program control flow, but it technically will work.

Categories