Solved

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

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 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