Solved

VB.net SqlClient insert query

Posted on 2011-09-15
3
442 Views
Last Modified: 2012-05-12
Hi

I am using the following code to insert a record into an online
SQL database. The MsgBox reads
"Records Inserted 1"
Is this a good way to check if the insert was successful?
If it isn't successful would should I expect?
If this isn't a good way is there a better way?
Thanks
Sub oInsert()
        Dim ra As Integer
     
        Dim oDate As String = "03/03/2011" 'Date.Now.ToString
        Dim oText As String = "sqlclient"
        Dim oNumber As Decimal = 100
        myConnection.Open()
        Dim sSQL As String
        sSQL = "Insert Into Table1 ([oDate],[oText], [oNumber]) Select '" & oDate & "' as Expr1, '" & oText & "' as Expr2, " & oNumber & " as Expr3"
        myCommand = New SqlCommand(sSQL, myConnection)
        ra = myCommand.ExecuteNonQuery()
        'Since no value is returned we use ExecuteNonQuery
        MsgBox("Records Inserted " & ra)

        myConnection.Close()
    End Sub

Open in new window

0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 36546457
Well, since ExecuteNonQuery returns the number of rows affected and your statement is an insert statement, you have successfully inserted 1 record. If the insert didn't work, you'd get 0 back.
0
 

Author Closing Comment

by:Murray Brown
ID: 36546503
Thanks. Just wanted to confirm that
0
 
LVL 40
ID: 36546528
This is the recommended way, but you should add an error trap around everything you do on the server.

A lot of things can happen when you communicate with a server, so you should be prepared for it. As a minimum, the following:

Try
      myConnection.Open()
        Dim sSQL As String
        sSQL = "Insert Into Table1 ([oDate],[oText], [oNumber]) Select '" & oDate & "' as Expr1, '" & oText & "' as Expr2, " & oNumber & " as Expr3"
        myCommand = New SqlCommand(sSQL, myConnection)
        ra = myCommand.ExecuteNonQuery()
Catch ex As SqlException
        MessageBox.Show("The following error occured while insterting the record: " & ex.Message)
        'Handle errors
End Try
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question