Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am working on VB.net 2008 i have not worked in vb.net so please help me
i have facing issue in login
The code i have written is :

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        'create connection to the database server
        Dim con As New SqlClient.SqlConnection
        Dim strcon As String = "data source=aarti-pc\sqlexpress.Invoice.dbo"
        Dim strcommand As String = "select * from login"
        Try
            Dim cm As New SqlClient.SqlConnection(strcommand, con)
            con.Open()
            cm.Executenonquery()
            MsgBox.show("You have Log In!!")
        Catch ex As Exception
            MsgBox.Show(ex.Message, "Error")
        Finally
            If con.State = ConnectionState.Open Then
                con.Close()

            End If
        End Try

    End Sub


Please can any one correct my code

Thanks in advance :)
Posted
Comments
Sandeep Mewara 25-Apr-11 0:59am    
What do mean can you correct! You have to be specific in the issue faced.

How can we correct it? It does nothing useful.

All it will do is show a message box "Error"!

There are many things wrong here:
1) You don't use the connection string to connect to the database.
2) If that did work, you try to read all the login table records. Provided that doesn't actually fail, you assume the user can log in.
3) You try to read all the records, but you throw away all the data...

Go back to your course notes, and read them this time. Properly!
 
Share this answer
 
Comments
aayu 24-Apr-11 15:57pm    
ok fine
Following Griffs answer you cant query the database with

VB
Dim cm As New SqlClient.SqlConnection(strcommand, con)


you would use the sqlcommand object like so

VB
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using
End Sub


this example was taken from SQL Connection Class[^]

Also your not returning a resultset from the database as you are executing a non query .i.e no results are to be return except the number of rows that are return. Have a look at SQL Data Reader Class[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900