Solved

Compare data entered in two textbox against fields in a SQL Server table

Posted on 2009-06-27
5
374 Views
Last Modified: 2013-11-27
Please HELP and new to Visual Studios... I am working on comparing a Username textbox and a Password textbox against fields in a SQL Server database table. Table has two fields (Username and Passward)  and Project has textbox (Username) and (Password).  Login button needs to execute this.  PLEASE, How can I do this? PLEASE help with CODE?  
I found this example but I can not get it to work....
 
Public Class Form1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Close()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Form2.Show()
        'Me.Hide()
        Dim concheck As SqlConnection
        Dim cmdcheck As SqlCommand
        Dim intused As Integer
 
        concheck = New SqlConnection("")
        concheck.Open()
        cmdcheck = New SqlCommand("Select Count(username) From Userinfo Where username='" & username.Text & "'", concheck)
        cmdcheck.ExecuteScalar()
 
        intused = cmdcheck.ExecuteScalar()
 
        If intused > 0 Then
            concheck.Close()
            Dim dtrcheck As SqlDataReader
            concheck = New SqlConnection("Server=")
            concheck.Open()
            cmdcheck = New SqlCommand("Select Username, Password From Userinfo Where username='" & username.Text & "'", concheck)
            dtrcheck = cmdcheck.ExecuteReader
            Dim strdatabasepassword = dtrcheck("Password")
            Dim strtextboxpassword = password.Text
 
            If strdatabasepassword = strtextboxpassword Then
                If password.Text = dtrcheck("Password") Then
                    While dtrcheck.Read
                        Response.Write(strdatabasepassword)
                        Response.Write(strtextboxpassword)
                    End While
                    concheck.Close()
                    Dim newCookie As HttpCookie = New HttpCookie("userinfo")
                    newCookie.Values.Add("username", username.Text)
                    newCookie.Expires = Now.AddHours(4)
                    Response.Cookies.Add(newCookie)
                    Response.Redirect("default.aspx")
 
                Else
 
                    concheck.Close()
                    Response.Write("Invalid Username or Password")
 
                End If
            Else
                concheck.Close()
                Response.Write("Username is not in our system")
            End If
    End Sub
End Class

Open in new window

0
Comment
Question by:jparej73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:nasserd
ID: 24730081
You should create a stored procedure which matches values passed in (from the application).

From security and performance standpoints, do not pull credentials from the database.  Instead, pass them into the SQL command as parameters.

Have the SQL command return a count of matches to both username and password.
0
 
LVL 5

Expert Comment

by:rizwanidrees
ID: 24730582
you are writing code for a web application or desktop application?
0
 

Author Comment

by:jparej73
ID: 24731567
For desktop application
0
 

Author Comment

by:jparej73
ID: 24731571
You should create a stored procedure which matches values passed in (from the application).

From security and performance standpoints, do not pull credentials from the database.  Instead, pass them into the SQL command as parameters.

Have the SQL command return a count of matches to both username and password
 
Can you give an example?  I have no clue how to do this
0
 
LVL 4

Accepted Solution

by:
nasserd earned 500 total points
ID: 24732653
At the very least, you only need 1 SQL command:
"SELECT Count(username) FROM Userinfo WHERE username='" & username.Text & "' AND password='" & password.Text & "';"

The response will be a scalar value of 0 or more; if >0 then your person exists.
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Agent Job Error 13 83
Moving away from Access 2003 adp files 4 48
VMware PVSCSI SQL Server 2016 AlwaysOn 2 37
Do not display comma when no last name 8 48
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question