Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hi
I want to create a login page and and want to authenticate it from database.
Posted 30-Sep-12 21:12pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
er.harmanpreetsingh at 1-Oct-12 3:26am
   
I tried this code but it always shows invalid login
Mohd. Mukhtar at 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 at 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 at 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 at 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
Mohd. Mukhtar at 3-Oct-12 1:40am
   
Please check first if the connection to database is opening or not.
First open connection with below line
con.Open();
and then check the connection state with below code.
if (con.State == ConnectionState.Open)
{
//execute the other code
}
else
{
//through error
//Or open connection again here
}
 

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 350
1 Gihan Liyanage 332
2 OriginalGriff 236
3 ClimerChinna 222
4 vikinghunter 168
0 Sergey Alexandrovich Kryukov 8,298
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,005
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Oct 2012
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