1

I wrote a stored procedure to check whether the username and password exists in the database and return the results accordingly

This is the stored procedure

 ALTER proc [dbo].[checkuser]
 @userid varchar(20),
 @password varchar(20),
 @ReturnVal varchar(50) output
 As 
 Begin
 if exists(select userid from users where userid=@userid)
 set @ReturnVal='Logged in Successfully'
 else
 set @ReturnVal='Login Failed'
 end

and this is my c# code

private void button1_Click(object sender, EventArgs e)
{
    OleDbConnection conn = new OleDbConnection(
      "File Name=E:\\Vivek\\License Manager\\License Manager\\login.udl");

    try
    {  
        conn.Open();               
        OleDbCommand cmd = new OleDbCommand("checkuser",conn);              
        cmd.CommandType = CommandType.StoredProcedure;                
        OleDbParameter p1=new OleDbParameter("userid",username.Text); 
        OleDbParameter p2 = new OleDbParameter("password",password.Text);
        cmd.Parameters.Add(p1);                
        cmd.Parameters.Add(p2);                
        var returnParameter = cmd.Parameters.Add("ReturnVal",OleDbType.Integer);
        returnParameter.Direction = ParameterDirection.ReturnValue;
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);

    }
}

I am new to c#.I want to check whether the username exists from the database.But when I execute the code I get an error saying "too many arguments specified in a procedure or a function ".

Can anyone tell me where am I making a mistake?

Thanks

user2614235
  • 161
  • 3
  • 7
  • 21

3 Answers3

2

Try like this:

Replace Your Code with Followings Changes

 OleDbParameter p1=new OleDbParameter("@userid",username.Text);


OleDbParameter p2 = new OleDbParameter("@password",password.Text);

var returnParameter = cmd.Parameters.Add("@ReturnVal",OleDbType.Integer);

returnParameter.Direction = ParameterDirection.ReturnValue;

cmd.ExecuteNonQuery();

 int Result= Convert.ToInt32(returnParameter.Value);

In the Procedure

create proc checkuser
 @userid varchar(20),
 @password varchar(20),
@ReturnVal int output

 As 
 Begin
 if exists(select userid from users where userid=@userid)
 set @ReturnVal= 0
 else

 set @ReturnVal= 1
 end
Dgan
  • 10,077
  • 1
  • 29
  • 51
  • Thanks Ganesh but the options shown here int Result= Convert.ToInt(returnParameter.Value); is int16 and not int – user2614235 Oct 06 '14 at 06:11
  • Stored procedure throwing errors Msg 102, Level 15, State 1, Procedure checkuser, Line 4 Incorrect syntax near '@ReturnVal'. Msg 137, Level 15, State 1, Procedure checkuser, Line 8 Must declare the scalar variable "@ReturnVal". Msg 137, Level 15, State 1, Procedure checkuser, Line 10 Must declare the scalar variable "@ReturnVal". – user2614235 Oct 06 '14 at 06:14
  • 1
    @user2614235 Its working Fine For me did you copy paste that stored procedure?? – Dgan Oct 06 '14 at 06:17
  • Hey My application does not proceed further when I click the login button :( – user2614235 Oct 06 '14 at 06:26
2

Procedure never returns a value.You have to use a output parameter in stored procedure.

Changes in SP as shown here:

create proc checkuser
    @userid varchar(20),
    @password varchar(20)
    @result int output
As 
Begin
   if exists(select userid from users where userid=@userid)
      set @result = 0
   else
      set @result = 1
   end

and in C# code:

 SqlParameter returnParameter = cmd.Parameters.Add("result", SqlDbType.Int);
  returnParameter.Direction = ParameterDirection.ReturnValue;
  int id = (int) returnParameter.Value;
Neel
  • 11,625
  • 3
  • 43
  • 61
0

If you are using SQL-Server,Use Sql data base connection and command

Check this

Community
  • 1
  • 1
Prasanth V J
  • 1,126
  • 14
  • 32