Solved

Inserting windows form data into SQL Server 2008 table in vb.net

Posted on 2014-02-19
1
272 Views
Last Modified: 2014-02-20
Hi all.

I have the code below that inserts data into a SQL table using Visual Studio 2010. It works perfectly but I had a question concerning the & and + in the Insert Into part of the code.

In my code I used the &, but I've seen online some people use + and I tested my code with + and it also worked fine.

Does it matter which one I use, what is the best practice? Thank you in advance.

Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = "Data Source=myserver;Initial Catalog=mydb;Integrated Security=True"

        con.Open()

        cmd.Connection = con

        cmd.CommandText = "INSERT INTO ItemQuote(MasterItemNumber) VALUES ('" & txtMasterItemNo.Text & "')"
        cmd.ExecuteNonQuery()

        MsgBox("Item has been submitted")

        con.Close()

Open in new window

0
Comment
Question by:printmedia
1 Comment
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39871198
This has nothing to do with inserting data into SQL Server. Your question is about concatenating strings in VB.Net.

& and + are both concatenation operators. The problem comes in if you try to mix data types.

Dim s, t As String
Dim i As Integer

s1 = "Hello"
i = 1

t = s1 + i

Open in new window


The last line will throw an exception because i is an integer so it assumes the + means addition and will try to cast s to an integer, which it can't do.

Generally you should use & for string concatenation in VB.NET to avoid ambiguity.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

19 Experts available now in Live!

Get 1:1 Help Now