• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

New to .NET

ASP.NET Web Application
VB.NET / SQL Server 2000

tblLogin in the database PCHL
txtUser, txtPass, and cmdRun

I've got a web form with 2 text boxes and a button. When the button is clicked by a user
I want the application to check the DB, find a match for txtUser in the column 'username' then check to see if it correlates with the text entered in txtPass and the column 'password 'in the DB. How would I go about writing the code behind for this button? Is there anything else I need to consider that I wouldn't have had to in VB6? Can anyone give me an example of code and/or some good resources for .NET beginners? Thanks

M Dub
0
Mike Miller
Asked:
Mike Miller
3 Solutions
 
planoczCommented:
Hi,

Here is where I found good examples for getting started in VB.Net

.....http://msdn.microsoft.com/vbasic/downloads/samples/101samples.aspx

Link for ASP programs...

http://msdn.microsoft.com/asp.net/downloads/kits/default.aspx 
0
 
Bob LearnedCommented:
You need to:

(1) Create SQLConnection object with the appropriate connection information to SQL Server database.
(2) Create SQLCommand object to query the table for the password for a particular user.
(3) SQLCommand.ExecuteScalar to return the password into a string variable.
(4) Check the value in the TextBox against the value returned from the database.

Things to think about:
(1) Stored procedure in SQL Server
(2) Encrypted passwords - stored hashed password, then encrypt the plain text password and compare.  This way you don't ever see the real password as plain text.

Bob
0
 
mani_saiCommented:
Here is the sample code:


1) you have to Import namespace first

Imports System.Data.SqlClient

2)'Button click event code:

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

'Retrive the values from textboxes
Dim Userid as string = Textbox1.Text
Dim Password as string = Textbox2.Text

'define the connection(use your sql server connection string)
Dim objconn As SqlConnection = New SqlConnection("Data Source=isdc01;Initial Catalog=Internet_SMS;user id=sa;pwd=;")

'opens a dataset
Dim ds As New DataSet

'open a dataadapter
Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from tbl_Login", objconn) 'use your table name

'Fill the dataset
da.Fill(ds)

'open the connection
objconn.Open()

'define a datarow
Dim LoginName As DataRow

'loop the dataset

For Each LoginName In ds.Tables(0).Rows

If  Userid  =  LoginName("LoginName")  and  Password  =  LoginName("Password") Then

'login success
Response.Redirect("WelcomePage.aspx")  'redirect to your page.

'Clean Up
dr.Close()
objconn.Close()

Exit Sub

Next


lblResult.Text = "You have entered incorrect Log-in ID/Password! Try Again!"

'Clean Up
dr.Close()
objconn.Close()
End Sub



0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
caball88Commented:
it sounds like you are tring to create a login form, this might be a little off topic but also investigate forms authentication. this is a great feature that will allow you to protect your aspx pages.
0
 
Mike MillerSoftware EngineerAuthor Commented:
mani_sai:

In your code...
What is 'dr'???
Was it your intention not to end your IF statement?


caball88:
It is sort of a login form. Really I'm just trying to learn how to connect to a DB and use the data in an application. Any relative example would help. I'm not necessarily trying to build a login page for an application. Just trying to learn :-)
0
 
planoczCommented:
The Links that I posted should serve your purpose.
0
 
mani_saiCommented:
Sorry for the typo Error.

dr stands for SQlDataReader.

Here is the modifed code.

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

'Retrive the values from textboxes
Dim Userid as string = Textbox1.Text
Dim Password as string = Textbox2.Text

'define the connection(use your sql server connection string)
Dim objconn As SqlConnection = New SqlConnection("Data Source=isdc01;Initial Catalog=Internet_SMS;user id=sa;pwd=;")

'opens a dataset
Dim ds As New DataSet

'open a dataadapter
Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from tbl_Login", objconn) 'use your table name

'open a sql datareader
Dim dr as SqlDataReader

'Fill the dataset
da.Fill(ds)

'open the connection
objconn.Open()

'define a datarow
Dim LoginName As DataRow

'loop the dataset

For Each LoginName In ds.Tables(0).Rows

If  Userid  =  LoginName("LoginName")  and  Password  =  LoginName("Password") Then

'login success
Response.Redirect("WelcomePage.aspx")  'redirect to your page.

'Clean Up
dr.Close()
objconn.Close()

Exit Sub  'exit the loop here if you find a match

End if

Next


lblResult.Text = "You have entered incorrect Log-in ID/Password! Try Again!"

'Clean Up
dr.Close()
objconn.Close()
End Sub


good Luck

0
 
mani_saiCommented:
Sorry please neglect my previous code.
Actually dr--datareader is not required  in this scenario.


Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

'Retrive the values from textboxes
Dim Userid as string = Textbox1.Text
Dim Password as string = Textbox2.Text

'define the connection(use your sql server connection string)
Dim objconn As SqlConnection = New SqlConnection("Data Source=isdc01;Initial Catalog=Internet_SMS;user id=sa;pwd=;")

'opens a dataset
Dim ds As New DataSet

'open a dataadapter
Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from tbl_Login", objconn) 'use your table name

'Fill the dataset
da.Fill(ds)

'open the connection
objconn.Open()

'define a datarow
Dim LoginName As DataRow

'loop the dataset

For Each LoginName In ds.Tables(0).Rows

If  Userid  =  LoginName("LoginName")  and  Password  =  LoginName("Password") Then

'login success
Response.Redirect("WelcomePage.aspx")  'redirect to your page if there is a match.

'Clean Up
objconn.Close()

Exit Sub  'exit the loop here if you find a match

End if

Next


lblResult.Text = "You have entered incorrect Log-in ID/Password! Try Again!"

'Clean Up
objconn.Close()
End Sub

good luck.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now