Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > Use VB.NET to retrieve SQL data.
 

Use VB.NET to retrieve SQL data.

Hello everyone, i've come to the place where i don't know what to do, so i'm asking you. Preety much, i need help with my VB.NET application to be used with SQL.
I will write here i want to do:

1. user writes he's details (textbox1 for username, textbox2 for password)
2. user hits "Login"
3. application tries to retrive data from sql server. (in this case localhost/127.0.0.1)
4. application retrieves data from test/tables/members
5. if the specified account information matches the servers data, it will let you through to the next form. Else, it will give an error that the user/pass is incorrect.

Now look, the table looks like this

[id] | [username] | [password]
1 username password

OR look here at the picture: http://img697.imageshack.us/img697/2931/sqlsetup.png

Thanks for all help everyone!
KaretuN  Monday, November 23, 2009 8:16 PM

Thank you BarbaraR and Malange for your friendly help.


Hi KaretuN,

Welcometo MSDN forums!

Walkthrough/Example: How do I create a Login Verification routine (Login Form) in VB.NET?

1) e.g. Prepare a SQL Server database file myDB.mdf containing a Users table with the following two fields:

Field Name Data Type

Username VarChar(20)

PasswordVarChar(20)

2) Create a new Windows Forms application, then add a “Login Form�template:

Project menu-> Add Windows Form -> Select "Login Form" template

3) Code sample
Prerequisites: txtUsername TextBox, txtPassword TextBox, OK button and Cancel button on Login Form.

Imports System.Data.SqlClient

Public Class LoginForm1

' OK button

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

Dim con As New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=D:\myDB.mdf")

Dim cmd As New SqlCommand("SELECT * FROM Users WHERE Username = '" & txtUsername.Text & "' AND [Password] = '" & txtPassword.Text & "' ", con)

con.Open()

Dim sdr As SqlDataReader = cmd.ExecuteReader()

' If the record can be queried, it means passing verification, then open another form.

If (sdr.Read() = True) Then

MessageBox.Show("The user is valid!")

Dim mainForm As New MainForm

mainForm.Show()

Me.Hide()

Else

MessageBox.Show("Invalid username or password!")

End If

End Sub

' Cancel button

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

Me.Close()

End Sub

End Class



Check this FAQ for detailed instruction:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/b2eaa4a2-a202-4cad-9673-75a0d35d79fe


Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

Martin Xie - MSFT  Monday, November 30, 2009 9:54 AM
I gather you're stuck at the actual SQL Data retrieval steps.
check out the ADO.NET sample application from Microsoft.

http://msdn.microsoft.com/en-us/library/dw70f090%28VS.71%29.aspx

the VB sample code at the top is the basic logic for executing a simple query against SQL db.


the basic steps in the sample are:
1) Create connection to your SQL db
2) Create a SQL command object
3) Put SQL query into command object
4) open the connection
5) Execute the query putting results into Sqlata Reader
6) Read through results
7) release resources (close methods)







BarbaraR  Monday, November 23, 2009 8:51 PM
To be honest, i do not understand at all how to use it really, maybe you can help me a little bit ?

Thanks
KaretuN  Monday, November 23, 2009 10:57 PM
how far are you with your project? Do You have any code so far? Seems to be you don't understand anything about SQL and Developing .Net. If that is the case you need to do a research, have a look here please:
http://www.java2s.com/Code/VB/CatalogVB.htm

Tehy explain you step by step...

All the best for you.

Don't judge me, just Upgrade me. Thanks!
Malange  Tuesday, November 24, 2009 10:40 PM

Thank you BarbaraR and Malange for your friendly help.


Hi KaretuN,

Welcometo MSDN forums!

Walkthrough/Example: How do I create a Login Verification routine (Login Form) in VB.NET?

1) e.g. Prepare a SQL Server database file myDB.mdf containing a Users table with the following two fields:

Field Name Data Type

Username VarChar(20)

PasswordVarChar(20)

2) Create a new Windows Forms application, then add a “Login Form�template:

Project menu-> Add Windows Form -> Select "Login Form" template

3) Code sample
Prerequisites: txtUsername TextBox, txtPassword TextBox, OK button and Cancel button on Login Form.

Imports System.Data.SqlClient

Public Class LoginForm1

' OK button

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

Dim con As New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=D:\myDB.mdf")

Dim cmd As New SqlCommand("SELECT * FROM Users WHERE Username = '" & txtUsername.Text & "' AND [Password] = '" & txtPassword.Text & "' ", con)

con.Open()

Dim sdr As SqlDataReader = cmd.ExecuteReader()

' If the record can be queried, it means passing verification, then open another form.

If (sdr.Read() = True) Then

MessageBox.Show("The user is valid!")

Dim mainForm As New MainForm

mainForm.Show()

Me.Hide()

Else

MessageBox.Show("Invalid username or password!")

