String concatenation in SQL using VB.NET

vuhua

Member
Joined
May 15, 2006
Messages
7
Programming Experience
Beginner
Hello Friend,

I have a problem writing a string into a MS Access database. The code is in SQL but the platform is in VB.NET. Below is my code.

OleDataAdapter.InsertCommand.CommandText = _
"INSERT INTO Expense(Amount, Dates, Debit, Credit, " & _
"ExpenseType, PaymentMethod, Summary) " & _
"VALUES(" & Val(txtAmount.Text) & " , " & _
"" &
CType(txtDate.Text, DateTime) & " , " & _
"'" & radDebit.Checked & "' , '" & radCredit.Checked & "' , " & _
"'" & cboExpType.SelectedItem & "' , " & _
"'" & cboPayMethod.SelectedItem & "' , " & _
"" & txtSummary.Text & ""

Problem: The problem is that the code work just fine. However, when I try to enter a string in the summary box that contain a string ' such as don't, it will not work. I know that string concatenation in SQL is not good. Is there an esay way to fix this so I can enter text into a database that contain word such as don't, doesn't (where an extra ' is being added)?
 
The values you put into the Insert statement need to be in the square parenthese

For example...

INSERT INTO YOUR_TABLE
column1, column2, column3
VALUES
[textbox1.text], [textbox2.text], [textbox3.text]


Hope that helps...
 
Thanks,

However, I'm not able to get the code to work when adding the [] to the code. Would you please give me some example using my code listed in the thread? Thanks
 
Check out this article. ... it shows how to use a parameterized query to perform a search. You should be able to adapt it to do an insert.

-tg
 
Back
Top