Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL .NET .NET4 login , +
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
 

 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();
        }
    
    }
 

 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 31-Jul-12 21:16pm
Edited 31-Jul-12 21:27pm
v2
Comments
Zoltán Zörgő at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
ajithk444 at 1-Aug-12 4:52am
   
thanks for ur help sauravbanthia
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
            }
 
        }
  Permalink  
Comments
ajithk444 at 1-Aug-12 3:41am
   
thanks Himanshu yadav.. it worked yar... thanku so much..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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();
}
}
 
}
  Permalink  
Comments
CHill60 at 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)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

just set integer number like this due to it return the number of row

int row_return = cmd.ExecuteNonQuery();
             if(row_return>0)
            Response.Redirect("ISSRegisterUsingSP.aspx");
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,034
1 Sergey Alexandrovich Kryukov 4,838
2 Peter Leow 3,129
3 Maciej Los 3,079
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 15 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100