I am making a web application with ASP.Net C# language but I am validating the OUT
output so that it shows me the error message:
This is my procedure:
ALTER PROCEDURE pAdminUsersEvaSmart @tipo INT
,@UserName VARCHAR(100)
,@ERROR VARCHAR(100) OUT
AS
BEGIN
IF @tipo = 0
BEGIN
IF EXISTS (
SELECT *
FROM seguridad..Usuario
WHERE username = @UserName
)
BEGIN
UPDATE seguridad..Usuario
SET IntentosUltimoAcceso = 0
,IntentosActualAcceso = 0
,Conexion = 'DES'
WHERE UserName = @UserName
SET @ERROR = 'SE DESBLOQUEO CORRECTAMENTE..'
END
ELSE
BEGIN
SET @ERROR = 'NO SE PUEDE DESBLOQUEAR EL USUARIO NO EXISTE..'
END
END
END
ASP.Net code:
protected void btnDesbloqueo_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=10.0.101.85\\instbdd01;Database=Seguridad;User Id=sa;Password=SA123456789*;");
SqlCommand cmd = new SqlCommand("pAdminUsersEvaSmart", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@tipo", SqlDbType.Int);
cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 100);
cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
cmd.Parameters["@tipo"].Value = 0;
cmd.Parameters["@UserName"].Value = txtUserName.Text;
cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;
conn.Open();
cargardatos();
cmd.ExecuteNonQuery();
conn.Close();
Label1.Text = cmd.Parameters["@ERROR"].Value.ToString(); ;
if (Label1.Text == "SE DESBLOQUEO CORRECTAMENTE..")
{
ScriptManager.RegisterStartupScript(this, GetType(), "Show Modal Popup", "showmodalpopup1();", true);
}
else if (Label1.Text == "NO SE PUEDE DESBLOQUEAR EL USUARIO NO EXISTE..")
{
ScriptManager.RegisterStartupScript(this, GetType(), "Show Modal Popup", "showmodalpopup();", true);
}
}
As you can see, it does have the same name by which it must enter that if
but it does not enter, I do not understand why if they are the same strings.
If you can see in the image that you send from the debugging, you can't see where the quotes are being closed for the value of
Label1.Text
. This means that SQL SERVER is sending you the text with blanks to the right. Having said thatTo fix this, simply change this line of code
For this other one, which removes the blank spaces from the
string
Initially, I would do a small restructure to your Stored Procedure so that you don't have an output message:
Now, on the side of your C# code, get the message with
ExecuteScalar
, validate the message and assign it toLabel1
:It is always recommended to make these comparisons by Number, that is, the error code instead of a string, but from what you see in your LABEL1.Text, your string does not end in the ".." apparently, so it looks continuous with spaces at the end this is why when comparing the strings they are not matching.