Solved

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

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

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

13 Experts available now in Live!

Get 1:1 Help Now