C# code:
protected void btnsearch_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
SqlCommand com = new SqlCommand("sp_studentresult", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#id", textstudentid.Text);
SqlDataAdapter adp = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
}
SqlParameter retval = new SqlParameter("#output", SqlDbType.VarChar, 50);
retval.Direction = ParameterDirection.Output;
com.Parameters.Add(retval);
com.ExecuteNonQuery();
string Output = retval.Value.ToString();
}
Stored procedure:
ALTER PROCEDURE sp_studentresult
(
#id int,
#output varchar(50) output,
#id_student varchar(50)
)
AS
begin
SELECT * from studentresult where id_student=#id
End
IF EXISTS (SELECT * FROM student WHERE id=#id_student)
BEGIN
SET #output='EXIST'
END
I'm new to .net. When I enter student id and search I get
Procedure or function sp_studentresult has too many arguments specified.
May I know what my mistake in the above code?
Any help would be highly appreciated.
Thanks,
i bet, you are trying to return a sql result set and an output parameter, right? if so, then try the code below:
protected void btnsearch_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
SqlCommand com = new SqlCommand("sp_studentresult", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#id", textstudentid.Text);
com.Parameters.AddWithValue("#id_student", textIDStudent.Text);
SqlParameter retval2 = new SqlParameter("#output", SqlDbType.VarChar, 50);
retval2.Direction = ParameterDirection.Output;
com.Parameters.Add(retval2);
SqlDataAdapter adp = new SqlDataAdapter(com);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
txtid.Text = ds.Tables[0].Rows[0]["id"].ToString();
txttamil.Text = ds.Tables[0].Rows[0]["Tamil"].ToString();
txtenglish.Text = ds.Tables[0].Rows[0]["English"].ToString();
txtmaths.Text = ds.Tables[0].Rows[0]["Maths"].ToString();
txtscience.Text = ds.Tables[0].Rows[0]["Science"].ToString();
txtsocialscience.Text = ds.Tables[0].Rows[0]["SocialScience"].ToString();
}
SqlParameter retval = new SqlParameter("#output", SqlDbType.VarChar, 50);
retval.Direction = ParameterDirection.Output;
com.Parameters.Add(retval);
com.Parameters.AddWithValue("#id", textstudentid.Text);
com.Parameters.AddWithValue("#id_student", textIDStudent.Text);
com.ExecuteNonQuery();
string Output = retval.Value.ToString();
}
The stored procedure names are different.
SqlCommand com = new SqlCommand("sp_studentresult", con);
here you are referring to "sp_studentresult". The stored procedure code you have inserted is
ALTER PROCEDURE sp_searchupdate
Obviously the parameters list should be different for these procedures.
Its because the stored procedure is not getting value for all the parameters ,
Check the number of parameters which are passed to the SP from SqlCommand .
If possible update your stored procedure
You are missing parameter id_student which must supply.
Only the parameters marked as Output need not to supply.
Add it like you have added #id
e.g.
com.Parameters.AddWithValue("#id", Value);
I am not sure what will be the value for it , so you can put the correct value in place of variable Value
Related
I have a stored procedure which looks like this:
PROCEDURE semifinished_lable_data(p_piece_num_id IN INTEGER,
p_piece_id OUT STRING,
p_tickness OUT NUMBER)
IS begin
select p_piece_id,p_tickness into p_piece_id,p_tickness from piece p where p.piece_num_id=p_piece_num_id;
end;
I've executed it from the database and it worked perfectly. When I execute it from c# app I don't get any value back.
Then when I researched the problem , I added that : cmd.BindByName = true
But this time i got the following error
multiple instances of named argument in list
C# codes :
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.InitialLONGFetchSize = 1000;
var seciliProsedur = "QUA_PRINTING.semifinished_lable_data";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = seciliProsedur;
cmd.BindByName = true;
object piece_num_id = gridViewMain.GetFocusedRowCellValue(gridViewMain.FocusedColumn);
OracleParameter PAR_ID1 = new OracleParameter();
PAR_ID1.ParameterName = "p_piece_id ";
PAR_ID1.OracleDbType = OracleDbType.Int32;
PAR_ID1.Direction = System.Data.ParameterDirection.Input;
PAR_ID1.Value =piece_num_id;
PAR_ID1.Size = 100;
cmd.Parameters.Add(PAR_ID1);
cmd.Parameters.Add("p_piece_id", OracleDbType.Varchar2, 1000, null, ParameterDirection.Output);
cmd.Parameters.Add("p_tickness", OracleDbType.Double,200, null, ParameterDirection.Output);
conn.Open();
var da = new OracleDataAdapter(cmd);
var dt = new DataTable();
da.Fill(dt);
How can I solve this problem ?
Actually I never used CommandType = CommandType.StoredProcedure.
Try this
cmd.CommandType = CommandType.Text;
seciliProsedur = "BEGIN QUA_PRINTING.semifinished_lable_data(:p_piece_num_id , :p_piece_id, :p_tickness); END;"
OracleParameter PAR_ID1 = new OracleParameter();
PAR_ID1.ParameterName = "p_piece_num_id";
...
cmd.Parameters.Add("p_piece_id", OracleDbType.Varchar2, ParameterDirection.Output);
cmd.Parameters("p_piece_id").DbType = DbType.Varchar2;
cmd.Parameters.Add("p_tickness", OracleDbType.Varchar2, 240, null, ParameterDirection.Output);
cmd.Parameters("p_tickness").DbType = DbType.String;
I think you must set DbType and OracleDbType property.
Then another problem of your code is
var da = new OracleDataAdapter(cmd);
var dt = new DataTable();
da.Fill(dt);
Your procedure does not return any resultSet (i.e. RefCursor), thus above commands do not work. Use
cmd.ExecuteNonQuery()
private static Registration InsertData(Registration register)
{
string connstr = #"Data Source=JUZAR-PC\SQLEXPRESS;Initial Catalog=Dbsaif;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connstr))
{
using (SqlCommand cmd = new SqlCommand("Registration_proc1", con))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(#"p_fullname", SqlDbType.VarChar).Value = register.FullName;
cmd.Parameters.AddWithValue(#"p_email", SqlDbType.VarChar).Value = register.Email;
cmd.Parameters.AddWithValue(#"p_password", SqlDbType.VarChar).Value = register.Password;
cmd.Parameters.AddWithValue(#"p_cfnpswd", SqlDbType.VarChar).Value = register.ConfirmPassword;
cmd.Parameters.AddWithValue(#"p_city", SqlDbType.VarChar).Value = register.City;
da.SelectCommand = cmd;
con.Open();
da.Fill(dt);
con.Close();
}
}
}
if (dt != null && dt.Rows != null && dt.Rows.Count > 0)
{
register = (from DataRow dr in dt.Rows
select new Registration()
{
FullName = dr["Name"].ToString(),
Email = dr["Email"].ToString(),
Password = dr["Password"].ToString(),
ConfirmPassword = dr["ConfirmPassword"].ToString(),
City = dr["City"].ToString()
}).ToList().FirstOrDefault();
}
return register;
}
STORED PROCEDURE FOR THE ABOVE CODE IS:
ALTER PROCEDURE [dbo].[Registration_proc1]
#p_fullname varchar(50),
#p_email varchar(50),
#p_password varchar(8),
#p_cfnpswd varchar(8),
#p_city varchar(50)
AS
BEGIN
SET NOCOUNT ON;
Insert into Registeration(Name,Email,Password,ConfirmPassword,City)
Values (#p_fullname,#p_email,#p_password,#p_cfnpswd,#p_city)
END
Help me with this please:
1.It is throwing an exception on stored proc parameter p_fullname in C# code.
2.And usually I get this error please help me know where am I going wrong.
3.Mostly faced this error while using SQL Server
Without seeing the stored procedure in question, I would say that this line here:
cmd.Parameters.AddWithValue(#"p_name", SqlDbType.VarChar).Value = register.FullName;
should be
cmd.Parameters.AddWithValue(#"p_fullname", SqlDbType.VarChar).Value = register.FullName;
Just as the exception states. It's expecting THAT parameter and you are not sending it.
I want to show the stored procedure but i am getting the error of Procedure SPselcocpd has no parameters and arguments were supplied.
cs page
lbllgintype.Text = com;
lblname.Text = com1;
sqlq = "";
sqlq = "select [name] from admin where userid='" + com1 + "'";
SqlDataAdapter da1 = new SqlDataAdapter(sqlq, con);
DataSet ds = new DataSet();
da1.Fill(ds);
name.Text = "";
name.Text = ds.Tables[0].Rows[0][0].ToString();
SqlCommand cmd = new SqlCommand("SPselcocpd");
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("#comname", comname.Text);
SqlDataAdapter da2 = new SqlDataAdapter(cmd);
DataSet dt1 = new DataSet();
da2.Fill(dt1);
con.Open();
brand.Items.Add("<--select-->");
for (int i = 0; i < dt1.Tables[0].Rows.Count; i++)
{
brand.Items.Add(dt1.Tables[0].Rows[i][0].ToString());
}
con.Close();`
SQL STORED PROCEDURE
alter procedure SPselbdralt
#comname varchar(100)
as
begin
select brandname from rpmallot where comname=(select id from companydetails where comname=#comname)
end
please solve my problem as soon as possible I am working on a live project
Please make sure, You are using the correct sp
I want to show the stored procedure but i am getting the error of
Procedure SPselcocpd has no parameters and arguments were
supplied.
In code you are calling different SP SPselcocpd
SqlCommand cmd = new SqlCommand("SPselcocpd"); // Here it is SPselcocpd
And SQL SP is
alter procedure SPselbdralt
Confirm that you are calling the correct SP
You are calling wrong stored procedure.
Change
SqlCommand cmd = new SqlCommand("SPselcocpd");
To
SqlCommand cmd = new SqlCommand("SPselbdralt");
C#:
public DataSet ListaClientes()
{
DataSet ds = new DataSet();
try
{
System.Data.OracleClient.OracleConnection conexion = Conexion.GetConnection();
if (conexion.State == System.Data.ConnectionState.Open)
{
System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand();
cmd.Connection = conexion;
cmd.CommandText = "ListadoClientes";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("resul", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
conexion.Close();
System.Data.OracleClient.OracleDataAdapter da = new System.Data.OracleClient.OracleDataAdapter(cmd);
da.Fill(ds);
}
return ds;
}
catch(Exception e)
{
throw e;
}
}
Oracle stored procedure:
CREATE OR REPLACE PROCEDURE ListadoClientes(resul OUT sys_refcursor)
IS
BEGIN
OPEN resul for select ID ,NOMBRES ,APELLIDOS ,CEDULA ,DIRECCION ,TELEFONO
from cliente;
END ListadoClientes;
ERROR visible from the C# CATCH block:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'LISTADOCLIENTES'
ORA-06550: line 1, column 7: PL/SQL: Statement ignored
Is there a reason why you do not use a function instead of procedure?
CREATE OR REPLACE FUNCTION ListadoClientes() RETURN sys_refcursor
IS
resul Sys_refcursor;
BEGIN
OPEN resul for select ID ,NOMBRES ,APELLIDOS ,CEDULA ,DIRECCION ,TELEFONO
from cliente;
RETURN resul;
END ListadoClientes;
Then in C# you must change it to this:
cmd.Parameters.Add("resul", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
When you run da.Fill(ds); then the function is executed, i.e. using cmd.ExecuteNonQuery(); executes the function twice.
Anyway, for a procedure the right way should be this one:
cmd.CommandText = "ListadoClientes(:resul)";
Finally, I managed to make it worked this way:
stored procedure
create or replace PROCEDURE ListadoClientes(resul OUT sys_refcursor) IS BEGIN OPEN resul for select ID ,NOMBRES ,APELLIDOS ,CEDULA ,DIRECCION ,TELEFONO from cliente; END ListadoClientes;
source c#
reference using Oracle.DataAccess.Client;
public DataSet ListaClientes()
{
DataSet ds = new DataSet();
try
{
OracleConnection conexion = Conexion.GetConnection2();
if (conexion.State == System.Data.ConnectionState.Open)
{
OracleCommand cmd = new OracleCommand();
cmd.Connection = conexion;
cmd.CommandText = "ListadoClientes";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("resul", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
conexion.Close();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(ds);
}
return ds;
}
catch(Exception e)
{
throw e;
}
}
I am getting the following error while supplying parameters to a stored procedure:
Procedure or function 'ismovieexists' expects parameter '#movie_name', which was not supplied
and the same error message for the procedure insert_values_in_movie_master..
public int add_movie(mymovie objmymovie)
{
SqlConnection cn = new SqlConnection(_connectionstring);
cn.Open();
//SqlDataReader dr;
SqlCommand cmd = new SqlCommand("ismovieexists", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#movie_name", objmymovie.MOVIE_NAME);
SqlParameter d = new SqlParameter("#d", SqlDbType.Int);
d.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(d);
cmd.ExecuteReader();
int i = (int)cmd.Parameters["#d"].Value;
if (i == 0)
{
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = cn;
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.CommandText = "insert_values_in_movie_master";
cmd1.Parameters.AddWithValue("#movie_name", objmymovie.MOVIE_NAME);
cmd1.Parameters.AddWithValue("#rating", objmymovie.RATING);
cmd1.Parameters.AddWithValue("#realease_year", objmymovie.REALEASE_YEAR);
cmd1.Parameters.AddWithValue("#starcast", objmymovie.STARCAST);
cmd1.Parameters.AddWithValue("#language", objmymovie.LANGUAGE);
cmd1.Parameters.AddWithValue("#display_home", objmymovie.DISPLAY_HOME);
cmd1.Parameters.AddWithValue("#block_status", objmymovie.BLOCK_STATUS);
cmd1.Parameters.AddWithValue("#no_of_copies", objmymovie.no_of_copies);
cmd1.Parameters.AddWithValue("#MOVIE_category", objmymovie.MOVIE_category);
cmd1.Parameters.AddWithValue("#MOVIE_flag", objmymovie.MOVIE_FLAG);
cmd1.ExecuteNonQuery();
return i;
}
else
return 1;
}
Does the parameter #Movie_Name exist in your Stored Procedures? If the parameter does exist it's likely that you are not passing a value to objmymovie.MOVIE_NAME