I'm trying to hit the stored procedure from C# code but always get the result == -1. I don't know where I went wrong. I have searched a lot but didn't' find any solution. Please have a look into my code snippet and guide me what I'm doing wrong.
Thanks in advance.
C# code:
using (SqlConnection connection = new SqlConnection(getConnectionString()))
using (SqlCommand command = new SqlCommand())
{
Int32 rowsAffected;
command.CommandText = "SP_LOGIN_GETUSERBYNAME";
command.CommandType = CommandType.StoredProcedure;
// command.Parameters.Add(new SqlParameter("@Email", userObj.email));
// command.Parameters.Add("@Email", SqlDbType.VarChar).Value = userObj.email.Trim();
command.Parameters.AddWithValue("@Email", userObj.email.ToString());
command.Connection = connection;
connection.Open();
rowsAffected = command.ExecuteNonQuery();
connection.Close();
return rowsAffected;
}
Connection string:
return "Data Source=MUNEEB-PC;Initial Catalog=HRPayRoll;User ID=sa; Password=sa";
Stored procedure code:
CREATE PROCEDURE SP_LOGIN_GETUSERBYNAME
@Email varchar(50)
AS
SELECT *
FROM [User]
WHERE Email = @Email
GO
From
ExecuteNonQuery
doc;Since your command is SELECT, it is too normal to get -1 as a return value.
If you wanna reach your results, you can use
ExecuteReader
method instead.