Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello I am having a problem in this code can anyone help me. the program is running but it allows login for wrong user name and password. I have created a stored procedure for this. So anyone can help me to improve this code.


stored procedure
SQL
ALTER proc [dbo].[res] @uname  varchar(50)=null , @pword varchar(50)=null

as
select user_name,pass from test3 where user_name = isnull(@uname,user_name) and pass = isnull(@pword,pass)



asp.net code

using System.Data;
using System.Data.SqlClient;
public partial class login : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["loginconnectionstring"].ConnectionString;
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "res";
cmd.Parameters.Add("@uname", TextBox1.Text);
cmd.Parameters.Add("@pword", TextBox2.Text);
cmd.ExecuteNonQuery();
Label3.Visible = true;
Label3.Text = "login Successful";
TextBox1.Text = "";
TextBox2.Text = "";
}
catch (Exception ex)
{
Label4.Visible = true;
Label4.Text = ex.ToString();
}
finally
{
con.Close();
}
}
}
Posted

Your SP has
SQL
where user_name = isnull(@uname,user_name)


which means passing a null user name and password will return every record on the table - you just don't need the Isnull - use

SQL
where user_name = @uname


In your C# you ExecuteNonQuery - but you are executing a query! You probabl;y want to execute this and look at the returned records - there will be zero records for an invalid user name and password.

If you do just want a test (my log ins usually return some user data too so I can display "welcome Back Max instead of Welcome Back Maxxxx0192) you could change the Sp to return a value - something like

if exists(select 1 from usertable where name=@name and pwd=@pwd)
return 1
else
return 0



Then - in your code you execute the query then set the labelto visible - I assume you were expecting an exception on the executeNonQuery -= you won't get one unless your SP is invalid - i.e. syntactically incorrect.

So you need to do

int result = cmd.,ExecuteNonQuery

then test result for 0 or 1.
 
Share this answer
 
Comments
rgboss 19-Mar-13 5:28am    
thank you Maxxxx very much
Hi Dear,

Here the thing is simple, Just check your stored procedure. You are checking username and password and you are trying to get the values from that record If it exists. And while accessing that values by your code, You used ExecuteNonQuery() --> That need to be used only if you are manipulating the data (DML- Insert, Update and Delete).

Instead of that use a DataReader. assign the return values of the stored proc to datareader. And check if datareader HasRows then say "Login Successful".

Cheers....
 
Share this answer
 
v2
Comments
rgboss 19-Mar-13 5:27am    
Thank you sanjeev...it was helpful..

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