End If

End Sub

' Cancel button

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

Me.Close()

End Sub

End Class



Check this FAQ for detailed instruction:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/b2eaa4a2-a202-4cad-9673-75a0d35d79fe


Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

Martin Xie - MSFT  Monday, November 30, 2009 9:54 AM
Additionally, here are four methods tomake simple Data Access application(Next, Previous, First, Last, Update,Delete,Insert, Save) for you to check, which may be helpful to you.

Method 1:Update (Insert/Update/Delete) data back into MS Access database from DataGridView.
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/5980181e-f666-4f0a-ab50-c4ebecf96f02/

ImportsSystem.Data.OleDb

PublicClassForm1

DimmyDAAsOleDbDataAdapter

DimmyDataSetAsDataSet

PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load

DimconAsOleDbConnection=NewOleDbConnection("Provider=Microsoft.jet.oledb.4.0;datasource=|DataDirectory|\myDB.mdb")

DimcmdAsOleDbCommand=NewOleDbCommand("SELECT*FROMTable1",con)

con.Open()

myDA=NewOleDbDataAdapter(cmd)

'AutomaticallygeneratesDeleteCommand,UpdateCommandandInsertCommandforDataAdapterobject

DimbuilderAsOleDbCommandBuilder=NewOleDbCommandBuilder(myDA)

myDataSet=NewDataSet()

myDA.Fill(myDataSet,"MyTable")

DataGridView1.DataSource=myDataSet.Tables("MyTable").DefaultView

con.Close()

con=Nothing

EndSub

'Savedatabackintodatabase

PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click

Me.Validate()

Me.myDA.Update(Me.myDataSet.Tables("MyTable"))

Me.myDataSet.AcceptChanges()

EndSub

EndClass



Method 2: Execute Select/Insert/Delete/Update T-SQL commands in VB.NET code
Code sample: How to Select/Insert/Delete/Update records in MS Access database in VB.NET
http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/87913f28-992d-4705-963b-cb0ffa53d8dd/
Code sample: How to Select/Insert/Delete/Update records in SQL Server database in VB.NET
http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/89e1067d-16e7-44e8-b12d-d78845bf255f/


Method 3
.
Using Data Wizard with BindingNavigator control.
Please check the 11th post in this threadfor detailed walkthrough
:
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/ff3f953b-da66-4f03-b4e4-981bab7d783b/




Method 4. Using DataSet/DataTable/DataAdapter in VB.NET code
Please check the 12th post and 13th post in this thread for detailed code sample:
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/ff3f953b-da66-4f03-b4e4-981bab7d783b/

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Monday, November 30, 2009 9:58 AM
Thanks for the help Martin!!! This is exactly what i've been looking for for a long time. Thanks for everything!
KaretuN  Monday, November 30, 2009 5:26 PM

Martin, i have a problem now. Database is created, and i've added 2 users to the table. But i'm getting this error while trying to fetch data from DB:

"A network-related or instance-specific error occured while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server.)"

KaretuN  Monday, November 30, 2009 5:59 PM

Hi KaretuN,

You're welcome!

1. If you use connection string as below, please try unattaching thedatabase file from SQL Server.
Dim con As New SqlConnection("Data Source=.;Integrated Security=True;AttachDbFilename=D:\myDB.mdf")

2. You can try other connnection string specifying SQL Server name and the Instance namelike this:

Data Source=ServerName;Initial Catalog=databaseName;Persist Security Info=True;User ID=myUserName;Password=mypassword!
Data Source=ServerName\SQLEXPRESSInstanceName;Integrated Security=True;AttachDbFilename=D:\myDB.mdf



Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Tuesday, December 01, 2009 6:55 AM
Finally martin, i've got it to work! Now, one last question! i'm releasing this application i'm coding online, so i would like to know these below:

1. How do i make the server reachble remotely ?

2. I have a registration form on the application, where i want the application to once again, connect to sql, and there after add a new line to users table.
the form consists of

1. Username textbox(named TextboxX1), Password textbox (named TextBoxX2), Create Account button (named ButtonX1)


KaretuN  13 hours 27 minutes ago

Hi KaretuN,

Please check:

1. How to configure SQL Server to allow remote connections
http://support.microsoft.com/kb/914277
http://www.byteblocks.com/post/2009/04/02/Configure-SQL-Server-to-allow-remote-connections.aspx

2. How to create Login Form and Add/Delete an User record?
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/f54783d2-a236-421d-8b2d-aa6779ceede2/


Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  1 hour 57 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• How to determine sender control in DragDrop event of a DataGridView
• Making and printing Reports from database
• microsoft.data.connectionUi
• Download Monitor app
• Host Application Online?
• ListView Image is blurring
• Frustrating hex problem
• Random Generate Quiz
• Displaying ALL drives for multiple computers
• Want to Learn Visual Basic 2008 through Videos