hi,
if i run the aspx file i get the following error:
"ExecuteReader requires an open and available Connection. The connection's current state is Closed.".
i didnt use ExecuteReader anywhere in the coding. actually im using 3 files to validate the username and the password of the user. please let me know how to validate the data with the database value. if the username is true i want to redirect to another aspx file, otherwise i must show an error message in the login file.
the codings are:
-------------login.aspx-------------
<%@ Page Language="vb" Codebehind="Login.aspx.vb" AutoEventWireup="false" Inherits="shopping.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Login Form</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server" />
<br>
<br>
<asp:Label ID="lblUserName" Runat="server" text="Enter User Name" />
<asp:TextBox ID="txtUserName" MaxLength="10" TextMode="SingleLine" Runat="server" />
<br>
<asp:Label ID="lblPassword" Runat="server" text="Enter Password" />
<asp:TextBox ID="txtPassword" MaxLength="10" TextMode="Password" Runat="server" />
<br>
<br>
<asp:ImageButton id="LoginBtn" ImageURL="images/signin.gif" runat="server" />
<br>
<br>
<br>
</form>
</body>
</HTML>
-------------------login.aspx.vb---------------
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Security
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents lblUserName As System.Web.UI.WebControls.Label
Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
Protected WithEvents LoginBtn As System.Web.UI.WebControls.ImageButton
Protected WithEvents Message As System.Web.UI.WebControls.Label
Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Public Sub New()
End Sub
Private Sub LoginBtn_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles LoginBtn.Click
Dim mycomponent As shopping.CustomersDB = New shopping.CustomersDB()
Dim custid As String = mycomponent.Login(txtUserName.Text, txtPassword.Text)
If custid = "" Then
txtMessage.Text = "Login Failed"
Else
txtMessage.Text = "Login Success"
End If
End Sub
End Class
-----customer.vb-------
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Namespace shopping
Public Class CustomersDB
Public Function Login(ByVal UserName As String, ByVal Password As String) As String
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"
)
Dim myCommand As SqlCommand = New SqlCommand("CustomerLogin", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
Dim parUserName As SqlParameter = New SqlParameter("@UserName", SqlDbType.NVarChar, 10)
parUserName.Value = UserName
myCommand.Parameters.Add(parUserName)
Dim parUserPass As SqlParameter = New SqlParameter("@UserPass", SqlDbType.NVarChar, 10)
parUserPass.Value = Password
myCommand.Parameters.Add(parUserPass)
Dim parameterCustomerID As SqlParameter = New SqlParameter("@CustomerID", SqlDbType.NVarChar, 10)
parameterCustomerID.Direction = ParameterDirection.Output
myCommand.Parameters.Add(parameterCustomerID)
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Dim customerId As String = parameterCustomerID.Value
If customerId = "" Then
Return Nothing
Else
Return customerId
End If
End Function
End Class
End Namespace
if i run the aspx file i get the following error:
"ExecuteReader requires an open and available Connection. The connection's current state is Closed.".
i didnt use ExecuteReader anywhere in the coding. actually im using 3 files to validate the username and the password of the user. please let me know how to validate the data with the database value. if the username is true i want to redirect to another aspx file, otherwise i must show an error message in the login file.
the codings are:
-------------login.aspx-------------
<%@ Page Language="vb" Codebehind="Login.aspx.vb" AutoEventWireup="false" Inherits="shopping.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Login Form</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server" />
<br>
<br>
<asp:Label ID="lblUserName" Runat="server" text="Enter User Name" />
<asp:TextBox ID="txtUserName" MaxLength="10" TextMode="SingleLine" Runat="server" />
<br>
<asp:Label ID="lblPassword" Runat="server" text="Enter Password" />
<asp:TextBox ID="txtPassword" MaxLength="10" TextMode="Password" Runat="server" />
<br>
<br>
<asp:ImageButton id="LoginBtn" ImageURL="images/signin.gif" runat="server" />
<br>
<br>
<br>
</form>
</body>
</HTML>
-------------------login.aspx.vb---------------
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Security
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents lblUserName As System.Web.UI.WebControls.Label
Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
Protected WithEvents LoginBtn As System.Web.UI.WebControls.ImageButton
Protected WithEvents Message As System.Web.UI.WebControls.Label
Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Public Sub New()
End Sub
Private Sub LoginBtn_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles LoginBtn.Click
Dim mycomponent As shopping.CustomersDB = New shopping.CustomersDB()
Dim custid As String = mycomponent.Login(txtUserName.Text, txtPassword.Text)
If custid = "" Then
txtMessage.Text = "Login Failed"
Else
txtMessage.Text = "Login Success"
End If
End Sub
End Class
-----customer.vb-------
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Namespace shopping
Public Class CustomersDB
Public Function Login(ByVal UserName As String, ByVal Password As String) As String
Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"
Dim myCommand As SqlCommand = New SqlCommand("CustomerLogin", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
Dim parUserName As SqlParameter = New SqlParameter("@UserName", SqlDbType.NVarChar, 10)
parUserName.Value = UserName
myCommand.Parameters.Add(parUserName)
Dim parUserPass As SqlParameter = New SqlParameter("@UserPass", SqlDbType.NVarChar, 10)
parUserPass.Value = Password
myCommand.Parameters.Add(parUserPass)
Dim parameterCustomerID As SqlParameter = New SqlParameter("@CustomerID", SqlDbType.NVarChar, 10)
parameterCustomerID.Direction = ParameterDirection.Output
myCommand.Parameters.Add(parameterCustomerID)
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Dim customerId As String = parameterCustomerID.Value
If customerId = "" Then
Return Nothing
Else
Return customerId
End If
End Function
End Class
End Namespace