I have many stored procedure calls in my C# code, but only this one keeps failing. I'm running VS 2012 and SQL Server 2008 R2. My connection string is the same for all my stored procedures and I have the same permissions on all of them.
I get this error
Could not find stored procedure 'StP_Map_Preload #bldg, #linePos, #startD, #lineNo, #Pgrm, #apPos, #sessionID'.
System.Exception {System.Data.SqlClient.SqlException}
on this line:
SqlDataReader dr = cmd.ExecuteReader();:
I have tried creating a new stored procedure with the same code, setting permissions, but it fails too.
public ArrayList DetailPreload(string bldg, string linePos, DateTime startD, string lineNo, string Pgrm, int apPos, string sessionID)
{
string strSQL = "StP_Map_Preload #bldg, #linePos, #startD, #lineNo, #Pgrm, #apPos, #sessionID";
ArrayList list = new ArrayList();
using (SqlConnection conStr = new SqlConnection(connM))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("#bldg", bldg);
cmd.Parameters.AddWithValue("#linePos", linePos);
cmd.Parameters.AddWithValue("#startD", startD);
cmd.Parameters.AddWithValue("#lineNo", lineNo);
cmd.Parameters.AddWithValue("#Pgrm", Pgrm);
cmd.Parameters.AddWithValue("#apPos", apPos);
cmd.Parameters.AddWithValue("#sessionID", sessionID);
cmd.CommandText = strSQL;
cmd.Connection = conStr;
conStr.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(new
{
company = dr["Company"],
Pgrm = dr["Pgrm"],
lineNum = dr["lineNum"],
lineStation = dr["lineStation"],
jobCount = dr["Job"],
item = dr["Item"],
qty = dr["Qty"],
partQty = dr["partQty"],
invCnt = dr["invCnt"],
runID = dr["runID"]
});
}
conStr.Close();
return list;
}
}
}
My stored procedure is in SQL Server and I can execute it in SQL Server
/*
StP_Map_Preload 'AA-12', '7', '09/19/2014', '', '247', 7, 'val2gxfh5ihoqy4tshzl4tp3'
*/
ALTER proc [dbo].[StP_Map_Preload]
#Bldg varchar(10),
#linePos varchar(5),
#startD date,
#LineNo varchar(5),
#Pgrm varchar(5),
#apPos int,
#sessionID varchar(50)
AS
BEGIN
declare #sql varchar(Max), #PST varchar(20),
#SI1 varchar(5), #SI2 varchar(5), #SI3 varchar(5),
#hasAP bit, #CCLen varchar,
#Co varchar(5), #CCs varchar(25), #lsGrp varchar(max)...
Change your line to
string strSQL = "StP_Map_Preload";
There is no need to list the parameters in the string that names the stored procedure.
Related
I am trying to count how many users is updated and how many users are inserted after I run my stored procedure.
CREATE PROCEDURE [dbo].[ADProcTemp]
#Username varchar(250),
#DisplayName varchar(70),
#isEnabled tinyint,
#PassNevExp tinyint,
#addedUser int OUTPUT,
#updatedUser int OUTPUT
AS
BEGIN
SET #addedUser = 0
SET #updatedUser = 0
IF NOT EXISTS (SELECT TOP 1 PrezimeIme FROM [dbo].[tblZaposleni_AD] WITH (NOLOCK)
WHERE NetworkLogin = #Username)
BEGIN
IF(#isEnabled = 1 OR #PassNevExp = 1)
INSERT INTO [dbo].[tblZaposleni_AD](NetworkLogin, PrezimeIme, Status, PassNevExp)
VALUES (#Username, #DisplayName, #isEnabled, #PassNevExp)
SET #addedUser = #addedUser + ##ROWCOUNT;
SELECT #addedUser As UkupnoDodanihKorisnika
END
ELSE
BEGIN
UPDATE [dbo].[tblZaposleni_AD]
SET Status = #isEnabled,
PassNevExp = #PassNevExp
WHERE NetworkLogin = #Username
AND (Status <> #isEnabled) OR (PassNevExp <>#PassNevExp)
SET #updatedUser = #updatedUser + ##ROWCOUNT;
SELECT #updatedUser As UkupnoIzmjenjenihKorisnika
END
END
Here is my stored procedure and right now I want in my C# code display #addedUser and #updatedUser variable from stored procedure.
So far I create this
public void ExcStrPrc(string Username, string DisplayName, bool isEnable, bool PassNevExp)
{
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DesignSaoOsig1;Integrated Security=True");
SqlCommand cmd = new SqlCommand("ADProcTemp", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Username", Username.ToString().Trim());
cmd.Parameters.AddWithValue("#DisplayName", DisplayName.ToString().Trim());
cmd.Parameters.AddWithValue("#isEnabled", Convert.ToInt32(isEnable));
cmd.Parameters.AddWithValue("#PassNevExp", Convert.ToInt32(PassNevExp));
cmd.Parameters.Add("#addedUser", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Parameters.Add("#updatedUser", SqlDbType.Int).Direction = ParameterDirection.Output;
conn.Open();
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
Console.WriteLine("Uspjesno izvrseno !");
}
Console.WriteLine("Ukupno novih korisnika {0}");
conn.Close();
}
Any idea how to display counter from stored procedure to C# function? I added inside my function and right now I didn't get any data in output...
TBH was a long time ago since I messed around with output parameters, but you add output parameters, but you never check those. The values you require should be in those parameters.
I suggest you try to get those values like (after the execution of course):
var addedUserCount = (int)cmd.Parameters["#addedUser"].Value;
Or something similar
I have created a procedure in oracle to insert data into table.When i call the procedure from asp.net webservices, its invoking the following error..
"ORA-06550: line 1, column 7:\nPLS-00905: object TEST.CMPPROJECTPROC is invalid\nORA-06550: line 1, column 7:\nPL/SQL: Statement ignored"}"
My procedure code is
CREATE OR REPLACE PROCEDURE CMPPROJECTPROC (
p_projectname CMPPROJECT.PROJECTNAME%TYPE,
p_description CMPPROJECT.DESCRIPTION%TYPE,
p_company CMPPROJECT.COMPANY%TYPE,
p_projectstatus CMPPROJECT.PROJECTSTATUS%TYPE,
p_websiteurl CMPPROJECT.WEBSITEURL%TYPE,
p_completedin CMPPROJECT.COMPLETEDIN%TYPE,
p_startedin CMPPROJECT.STARTEDIN%TYPE,
p_status CMPPROJECT.STATUS%TYPE)
IS
BEGIN
INSERT INTO CMPPROJECT (PROJECTNAME,DESCRIPTION,COMPANY,PROJECTSTATUS,WEBSITEURL,COMPLETEDIN,STARTEDIN,STATUS)
VALUES (p_projectname,p_description,p_company,p_projectstatus,p_websiteurl,p_completedin,p_startedin,p_status);
COMMIT;
END;
My webservice code is
[WebMethod]
public void Insert(string name, string description, int companyName, int projectStatus, string websiteurl, string completedin, string startredin, int status)
{
cmd = con.CreateCommand();
con.Open();
//cmd.CommandText = "CMPPROJECTPROC";
cmd = new OracleCommand("CMPPROJECTPROC", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_projectname", OracleDbType.Varchar2).Value = name;
cmd.Parameters.Add("p_description", OracleDbType.Varchar2).Value = description;
cmd.Parameters.Add("p_company", OracleDbType.Int16).Value = companyName;
cmd.Parameters.Add("p_projectstatus", OracleDbType.Int16).Value = projectStatus;
cmd.Parameters.Add("p_websiteurl", OracleDbType.Varchar2).Value = websiteurl;
cmd.Parameters.Add("p_completedin", OracleDbType.Varchar2).Value = completedin;
cmd.Parameters.Add("p_startedin", OracleDbType.Varchar2).Value = startredin;
cmd.Parameters.Add("p_status", OracleDbType.Int16).Value = status;
cmd.ExecuteNonQuery();
con.Close();
}
The stored procedure in the database is invalid. It will have to be compiled again. It may throw errors.
A procedure will be invalidated, when anything changes in the database the procedure was compiled against. For example if you changed the table the procedure is using, it has to be recompiled or you will get this error message.
You can recompile your stored procedure by doing this:
ALTER PROCEDURE TEST.CMPPROJECTPROC COMPILE;
I am trying to execute an Oracle stored procedure (1 input and 2 out parameters) using C#.
My table contain 3 columns; an integer id, and 2 varchar2 type columns.
This is table definition:
CREATE TABLE TESTTABLE
(
ID INT Not Null,
FNAME VARCHAR2(200),
LNAME VARCHAR2(200),
Constraint PK Primary Key (ID)
);
This is my stored procedure:
create or replace PROCEDURE TESTP
(
tempID IN TESTTABLE.ID%Type,
tempName Out TESTTABLE.NAME%TYPE,
tempLName out TESTTABLE.LNAME%TYPE
)
AS
BEGIN
select Name, LNAME
into tempName, tempLName
from TestTable
where ID = tempID;
END;
Here is the code to execute this procedure from C#:
try
{
Int32 id = 1;
string FName = "", LName = "";
using (_ora.GetOracleConnection())
{
Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand("TESTP", _ora.GetOracleConnection());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("tempId", Oracle.DataAccess.Client.OracleDbType.Int32,ParameterDirection.Input).Value = id;
cmd.Parameters.Add("tempName", Oracle.DataAccess.Client.OracleDbType.Varchar2,200,ParameterDirection.Output).Value = FName;
cmd.Parameters.Add("tempLName", Oracle.DataAccess.Client.OracleDbType.Varchar2,200,ParameterDirection.Output).Value = LName;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
This is the exception generated:
Oracle.DataAccess.Client.OracleException ORA-06502: PL/SQL: numeric or value error
ORA-06512: at "USMANDBA.TESTP", line 9
Can anyone help me ?
These modifications in your code worked for me:
using (connection)
{
Int32 id = 1;
OracleCommand cmd = new OracleCommand("TESTP", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("tempID", OracleDbType.Int32, ParameterDirection.Input).Value = id;
cmd.Parameters.Add("tempName", OracleDbType.Varchar2, 200).Direction = ParameterDirection.Output;
cmd.Parameters.Add("tempLName", OracleDbType.Varchar2, 200).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string FName = cmd.Parameters["tempName"].Value.ToString();
string LName = cmd.Parameters["tempLName"].Value.ToString();
}
You could also add exception blocks in Oracle procedure to handle no_data_found exception and avoid ORA-01403 error, like here:
CREATE OR REPLACE PROCEDURE TESTP (tempID IN TESTTABLE.ID%Type,
tempName out TESTTABLE.NAME%TYPE, tempLName out TESTTABLE.LNAME%TYPE) AS
BEGIN
select Name, LNAME Into tempName,tempLName from TestTable Where ID = tempID;
EXCEPTION WHEN NO_DATA_FOUND THEN
tempName := null;
tempLName := null;
END;
and add additional OUT parameter informing about success or failure and handle it in C# code.
I have created DataAccessLayer.cs file which helps me to make connection withe the database for insert records into the database and i have created store procedure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class DataAccessLayer
{
SqlConnection con = new SqlConnection("Connection String");
SqlCommand cmd;
public DataAccessLayer()
{
//
// TODO: Add constructor logic here
//
}
public int ExecuteProcedure(String procname, SqlParameter[]param)
{
cmd = new SqlCommand();
cmd.CommandText = procname;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
foreach (SqlParameter obj in param)
{
cmd.Parameters.Add(obj);
}
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
}
Code for inserting record into data base using store procedure
SqlParameter[] param = new SqlParameter[]
{
new SqlParameter("#Name",txtFirstName.Text),
new SqlParameter("#DOB",txtDOB.Text),
new SqlParameter("#Address",txtAddress.Text),
new SqlParameter("#CN",ddlCountry.SelectedValue),
new SqlParameter("#SN",ddlState.SelectedValue),
new SqlParameter("CNN",ddlCity.SelectedValue),
new SqlParameter("#Mobile",txtMobile.Text),
new SqlParameter("#Email",txtEmail.Text),
new SqlParameter("#Pincode",txtPincode.Text),
new SqlParameter("#returnval",SqlDbType.Int)
};
int i = DAL.ExecuteProcedure("AddContactInfo", param);
if (i == 1)
{
lblError.Text = "Sucess";
}
else
{
lblError.Text = "Not Sucess";
}
Store Procedure for inserting records
ALTER proc [dbo].[AddContactInfo]
(
#Name varchar(50),
#DOB datetime,
#Address Varchar(max),
#Mobile varchar(15),
#Email varchar(50),
#CN int,
#SN int,
#CNN int,
#Pincode int,
#returnval int output
)
As
Begin
if exists(select Id from Contact_Info where Email=#Email)
begin
set #returnval=-2;
return #returnval;
end
else
begin
insert into Contact_Info(Name,DOB,Address,CountryName,StateName,CityName,Pincode,Mobile,Email,CreatedOn )
values(#Name,#DOB,#Address,#CN,#SN,#CNN,#Pincode,#Mobile,#Email,GETDATE())
if ##ERROR<>0
begin
set #returnval=-3;
return #returnval;
end
else
begin
set #returnval=1;
return #returnval;
end
end
End
But i am getting error which is "Procedure or function 'AddContactInfo' expects parameter '#returnval', which was not supplied."
You'll have to set your new SqlParameter("#returnval", SqlDbType.Int) to be an output parameter:
foreach (SqlParameter obj in param)
{
if (cmd.ParameterName == "#returnval")
{
cmd.Direction = ParameterDirection.Output
}
cmd.Parameters.Add(obj);
}
Of course, this expects all SP's to have the same #returnval when appropriate. It's better to set it when creating the list of parameters.
I'm trying to get the ID of the row affected in "real time". I could check it for the parameters used or the last row, but I want to do it with the least delay possible, so that there isn't multiple users mixing information in the tables.
Tried so far:
public int setFileInfo(string fileName, int filePrivacy, string filePassword, string fileDesc, string fileOwner)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bitRain"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("dbo.Upload", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
if(!String.IsNullOrEmpty(filePassword))
{
filePassword = FormsAuthentication.HashPasswordForStoringInConfigFile(filePassword, "MD5");
}
try
{
conn.Open();
cmd.Parameters.Add("#fileName", fileName);
cmd.Parameters.Add("#filePrivacy", filePrivacy);
cmd.Parameters.Add("#filePassword", filePassword);
cmd.Parameters.Add("#fileDescription", fileDesc);
cmd.Parameters.Add("#fileOwner", fileOwner);
int fileID = (int)cmd.ExecuteScalar();
return fileID;
}
catch (Exception ex)
{ }
finally
{
conn.Close();
}
return -1;
}
}
Stored Procedure:
CREATE PROCEDURE [dbo].[Upload]
#fileName nvarchar(20),
#filePrivacy int,
#filePassword nvarchar(50),
#fileDescription nvarchar(200),
#fileOwner nvarchar(14)
AS
INSERT INTO Files ([FileName], FilePrivacy, FilePassword, FileDescription, FileOwner)
VALUES (#fileName, #filePrivacy, #filePassword, #fileDescription, #fileOwner)
RETURN 0
I need some OUTPUT parameter, but I don't know how to use it and msdn examples aren't clear enough for me.
Stored Procedure:
CREATE PROCEDURE [dbo].[Upload]
#fileName nvarchar(20),
#filePrivacy int,
#filePassword nvarchar(50),
#fileDescription nvarchar(200),
#fileOwner nvarchar(14),
#id int out
AS
INSERT INTO Files ([FileName], FilePrivacy, FilePassword, FileDescription, FileOwner)
VALUES (#fileName, #filePrivacy, #filePassword, #fileDescription, #fileOwner)
set #id = SCOPE_IDENTITY()
END
In cs add
cmd.Parameters["#id"].Direction = ParameterDirection.Output;
try to use SCOPE_IDENTITY function