Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a store procedure i want to show @Result in lable control.

here is the stored proceudure. this procedure result in nothing.. probably dere would be some error in my code.

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertUser] (
@UserName varchar(50),
@Password varchar(50),
@FirstName varchar(50),
@LastName varchar(50),
@Email varchar(150),
@Mobile varchar(50),
@Result varchar(100) output )
AS
BEGIN
 
Declare @UserCount int;
 
SELECT @UserCount = COUNT(*) FROM users WHERE UserName = @UserName;
 
IF(@UserCount > 0)
begin
 
 Set  @Result = 'User already exists';
 
end
ELSE
begin
 
 INSERT INTO users(UserName,Password,FirstName,LastName,Email,MobileNo)VALUES (@UserName,@Password,@FirstName,@LastName,@Email,@Mobile);
 

 Set  @Result = 'User successfully added';
 
end
	
END


my web application is three teir based..

here is the code of presentation layer..

C#
registerUser reg = new registerUser();
reg.UserName = txtUserName.Text;
reg.Password = txtPassword.Text;
reg.FirstName = txtfirstname.Text;
reg.LastName = txtlastname.Text;
reg.Email = txtemail.Text;
reg.MobileNo = txtmobileno.Text;

int b =reg.insertUser(reg);
if (b == 1)

{
    lblreg.Text = "User Registration Successful";

}
else
{
    lblreg.Text = "Oh Their is Some Problem, Come back Lator";
}


and thats for business layer..

C#
public int insertUser(registerUser reg)
    {
        dbcon obj = new dbcon();
        SqlParameter[] objParams = new SqlParameter[6];


        objParams[0] = new SqlParameter("@UserName", reg.UserName);
        objParams[1] = new SqlParameter("@Password", reg.Password);
        objParams[2] = new SqlParameter("@FirstName", reg.FirstName);
        objParams[3] = new SqlParameter("@LastName", reg.LastName);
        objParams[4] = new SqlParameter("@Email", reg.Email);
        objParams[5] = new SqlParameter("@Mobile", reg.MobileNo);

        int b=obj.ExecuteNonQuery("insertUser", objParams);
        return b;
    }


what changes i have to made to get my disired results..
Posted
Updated 27-Nov-11 4:24am
v2
Comments
OriginalGriff 27-Nov-11 10:34am    
Please, it is called a Presentation layer for a reason. Have you ever considered spell-checking your messages?
lblreg.Text = "Oh Their is Some Problem, Come back Lator";
Makes you look very unprofessional!
"Their" is a plural adjective meaning "Belonging to or associated with the people or things previously mentioned or easily identified".
"There" is an adverb meaning "In, at, or to that place or position".
"Lator" is not a word.
"Later" is an adverb meaning "at a subsequent time".

If You Are Going To Capitalise Every Word, Don't forget One Near The End.
codegeekalpha 27-Nov-11 10:37am    
oh dear.. thats not english grammer forum..

You would need a SqlParameter in your objParams to hold your @Result.
The b you seem to be using will be the execution status of your stored procedure (success/fail), not the result you determine within it.
 
Share this answer
 
It has been years since I've done any SQL, try this code hope it helps!

SQL
SELECT @UserCount = COUNT(*) FROM users WHERE UserName = @UserName;
 
IF(@UserCount > 0)
begin
 
 SELECT '0';
 
end
ELSE
begin
 
 INSERT INTO users(UserName,Password,FirstName,LastName,Email,MobileNo)VALUES (@UserName,@Password,@FirstName,@LastName,@Email,@Mobile);
 
 
 SELECT '1';
 
end
	
END
 
Share this answer
 
hi,

try my codes without output parameter..

sql section:

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertUser] (
@UserName varchar(50),
@Password varchar(50),
@FirstName varchar(50),
@LastName varchar(50),
@Email varchar(150),
@Mobile varchar(50)
AS
BEGIN
 
Declare @UserCount int;
Declare @result int);
 
set @UserCount = SELECT COUNT(*) FROM users WHERE UserName = @UserName;
 
IF(@UserCount > 0)
begin
 
 Set  @Result = -1
 return
end
ELSE
begin
 
 INSERT INTO users(UserName,Password,FirstName,LastName,Email,MobileNo)VALUES (@UserName,@Password,@FirstName,@LastName,@Email,@Mobile);
 
 --succesful
 Set  @Result = -2;
 return
end
	
END


Business layer:

C#
public int insertUser(registerUser reg)
    {
        dbcon obj = new dbcon();
        SqlParameter[] objParams = new SqlParameter[6];
 

        objParams[0] = new SqlParameter("@UserName", reg.UserName);
        objParams[1] = new SqlParameter("@Password", reg.Password);
        objParams[2] = new SqlParameter("@FirstName", reg.FirstName);
        objParams[3] = new SqlParameter("@LastName", reg.LastName);
        objParams[4] = new SqlParameter("@Email", reg.Email);
        objParams[5] = new SqlParameter("@Mobile", reg.MobileNo);
 
        int b=obj.ExecuteScalar("insertUser", objParams);
        return b;
    }


presentation layer:

C#
registerUser reg = new registerUser();
        reg.UserName = txtUserName.Text;
        reg.Password = txtPassword.Text;
        reg.FirstName = txtfirstname.Text;
        reg.LastName = txtlastname.Text;
        reg.Email = txtemail.Text;
        reg.MobileNo = txtmobileno.Text;
 
        int b =reg.insertUser(reg);
        if (b == -2)
 
        { 
            lblreg.Text = // successful message; 
 
        }
        else
        { 
            lblreg.Text = //error message; 
        }


try this codes and ask me if it gives you error.
hope it helps..

mark this as answer if it helps you..
thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900