Solved

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

Posted on 2014-02-19
1
277 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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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