Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.57/5 (3 votes)
See more: , +
I am developing a login page in asp.net.IF the username and password matches i want to redirect to another aspx page.This is my stored procedure and my c# code.My problem is even if the username and password are given wrongly it is moving to that aspx page.Please help me with the correct code


C#
protected void btnLogin_Click(object sender, EventArgs e)
   {
       try
       {
           SqlCommand cmd = new SqlCommand("sp_ValidateUser", SqlConObject);
           cmd.CommandType = CommandType.StoredProcedure;
           SqlParameter param1 = new SqlParameter("@User_name", txtUsename.Text);
           SqlParameter param2 = new SqlParameter("@User_password", txtPassword.Text);
           cmd.Parameters.Add(param1);
           cmd.Parameters.Add(param2);
           SqlConObject.Open();
           cmd.ExecuteNonQuery();
           Response.Redirect("ISSRegisterUsingSP.aspx");
       }
       catch (Exception e7)
       {
           lblErrorMessage.Text = e7.Message;
       }
       finally
       {
           SqlConObject.Close();
       }

   }



SQL
 IF EXISTS
(SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE SPECIFIC_SCHEMA=N''
AND SPECIFIC_NAME=N'')

DROP PROCEDURE sp_ValidateUser
GO
CREATE PROCEDURE sp_ValidateUser
(
@User_name VARCHAR(50)
,@User_password VARCHAR(50)
)
AS
BEGIN
SELECT UserName
		,UserPassword
FROM ISSLogin_Details
WHERE UserName=@User_name AND UserPassword=@User_password
END
GO
Posted
Updated 31-Jul-12 21:27pm
v2
Comments
Zoltán Zörgő 1-Aug-12 3:20am    
How do you retrieve the result of your stored procedure? ExecuteNonQuery is for DML in the first place. You need to read the result and redirect based on the result.
ajithk444 1-Aug-12 3:26am    
Hi I am new to asp.
If it is not ExecuteReader() what should i use.Can u get me the correct code for this.
ajithk444 1-Aug-12 3:29am    
i jus need a stored procedure to validate the username and password and redirect to a new page.what changes should i need to do in this code..pls help me

If you want to check through a stored procedure that whether the username and password used in the login process is correct or not, then you may use an output parameter in the stored procedure.

Declare an output parameter as follows:

Declare @result as int out

Suppose table name is tblInfo

select @result = count(*) from tblInfo where username=@username and password=@password

If this output parameter return a value greater than 0, which may be checked din the front end, then the login credentials are correct, otherwise its false.

For further details you may visit us @ http://www.industrialtrainingkolkata.com/?cat=12

Besides, you may also join our 100% Job Oriented Practical training on ASP.NET with C# and SQL Server. You may call us @ 9830386818/09831709190.
 
Share this answer
 
Comments
ajithk444 1-Aug-12 4:52am    
thanks for ur help sauravbanthia
Hi,
Every thing is fine but instead of using ExecuteNonQuery you use SqlDataAdapter like as follows..

protected void btnLogin_Click(object sender, EventArgs e)
      {
          try
          {
              SqlCommand cmd = new SqlCommand("sp_ValidateUser", SqlConObject);
              cmd.CommandType = CommandType.StoredProcedure;
              SqlParameter param1 = new SqlParameter("@User_name", txtUsename.Text);
              SqlParameter param2 = new SqlParameter("@User_password", txtPassword.Text);
              cmd.Parameters.Add(param1);
              cmd.Parameters.Add(param2);
              SqlConObject.Open();
              //cmd.ExecuteNonQuery();
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              DataTable dt=new DataTable ();
              da.Fill(dt);
              if(dt.Rows.Count>0)
              {
               Response.Redirect("ISSRegisterUsingSP.aspx");
              }
          }
          catch (Exception e7)
          {
              lblErrorMessage.Text = e7.Message;
          }
          finally
          {
              SqlConObject.Close();
          }

      }
 
Share this answer
 
Comments
ajithk444 1-Aug-12 3:41am    
thanks Himanshu yadav.. it worked yar... thanku so much..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
public partial class Hello : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection("Data Source=DEEPSINGH-C\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
SqlCommand cmd = new SqlCommand("loginreg",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, true);
Response.Redirect("Default2.aspx");
con.Close();
}
}

}
 
Share this answer
 
Comments
CHill60 27-Jul-13 11:32am    
You do realise this question is a year old and there is little difference between if(dt.Rows.Count>0) from Solution 1 to your if (dr.HasRows)
just set integer number like this due to it return the number of row

C#
int row_return = cmd.ExecuteNonQuery();
             if(row_return>0)
            Response.Redirect("ISSRegisterUsingSP.aspx");
 
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