Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
1.Write a Storeprocedure which will take username and password and match against your table if value has been found then return true then redirect user to desired page.
The problem still the same..In stored procedures the data became like below
ALTER PROCEDURE CheckUserDetails
(@UserName varchar(24),@Password varchar(24)
)
as
Begin
select count(*) from Pat where UserName=@UserName and
Password=@Password End
And I also change the from nvarchar to varchar as you asked but the problem still same. This is the code I used in Login.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
Dim UserName As String
Dim Password As String
UserName = TextBoxUserName.Text ' you need to ask the user name from user
Password = TextBoxPassword.Text ' you need to ask the password from user
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "CheckUserDetails"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Checkuser.CommandType = CommandType.StoredProcedure
Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.VarChar, 24))
Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.VarChar, 24))
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
DatabaseW7ConnectionString.Close()
Dim result As String = "User is Authenticated, Now you can Redirect him"
Response.Redirect("~/Admin/HomeAdmin.aspx")
Else
Response.Write("<script>alert('Invalid user name or password, please check the credentials');</script>")
Return
End If
End Sub
End Class
Below is the error I get.
Server Error in '/WebSite7' Application.
Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Source Error:
Line 19: Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.VarChar, 24))
Line 20: Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.VarChar, 24))
Line 21: Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) Line 22: If temp = 1 Then
Line 23: DatabaseW7ConnectionString.Close()
Source File: D:\VStudio for testing\WebSite7\Login.aspx.vb Line:
21
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "Select count (*) from Pat where UserName='" + TextBoxUserName.Text & "'"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString) Dim objDataAdapter As New SqlDataAdapter(Checkuser) Dim dsResult As New DataSet()
objDataAdapter.Fill(dsResult)
if dsResult.tables(0).rows.count>0 then
if dsResult.tables(0).rows(0).item("password")=TextBoxUserName.Text then
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: how to create custom login
Mar 23, 2012 11:13 AM|LINK
can you use Varchar rather then using NVarchar
Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
MahadPK
Participant
778 Points
225 Posts
Re: how to create custom login
Mar 23, 2012 11:53 AM|LINK
it is Ideal way and it will definitely help you.
Use Session
Like this :
protected void btnLogin_Click(object sender, EventArgs e) { try { con.Open(); str = "Select username, password from login where username = '" + txtUsername.Text + "' and password = '" + txtPass.Text + "'"; cmd = new SqlCommand(str, con); sdr = cmd.ExecuteReader(); if (sdr.Read()) { Session["User"] = txtUsername.Text; Response.Redirect("Welcome.aspx"); } else { Response.Write("Invalid username/password"); } con.Close(); } catch (Exception ex) { Response.Write(ex.Message + "<br/>" + ex.StackTrace); } finally { con.Close(); } }WELCOME PAGE CODE protected void Page_Load(object sender, EventArgs e) { if (Session["User"] == null) Response.Redirect("Login.aspx"); else Label1.Text = Session["User"].ToString(); } protected void LogoutButton_Click(object sender, EventArgs e) { Session.RemoveAll(); Response.Redirect("Login.aspx"); }paritoshmmec
Participant
1555 Points
304 Posts
Re: how to create custom login
Mar 23, 2012 01:29 PM|LINK
1.Write a Storeprocedure which will take username and password and match against your table if value has been found then return true then redirect user to desired page.
2.If not then show your error.
Please let me know in case of any concerns.
Thanks and Regards,
Paritosh
hanis
Member
44 Points
35 Posts
Re: how to create custom login
Mar 23, 2012 01:41 PM|LINK
The problem still the same..In stored procedures the data became like below
ALTER PROCEDURE CheckUserDetails
(@UserName varchar(24),@Password varchar(24)
)
as
Begin
select count(*) from Pat where UserName=@UserName and
Password=@Password End
And I also change the from nvarchar to varchar as you asked but the problem still same. This is the code I used in Login.aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
Dim UserName As String
Dim Password As String
UserName = TextBoxUserName.Text ' you need to ask the user name from user
Password = TextBoxPassword.Text ' you need to ask the password from user
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "CheckUserDetails"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Checkuser.CommandType = CommandType.StoredProcedure
Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.VarChar, 24))
Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.VarChar, 24))
Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString())
If temp = 1 Then
DatabaseW7ConnectionString.Close()
Dim result As String = "User is Authenticated, Now you can Redirect him"
Response.Redirect("~/Admin/HomeAdmin.aspx")
Else
Response.Write("<script>alert('Invalid user name or password, please check the credentials');</script>")
Return
End If
End Sub
End Class
Below is the error I get.
Server Error in '/WebSite7' Application.
Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Procedure or function 'CheckUserDetails' expects parameter '@UserName', which was not supplied.
Source Error:
Line 19: Checkuser.Parameters.Add(New SqlParameter("@UserName", SqlDbType.VarChar, 24)) Line 20: Checkuser.Parameters.Add(New SqlParameter("@Password", SqlDbType.VarChar, 24)) Line 21: Dim temp As Integer = Convert.ToInt32(Checkuser.ExecuteScalar().ToString()) Line 22: If temp = 1 Then Line 23: DatabaseW7ConnectionString.Close()Source File: D:\VStudio for testing\WebSite7\Login.aspx.vb Line: 21
Munish.Soni
Member
23 Points
18 Posts
Re: how to create custom login
Mar 23, 2012 02:37 PM|LINK
Imports System.Data.SqlClient
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub ButtonLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonLogin.Click
Dim DatabaseW7ConnectionString As New SqlConnection(ConfigurationManager.ConnectionStrings("DatabaseW7ConnectionString").ConnectionString)
DatabaseW7ConnectionString.Open()
Dim cmdStr As String = "Select count (*) from Pat where UserName='" + TextBoxUserName.Text & "'"
Dim Checkuser As New SqlCommand(cmdStr, DatabaseW7ConnectionString)
Dim objDataAdapter As New SqlDataAdapter(Checkuser)
Dim dsResult As New DataSet()
objDataAdapter.Fill(dsResult)
if dsResult.tables(0).rows.count>0 then
if dsResult.tables(0).rows(0).item("password")=TextBoxUserName.Text then
response.redirect("~/welcome.aspx")
else
response.write("Invalid User")
end if
end sub
end class