A
Arpan
A class file named "Users.vb" has the following code:
Namespace Users
Public Class UserDetails
Public FirstName As String
Public LastName As String
Public UserName As String
Public Password As String
Public UserID As String
End Class
Public Class User
Public Function Login(ByVal UserName As String, ByVal Password
As String) As Integer
Dim iID As Integer
Dim sqlCmd As SqlCommand
Dim sqlConn As SqlConnection
Dim sqlReader As SqlDataReader
sqlConn = New SqlConnection("......")
sqlCmd = New SqlCommand("SELECT ID FROM tblUsers WHERE
UserName='" & UserName & "' AND Password='" & Password & "'", sqlConn)
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader()
Do While (sqlReader.Read)
iID = sqlReader.GetInt32(0)
Loop
sqlConn.Close()
Return iID
End Function
Public Function GetDetails(ByVal UserID As Integer) As
UserDetails
Dim sqlCmd As SqlCommand
Dim uDetails As UserDetails
Dim sqlConn As SqlConnection
Dim sqlReader As SqlDataReader
sqlConn = New SqlConnection("........")
sqlCmd = New SqlCommand("SELECT
FirstName,LastName,UserName,Password FROM tblUsers WHERE ID=" & UserID,
sqlConn)
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
uDetails = New UserDetails
While (sqlReader.Read)
uDetails.FirstName = sqlReader.GetString(0)
uDetails.LastName = sqlReader.GetString(1)
uDetails.UserName = sqlReader.GetString(2)
uDetails.Password = sqlReader.GetString(3)
uDetails.UserID = UserID
End While
sqlReader.Close()
sqlConn.Close()
Return uDetails
End Function
End Class
End Namespace
I successfully compiled the above class into "Users.dll" & this is how
I am using the DLL in an ASPX page:
<%@ Import Namespace="Users" %>
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
Dim boUser As User
Dim boUserDetails As UserDetails
pnlLogin.Visible = True
pnlShowMsg.Visible = False
boUser = New User
boUserDetails = New UserDetails
boUserDetails = boUser.GetDetails(????)
lblMessage.Text = "Hello " & boUserDetails.FirstName
Else
pnlLogin.Visible = False
pnlShowMsg.Visible = True
End If
End Sub
Sub Login(ByVal obj As Object, ByVal ea As EventArgs)
Dim boUser As User
Dim iUID As Integer
boUser = New User
iUID = boUser.Login(txtUserName.Text, txtPassword.Text)
End Sub
</script>
<form runat="server">
<aspanel ID="pnlLogin" runat="server">
UserName: <asp:TextBox ID="txtUserName" runat="server"/><br>
Password: <asp:TextBox ID="txtPassword" TextMode="password"
runat="server"/><br>
<asp:Button ID="btnLogin" OnClick="Login" Text="LOGIN" runat="server"/>
</aspanel>
<aspanel ID="pnlShowMsg" runat="server">
<asp:Label ID="lblMessage" runat="server"/>
</aspanel>
</form>
When the ASPX page loads for the first time, users see the 2 TextBoxes.
When a user enters his UserName & Password, assuming that the UserName
& Password exists in the database table, after executing the following
code line in the Page_Load sub
boUserDetails = boUser.GetDetails(????)
the Label "lblMessage" should display a message but I can't figure out
how to pass the ID (corresponding to the UserName & Password entered by
the user) to the "GetDetails" function which expects an integer ID
parameter under the class named "User". For e.g. if the ID
corresponding to the UserName & Password entered by the user is, say, 5
then the GetDetails line in the Page_Load sub should evaluate to
boUserDetails = boUser.GetDetails(5)
Can someone please tell me how do I pass the ID value to the
"GetDetails" function after the user enters a UserName & Password in
the 2 TextBoxes?
Thanks,
Arpan
Namespace Users
Public Class UserDetails
Public FirstName As String
Public LastName As String
Public UserName As String
Public Password As String
Public UserID As String
End Class
Public Class User
Public Function Login(ByVal UserName As String, ByVal Password
As String) As Integer
Dim iID As Integer
Dim sqlCmd As SqlCommand
Dim sqlConn As SqlConnection
Dim sqlReader As SqlDataReader
sqlConn = New SqlConnection("......")
sqlCmd = New SqlCommand("SELECT ID FROM tblUsers WHERE
UserName='" & UserName & "' AND Password='" & Password & "'", sqlConn)
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader()
Do While (sqlReader.Read)
iID = sqlReader.GetInt32(0)
Loop
sqlConn.Close()
Return iID
End Function
Public Function GetDetails(ByVal UserID As Integer) As
UserDetails
Dim sqlCmd As SqlCommand
Dim uDetails As UserDetails
Dim sqlConn As SqlConnection
Dim sqlReader As SqlDataReader
sqlConn = New SqlConnection("........")
sqlCmd = New SqlCommand("SELECT
FirstName,LastName,UserName,Password FROM tblUsers WHERE ID=" & UserID,
sqlConn)
sqlConn.Open()
sqlReader = sqlCmd.ExecuteReader
uDetails = New UserDetails
While (sqlReader.Read)
uDetails.FirstName = sqlReader.GetString(0)
uDetails.LastName = sqlReader.GetString(1)
uDetails.UserName = sqlReader.GetString(2)
uDetails.Password = sqlReader.GetString(3)
uDetails.UserID = UserID
End While
sqlReader.Close()
sqlConn.Close()
Return uDetails
End Function
End Class
End Namespace
I successfully compiled the above class into "Users.dll" & this is how
I am using the DLL in an ASPX page:
<%@ Import Namespace="Users" %>
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
Dim boUser As User
Dim boUserDetails As UserDetails
pnlLogin.Visible = True
pnlShowMsg.Visible = False
boUser = New User
boUserDetails = New UserDetails
boUserDetails = boUser.GetDetails(????)
lblMessage.Text = "Hello " & boUserDetails.FirstName
Else
pnlLogin.Visible = False
pnlShowMsg.Visible = True
End If
End Sub
Sub Login(ByVal obj As Object, ByVal ea As EventArgs)
Dim boUser As User
Dim iUID As Integer
boUser = New User
iUID = boUser.Login(txtUserName.Text, txtPassword.Text)
End Sub
</script>
<form runat="server">
<aspanel ID="pnlLogin" runat="server">
UserName: <asp:TextBox ID="txtUserName" runat="server"/><br>
Password: <asp:TextBox ID="txtPassword" TextMode="password"
runat="server"/><br>
<asp:Button ID="btnLogin" OnClick="Login" Text="LOGIN" runat="server"/>
</aspanel>
<aspanel ID="pnlShowMsg" runat="server">
<asp:Label ID="lblMessage" runat="server"/>
</aspanel>
</form>
When the ASPX page loads for the first time, users see the 2 TextBoxes.
When a user enters his UserName & Password, assuming that the UserName
& Password exists in the database table, after executing the following
code line in the Page_Load sub
boUserDetails = boUser.GetDetails(????)
the Label "lblMessage" should display a message but I can't figure out
how to pass the ID (corresponding to the UserName & Password entered by
the user) to the "GetDetails" function which expects an integer ID
parameter under the class named "User". For e.g. if the ID
corresponding to the UserName & Password entered by the user is, say, 5
then the GetDetails line in the Page_Load sub should evaluate to
boUserDetails = boUser.GetDetails(5)
Can someone please tell me how do I pass the ID value to the
"GetDetails" function after the user enters a UserName & Password in
the 2 TextBoxes?
Thanks,
Arpan