Solved

insert into sql server vb.net

Posted on 2013-01-18
2
703 Views
Last Modified: 2013-01-18
Hello there. This is my question. After searching the web I found this code for inserting a new record into an SQL Server db, from a control behind VB.NET




            Dim con As New SqlConnection
            Dim cmd As New SqlCommand
            Try
                con.ConnectionString = "Data Source=CDPC1093\CDPC1093;Initial Catalog=tba210;Integrated Security=True"
                con.Open()
                cmd.Connection = con
                cmd.CommandText = "INSERT INTO T_USR(N_USER_NO, S_NAME,S_USER_TYPE,D_REG,D_PERIOD_ST,D_PERIOD_END) VALUES (" & Gedetineerde_systeem_ID.Text & ", '" & Achternaam.Text & "' ,2," & Today & "," & Today & "," & DateAdd(DateInterval.Year, 1, Today)
                cmd.ExecuteNonQuery()

            Catch ex As Exception
                MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
            Finally
                con.Close()
            End Try


The fact is that I am always getting back the error message, meaning the insertion was not successful.

Could any of you guys give me a hand on this? What am I doing wrong or letting behind?
0
Comment
Question by:LUISOFO
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38792956
you would need to post the error message you get, to start the troubleshooting.
I presume you get a syntax error, by inspecting the commandtext you will see that it's not correct.

try:
cmd.CommandText = "INSERT INTO T_USR(N_USER_NO, S_NAME,S_USER_TYPE,D_REG,D_PERIOD_ST,D_PERIOD_END) VALUES (" & Gedetineerde_systeem_ID.Text & ", '" & Achternaam.Text & "' ,2,getdate(),getdate(), dateadd(year,1,getdate()) ) "

Open in new window



first, you where missing the closing ) for the VALUES() part
second, Today will be a "date", but by your " string " & " string" it will be no more a "date" for teh sql (see also this article: http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html )
finally,  you should read up on how to create a SqlCommand with Parameters to avoid SQL injection
0
 

Author Closing Comment

by:LUISOFO
ID: 38793030
Right to the point. Thanks for your input
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now