Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi I want to create a login page and and want to authenticate it from database.
Posted

1 solution

 
Share this answer
 
Comments
er.harmanpreetsingh 1-Oct-12 3:26am    
I tried this code but it always shows invalid login
Mohd. Mukhtar 1-Oct-12 5:52am    
Put your code block into the try-catch block and check the stack trace to get the exact error.
Please let me know if you are not able to resolve the issue.
er.harmanpreetsingh 1-Oct-12 8:21am    
Thank you dear 4 your great suuport..
I debug the programme.. it throws the exception.. {"Invalid object name 'dbo.HRIS_Employee'."}..here hris_Employee is my table name.

and my code is..
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace IVPTestPortal
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtUsername.Focus();
}

//private bool Authenticate(string userName, string password, string domain)
//{
// bool authentic = false;
// try
// {

// authentic = true;
// }
// catch (Exception ex)
// {
// vldAutheticate.ErrorMessage = "Invalid username and password. Contact your administrater !";
// }
// return authentic;
//}
//Code

public int Validate_Login(String Username, String Password)
{
//SqlConnection con = new SqlConnection(@"User id=sa;Password=123456;Server=LOVE-PC\YES;Database=HRIS_Employee");
SqlConnection con = null;
con = DABase.OpenConnection();
// con.Open();
SqlCommand cmdselect = new SqlCommand();
cmdselect.CommandType = CommandType.StoredProcedure;
cmdselect.CommandText = "[dbo].[p_Testportal_Login]";
cmdselect.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = Username;
cmdselect.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = Password;
cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
cmdselect.Connection = con;
int Results = 0;
try
{
//con.Open();
cmdselect.ExecuteNonQuery();
Results = (int)cmdselect.Parameters["@OutRes"].Value;
}
catch (SqlException ex)
{
lblMessage.Text = ex.Message;
}
finally
{
cmdselect.Dispose();
if (con != null)
{
con.Close();
}
}
return Results;
}

protected void btnLogin_Click(object sender, EventArgs e)
{


int Results = 0;

if (txtUsername.Text != null && txtPassword.Text != null)
{

Results = Validate_Login(txtUsername.Text, txtPassword.Text);

if (Results == 1)
{
Response.Redirect("TakeTest.aspx");
//lblMessage.Text = "Login is Good, Send the User to another page or enable controls";

}

else
{

lblMessage.Text = "Invalid Login";

lblMessage.ForeColor = System.Drawing.Color.Red;

}
}

else
{

lblMessage.Text = "Please make sure that the username and the password is Correct";

}

}
//code




//protected void btnLogin_Click(object sender, EventArgs e)
//{
// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
// con.Open();
// SqlCommand cmd = new SqlCommand("select * from HRIS_Employee where Username=@Name and Password=@Password", con);
// cmd.Parame
Mohd. Mukhtar 1-Oct-12 8:30am    
I think the Database name you are providing here is incorrect.
Please recheck the database name in the connection string and provide the correct databse name.

As per the error in your database HRIS_Employee database does not exist.
er.harmanpreetsingh 1-Oct-12 8:37am    
This is my store proc..
and i think the name provided is right...
My store proc is this..

[dbo].[p_Testportal_Login]

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



Create Proc [dbo].[p_Testportal_Login]
(
@Name VarChar(50),
@Password varChar(50),
@OutRes int OUTPUT
)
AS
set @OutRes = (SELECT count(*) FROM dbo.HRIS_Employee
WHERE Name = @Name And Password = @Password)
if(@OutRes = 1)

begin
set @OutRes = 1--Login is Correct
end
else
begin
set @OutRes = 0 --Bad login
end

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