Not properly calling a Stored procedure? - asp.net

I have an oracle stored procedure which is working fine on sqldeveloper inserting record.
How can i call it from asp.net? I have seen other solutions as well not helpfull.
OracleConnection objConn = new OracleConnection(_db.ConnectionString);
objConn.Open();
OracleCommand cmd = new OracleCommand("INSERTDOCUMENT", objConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("TMPLT_ID", OracleDbType.Decimal).Value = _schema.ID;
...
cmd.ExecuteNonQuery();
objConn.Close();
Someone please tell me what i am doing wrong?

I hope the stored procedure INSERTDOCUMENT is in the oracle schema "'RMI_PR".
Convert the statement
OracleCommand cmd = new OracleCommand("INSERTDOCUMENT", objConn);
into
OracleCommand cmd = new OracleCommand("RMI_PR.INSERTDOCUMENT", objConn);

Related

Get User Collection Type Object (Table) as Out Parameter from Oracle stored procedure

I need to get back from an Oracle stored procedure (inside a package) an output parameter defined as user collection type object (a table).
Procedure GetCon (DataRif IN VARCHAR2,Results OUT my_collectiontype)
IS
BEGIN
select my_ObjectType(Col1,Col2)
bulk collect into Results
FROM
TABLE(PMS.myfunc(DataRif ,'31-12-2019');
END;
....
The Function PMS.myfunc returns a my_collectiontype object
In my code (Asp.net 4.0 VB.NET with Oracle ManagedDataAccess Drivers), I've tried
strConn = "Data Source=XE.WORK;User Id=PMS;Password=xxxx"
Dim con As New OracleConnection(strConn)
Dim dt As DataTable = New DataTable()
Dim da As OracleDataAdapter = New OracleDataAdapter()
con.Open()
Dim cmd As New OracleCommand()
cmd.Connection = con
cmd.CommandText = "MySchema.MyPackage.GetCon"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("DataRif", OracleDbType.Varchar2,"23-07-2019", ParameterDirection.Input)
cmd.Parameters.Add("Results", OracleDbType.RefCursor).Direction = ParameterDirection.Output
da.SelectCommand = cmd
cmd.ExecuteNonQuery()
da.Fill(dt)
.....
I get this error
ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'GETCONT'
ORA-06550: line 1, column 7: PL/SQL: Statement ignored"
Sure the out parameter is not a ref cursor but I can't find the right object.
I need to use a stored procedure because I know it's possible execute something like this as simple view
SELECT * FROM TABLE (PMS.myfunc('01-01-2019', '31-12-2019')
but this solution is not the best at the moment.
Thanks in advance.
Solved in this following way that permits to receive a cursor and put it in a dataset.
Create Stored Procedure in Oracle like this
CREATE OR REPLACE PROCEDURE get_emp_rs (p_recordset OUT SYS_REFCURSOR) AS
BEGIN
OPEN p_recordset FOR
SELECT DIP_ID,DIP_NAME
FROM TABLE(fn_GetDataFunction('01-01-2019','31-12-2019','15-09-2019'));
END get_emp_rs;
/
In Asp.net
Public Function TestCursor() As DataSet
Dim strConn As String = "Data Source=MYSOURCE;User Id=MYID;Password=xxxxx"
Dim dsReturn As DataSet = Nothing
Try
Using con As New OracleConnection(strConn)
Using sda As New OracleDataAdapter()
Dim cmd As New OracleCommand()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "get_emp_rs"
cmd.Parameters.Add("p_recordset", OracleDbType.RefCursor).Direction = ParameterDirection.Output
sda.SelectCommand = cmd
Using ds As New System.Data.DataSet("results")
sda.Fill(ds, "MyTableName")
Return ds
End Using
End Using
End Using
Catch ex As Exception
'---------
End Try
End Function

SQL Server stored procedure in asp.net core

I created the application in ASP.NET core which needs to get details from SQL Server through a stored procedure.
Here is my code
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#lat", "31.77987");
cmd.Parameters.AddWithValue("#lng", "-106.4119");
cmd.Connection = connection;
cmd.CommandText = "sp_Procedure";
var r = cmd.ExecuteNonQuery();
When I run this code, I got an error
The server is attempting to use a feature that is not supported on this platform
If I missed out anything please correct me.

Code Error While Using SQL Output and ASP.NET

I have the below stored procedure in SQL Server 2008 which is not generating any errors in SQL, but is generating one in the web application which states "'GetGenInfo_Delete01_01_22' expects parameter '#FPath', which was not supplied". I am fairly novice at SQL, but what I am trying to do is return a field to VB.NET before the row is deleted. Any suggestions would be very helpful.
ALTER Procedure [dbo].[GetGenInfo_Delete01_01_22]
#IDX int,
#FPath varchar(100) OUTPUT
AS
Begin
SELECT #FPath = (SELECT FilePath FROM GenInfo_E1_01_22 Where ID=#IDX)
DELETE
FROM GenInfo_E1_01_22
WHERE ID = #IDX
END
Here is the VB code calling the stored proc
Using con As New SqlConnection(connstr)
Using cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetGenInfo_Delete01_01_22"
cmd.Parameters.Add("IDX", ID)
Dim returnParameter = cmd.Parameters.Add("#FPath", SqlDbType.VarChar)
returnParameter.Direction = ParameterDirection.ReturnValue
cmd.Connection = con
con.Open()
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
con.Close()
End Using
End Using
You're creating parameter returnParameter, but you're not adding it to the parameters collection. Use cmd.Parameters.Add(returnParameter) prior to DB Call.
You could add an FPath parameter to cmd. Do it like this:
SqlParameter fpath = new SqlParameter();
fpath.Direction = ParameterDirection.Output;
fpath.ParameterName = "#FPATH";
cmd.Parameters.Add(p);

Execute SQL stored using ado.net

I am using ado.net to access sql server I have costum Stored Procedure I am executing it using ado.net :
SqlCommand command = new SqlCommand("Custom",con);
and i am sending the parameters like this :
command.Parameters.Add(new SqlParameter("#Parm1",SqlDbType.Int,0,"Parm1"));
command.Parameters.Add(new SqlParameter("#Parm2",SqlDbType.Int,0,"Parm2"))
it dose not work and it did not give me an error as well it is the first time to work without DAL generator
Please, use this pattern:
SqlCommand command = new SqlCommand("nameOfMyStoredProcedure", mySqlConnectionObject);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("#ID", SqlDbType.Int);
command.Parameters["#ID"].Value = customerID;
to make it work add this line before set the params :
command.CommandType = CommandType.StoredProcedure;
and set the params using :
command.Parameters[0].Value=4;
command.Parameters[1].Value=2;
:)

error in mysql connection "The given key was not present in the dictionary"

I have problem while connecting to mysql database using "ADO.NET Driver for MySQL (Connector/NET)"
I got this exception The given key was not present in the dictionary.
Edit:
MySqlConnection con = new MySqlConnection("Server=localhost;Database=pgs_db;Uid=root;Pwd=;");
MySqlCommand com = new MySqlCommand();
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.CommandText = "getStudent";
con.Open();
MySqlDataReader dr =com.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
sorry i was using a worng connectionstring
this is a right one :
"server=localhost; user id=user; password=password; database=mydatabase; pooling=false;"
thnx Oded
Without seeing the code, I am guessing you are trying to access a configuration key that does not exist in your application config file.
Edit:
After seeing the code sample, the problem is probably not with connecting to the DB.
I would say that in your GridView, you are binding to a field that is not returned by the DB query.

Resources