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

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




Help with SQL ExecuteNonQuery in VB.Net

 
Reply to this topicStart new topic

Help with SQL ExecuteNonQuery in VB.Net

nofear217
25 Jan, 2008 - 09:02 AM
Post #1

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
CODE
Dim envDated As New String(dtDate.Value.Date.ToString)
        Dim envInsert As New SqlCommand
        envInsert.Connection = SqlConnection1
        envInsert.CommandType = CommandType.Text
        envInsert.CommandText = _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('envDated', '1', 'txt1.Text'), " & _
        "('envDated', '2', 'txt2.Text'), " & _
        "('envDated', '3', 'txt3.Text'), " & _
        "('envDated', '4', 'txt4.Text'), " & _
        "('envDated', '5', 'txt5.Text'), " & _
        "('envDated', '6', 'txt6.Text'), " & _
        "('envDated', '7', 'txt7.Text'), " & _
        "('envDated', '8', 'txt8.Text'), " & _
        "('envDated', '9', 'txt9.Text'), " & _
        "('envDated', '10', 'txt10.Text'), " & _
        "('envDated', '11', 'txt11.Text'), " & _
        "('envDated', '12', 'txt12.Text'), " & _
        "('envDated', '13', 'txt13.Text'), " & _
        "('envDated', '14', 'txt14.Text'), " & _
        "('envDated', '15', 'txt15.Text'), " & _
        "('envDated', '16', 'txt16.Text'), " & _
        "('envDated', '17', 'txt17.Text'), " & _
        "('envDated', '18', 'txt18.Text'), " & _
        "('envDated', '19', 'txt19.Text'), " & _
        "('envDated', '20', 'txt20.Text'), " & _
        "('envDated', '21', 'txt21.Text'), " & _
        "('envDated', '22', 'txt22.Text'), " & _
        "('envDated', '23', 'txt23.Text'), " & _
        "('envDated', '24', 'txt24.Text')"
        envInsert.Connection.Open()
        envInsert.ExecuteNonQuery()
        envInsert.Connection.Close()


Getting an unhandled error....Incorrect syntax near ",". Any help or point in the right direction would be greatly appreciated.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Help With SQL ExecuteNonQuery In VB.Net
25 Jan, 2008 - 10:04 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
The syntax you're using for insert is non standard SQL; I believe MySQL supports something like that. However, you're using SqlCommand, which is Microsoft product specific, so it's a given it wont work.

I'm afraid you'll usually have to call the command once for each of those inserts. Look into binding parameter values for the most efficient way to do this.

Hope this helps.

User is online!Profile CardPM
+Quote Post

Sothrie
RE: Help With SQL ExecuteNonQuery In VB.Net
25 Jan, 2008 - 12:53 PM
Post #3

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Yeah, there's no Insert statement like that. But there is a little bit of a shortcut. Instead of having to run more than 20 commands, you can just write all the commands into the same command text delimeted by semicolons. Your command text then would look something like this:
CODE

envInsert.CommandText = _
"INSERT INTO TempTable (envDate, envType, envTotal) VALUES ('Thing', '234', 'Other'); " & vbCrLf & _
"INSERT INTO TempTable (envDate, envType, envTotal) VALUES ('Thin2', '235', 'SomeOther'); " & vbCrLf & _
"INSERT INTO TempTable (envDate, envType, envTotal) VALUES ('Thin3', '236', 'AnOther'); "
'And continue like that


So that's kind of a shortcut. Still long, but shorterish.
Hope that helps!
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Help With SQL ExecuteNonQuery In VB.Net
30 Jan, 2008 - 01:50 PM
Post #4

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
ended up going with something like this:
CODE
"INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt1.Tag & _
        "', '" & txt1.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt2.Tag & _
        "', '" & txt2.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt3.Tag & _
        "', '" & txt3.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt4.Tag & _
        "', '" & txt4.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt5.Tag & _
        "', '" & txt5.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt6.Tag & _
        "', '" & txt6.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt7.Tag & _
        "', '" & txt7.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt8.Tag & _
        "', '" & txt8.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt9.Tag & _
        "', '" & txt9.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt10.Tag & _
        "', '" & txt10.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt11.Tag & _
        "', '" & txt11.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt12.Tag & _
        "', '" & txt12.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt13.Tag & _
        "', '" & txt13.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt14.Tag & _
        "', '" & txt14.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt15.Tag & _
        "', '" & txt15.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt16.Tag & _
        "', '" & txt16.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt17.Tag & _
        "', '" & txt17.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt18.Tag & _
        "', '" & txt18.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt19.Tag & _
        "', '" & txt19.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt20.Tag & _
        "', '" & txt20.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt21.Tag & _
        "', '" & txt21.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt22.Tag & _
        "', '" & txt22.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt23.Tag & _
        "', '" & txt23.Text & "'); " & vbCrLf & _
        "INSERT INTO envDataTotals (envDate, envType, envTotal) " & _
        "VALUES ('" & CType(dtDate.Value.Date.ToString(), DateTime) & "', '" & txt24.Tag & _
        "', '" & txt24.Text & "')"


that worked out fine for my purposes...thanks for the help cool.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Help With SQL ExecuteNonQuery In VB.Net
30 Jan, 2008 - 02:58 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Glad it worked for you. Still, not over secure.

I believe, in TextBox txt22, I'll put the value "'); delete envDataTotals;" wink2.gif

You should really look into reusing the SqlCommand with some parameters, if you're doing anything serious.

User is online!Profile CardPM
+Quote Post

nofear217
RE: Help With SQL ExecuteNonQuery In VB.Net
31 Jan, 2008 - 10:20 AM
Post #6

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
For this program I'm not doing anything especially serious but SQL injection was something that bothered me although I'm still fairly new in the VB.Net coding world and I'm learning the ropes...still having a little trouble finding any good articles on what you are talking about though, if you know of any off hand would be greatly appreciated. Oh, and Grawp says hi.

P.S.--The text boxes only allow numerics also.....now that I think about it


And consequently, that statement that you used threw an unhandled exception but nothing hit SQL server. I've got numeric qualifiers on all the text boxes and the executenonquery wrapped in a try-catch.

This post has been edited by nofear217: 31 Jan, 2008 - 10:35 AM
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Help With SQL ExecuteNonQuery In VB.Net
1 Feb, 2008 - 01:41 PM
Post #7

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
baavgai,

I took your advice in a manner of speaking, I'm not using parameterized SQL, but I'm rewriting the SQL stuff in LINQ which I've found very manageable and helps with strong typing the input....and it also allows me to use logic on what to insert into the database and what not to and can still use the ErrorProvider for numeric checking.
CODE
Dim db As New EEUDataContext("***  :P")

        Try

            If txt1.Text <> 0 Or txt1.Text <> "" Then
                Dim type1 As New envDataTotal
                type1.envDate = CType(dtDate.Value.Date, DateTime)
                type1.envType = txt1.Tag
                type1.envTotal = txt1.Text
                db.envDataTotals.InsertOnSubmit(type1)
            End If

            If txt2.Text <> 0 Or txt2.Text <> "" Then
                Dim type2 As New envDataTotal
                type2.envDate = CType(dtDate.Value.Date, DateTime)
                type2.envType = txt2.Tag
                type2.envTotal = txt2.Text
                db.envDataTotals.InsertOnSubmit(type2)
            End If
.
.
.
            db.SubmitChanges()

            Catch ex As Exception

            End Try

Again, thanks for the help.

This post has been edited by nofear217: 1 Feb, 2008 - 02:42 PM
User is offlineProfile CardPM
+Quote Post

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

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