Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,068 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,808 people online right now. Registration is fast and FREE... Join Now!




SQL Command

 
Reply to this topicStart new topic

SQL Command

rch
15 Feb, 2008 - 06:04 AM
Post #1

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 4

Hi all!

Okay - firstly thanks in advance for all the help that you may give me with this. Am trying to insert data in an access DB like so...

CODE

Imports System.Data
Imports System.Data.OleDb
Public Class form1
    'DB  variables'
    Dim mypath = Application.StartupPath & "\db.mdb"
    Dim mypassword = ""
    Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)
    Dim cmd As OleDbCommand

    Private Sub addnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNewUser.Click
             Dim sql = "INSERT INTO users (username, password, usrlvel) VALUES (username='" & txtUsername.Text & "', password='" & txtPassword.Text & "' AND usrlevel='" & txtusrLevel.Text & "')"


        conn.Open()

        cmd = New OleDbCommand(sql, conn)
        conn.Close()
    End Sub
End Class


iT'S coming up with a syntax error, but I can't see anything wrong with it

Thanks in advance!

This post has been edited by rch: 15 Feb, 2008 - 06:05 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: SQL Command
15 Feb, 2008 - 06:06 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,160



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(rch @ 15 Feb, 2008 - 07:04 AM) *

Okay - firstly thanks in advance for all the help that you may give me with this. Am trying to insert data in a

Welcome to Dream In Code!

I'm not sure what your exact question is.... maybe this will help
http://www.w3schools.com/sql/sql_insert.asp
User is online!Profile CardPM
+Quote Post

rch
RE: SQL Command
15 Feb, 2008 - 07:21 AM
Post #3

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 4

Hey,

What Im asking is that VB.net is telling me I have a syntax error in this:

QUOTE
Dim sql = "INSERT INTO users (username, password, usrlvel) VALUES (username='" & txtUsername.Text & "', password='" & txtPassword.Text & "' AND usrlevel='" & txtusrLevel.Text & "')"



I've looked at lots of doc's, and I just don't get it

Cheers

This post has been edited by rch: 15 Feb, 2008 - 07:42 AM
User is offlineProfile CardPM
+Quote Post

stressnode
RE: SQL Command
15 Feb, 2008 - 09:46 AM
Post #4

New D.I.C Head
Group Icon

Joined: 29 Jun, 2007
Posts: 2


Dream Kudos: 25
My Contributions
Hello ...

Your SQL Statment has error ... it must be like this

CODE
Dim sql = "INSERT INTO users (username, password, usrlvel) VALUES ('" & txtUsername.Text & "','" & txtPassword.Text & "','" & txtusrLevel.Text & "')"


And also ... you need to supply

CODE
cmd.ExecuteNoneQuery()


after assigning the sql statment and the connection to the command.

CODE
conn.Open()

cmd = New OleDbCommand(sql, conn)

cmd.ExecuteNoneQuery()

conn.Close()

User is offlineProfile CardPM
+Quote Post

rch
RE: SQL Command
28 Feb, 2008 - 05:59 AM
Post #5

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 4

Hey all,

Thanks for the replies - but Im still getting the same error.

Any more wisdom?

Cheers
User is offlineProfile CardPM
+Quote Post

Jayman
RE: SQL Command
28 Feb, 2008 - 08:57 AM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Post the exact error message that you are getting that will help us guide you in this matter.
User is offlineProfile CardPM
+Quote Post

davegeek
RE: SQL Command
28 Feb, 2008 - 11:31 AM
Post #7

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
@ rch:
CODE

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)

don't u think that u have got to specify the data source here?

Moreover, when u get the error in Visual Studio u can go to the exact description of this error with the possible resolution.

This post has been edited by davegeek: 28 Feb, 2008 - 11:33 AM
User is offlineProfile CardPM
+Quote Post

rch
RE: SQL Command
4 Mar, 2008 - 05:20 AM
Post #8

New D.I.C Head
*

Joined: 15 Feb, 2008
Posts: 4

QUOTE(davegeek @ 28 Feb, 2008 - 12:31 PM) *

@ rch:
CODE

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)

don't u think that u have got to specify the data source here?

Moreover, when u get the error in Visual Studio u can go to the exact description of this error with the possible resolution.



Haha, of course I have specified the data source. The variable mypath is in there, which specifies the source;)

And I would do the whole vb.net solution lookup. Unfortunatley though it doesn't work at college..

And the error message once again is:

Syntax error in INSERT INTO statement. (oledbexception was unhandled)./

Thanks again for all your help
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: SQL Command
4 Mar, 2008 - 05:57 AM
Post #9

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
That wold be because you're doing your INSERT incorrectly. When you get to the VALUES() part you dont do like you're doing with the username=. Your statement should look like:


vb

Dim sql = "INSERT INTO users (username, password, usrlvel) " _
VALUES ('" & txtUsername.Text & "', '" & txtPassword.Text & "','" & txtusrLevel.Text & "')"



Here is some more information on SQL's INSERT statement smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month