Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Imports System.Data.OleDb

Public Class Form1
    Dim cnn As New OleDb.OleDbConnection


    Private Sub RefreshData()
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If

        Dim da As New OleDb.OleDbDataAdapter(" SELECT login as [login], password as [password] FROM login ", cnn)

        Dim dt As New DataTable

        da.Fill(dt)

        dgvData.DataSource = dt
        cnn.Close()
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim cmd As New OleDb.OleDbCommand

        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If

        cmd.Connection = cnn
        cmd.CommandText = " INSERT INTO login ([login], [password]) " & _
                        " VALUES(" & txtLog.Text & "," & txtPass.Text & ")"
        cmd.ExecuteNonQuery() ====> this is where the error occur

        RefreshData()

        cnn.Close()
   
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cnn = New OleDb.OleDbConnection
        Dim sql_query As String
        sql_query = "SELECT * FROM login"

        cnn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source = " & Application.StartupPath & "\Church.mdb"
    End Sub

End Class
Posted
Updated 16-Oct-14 5:59am
v2

Your code is susceptible to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query instead:
VB
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    Dim cmd As New OleDb.OleDbCommand

    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If

    cmd.Connection = cnn
    cmd.CommandText = "INSERT INTO login ([login], [password]) VALUES (?, ?)"
    cmd.Parameters.AddWithValue("p0", txtLog.Text)
    cmd.Parameters.AddWithValue("p1", txtPass.Text)

    cmd.ExecuteNonQuery()

    RefreshData()

    cnn.Close()
End Sub



Once you've fixed that gaping security vulnerability in your code, you need to look at your password storage. You're currently storing passwords in plain-text, which is a terrible idea. You should only ever store a salted hash of the password.

Salted Password Hashing - Doing it Right[^]
 
Share this answer
 
Comments
Maciej Los 16-Oct-14 15:11pm    
Good point! +5!
Hello, let this be a basic example for you, which you should follow suit. All though the solution you choose (Solution 1); worked. You are compromising your whole system by not using parameters. I stress that you change your solution from a security standpoint.

VB
'Please be sure to declare your cnn as your connection.         
            
                Query = "INSERT INTO login ([login], [password]) VALUES (@User, @Pass)"

'Do not concatenate queries. Use Parameters.

'Connect your query command to the connection.

                Dim QueryCom As New OleDb.OleDbCommand(Query, cnn)
                QueryCom.Parameters.AddWithValue("@username", txtLog.Text)

'@username and @guid is a placeholder for the declared declarations UserName and iUserGuid.
                QueryCom.Parameters.AddWithValue("@Pass", txtPass.Text)

'Check if the connection is open or not, if its closed, open it...
'Don't assume something, check it first.

                If cnn.State = ConnectionState.Closed Then
                    cnn.Open()
                End If
                QueryCom.ExecuteNonQuery()

                cnn.Close() 'Close the connection 
                cnn.Dispose() 'Dispose of it and its resources, don't just close it. 

'You should also wrap your communication statements in Try Catch Blocks to handle exceptions which may arise.             
 
Share this answer
 
v3
Comments
Maciej Los 16-Oct-14 15:28pm    
@CodingK, your answer is OK with one exception: there is no sql server database, it's ms access database.
Please, improve your answer to avoid down-voting.
Cheers,
Maciej
[no name] 16-Oct-14 15:36pm    
Done, but I don't see how it makes a difference as the SqlConn was not declared in my code which I instructed to do in the example, and the OP could still have changed his cnn variable name to SqlConn. Rather than changing it several times in my example. ;)

I've renamed it to the OPs original variable name instead.

Maciej Los 16-Oct-14 15:46pm    
(MySqlConn AND SqlConn) != OleDbConn
Different providers, true?
[no name] 16-Oct-14 16:03pm    
Theoretically speaking, yes they are different providers, but it still makes no difference what variable name is declaring the connection type.

I could use these variable names for any of the following connections:
Dim SqlConn As MySqlConnection
Dim SqlConn As OleDbConnection
Dim SqlConn As SqlConnection
And regardless of the variable name, they will all work for the declared type. The variable name is not important, but the variable type represented by that variable is.

Where are you going with this?
Maciej Los 16-Oct-14 16:11pm    
Have a look at this line: Dim QueryCom As New MySqlCommand(Query, cnn)
Do you know what i'm talking about?
And, of course, you're right. The name of variable is not important ;)
On the first look... You have to add a ' character around the string values.
SQL
INSERT INTO login ([login], [password]) 
VALUES('stringValue','stringValue')


In case of numeric values, you have to use this:
SQL
INSERT INTO login ([NumericField1], [NumericField2]) 
VALUES(22,0)


In case of datetime values, you have to use this:
SQL
INSERT INTO login ([DateTimeField1], [DateTimeField2]) 
VALUES(#MM/dd/yyyy#,#MM/dd/yyyy#)


Finally, i suggest to use queries with named parameters[^]:
SQL
PARAMETERS [slogin] CHAR, [sPasswd] CHAR;
INSERT INTO [login] ([login],[password])
VALUES([slogin], sPassword)


Using parameters[^] is very good practice. You can add it via OleDbParameterCollection.AddWithValue Method[^]

Finally, i need to warn you: DO NOT use reserverd words[^]! It might be the reason of several troubles.

Cheers!
 
Share this answer
 
v2
Comments
Member 11158833 16-Oct-14 12:24pm    
thanks for the answer it helped ^^
Richard Deeming 16-Oct-14 13:22pm    
Sorry, but you're encouraging the OP to continue using string concatenation to build SQL queries, so I've had to down-vote your answer.
Maciej Los 16-Oct-14 15:08pm    
Is it better now?
Richard Deeming 16-Oct-14 15:10pm    
Yes. I've updated my vote. :)
Maciej Los 16-Oct-14 15:11pm    
Thank you, Richard ;)

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