Hi
I have a dataset (MyDataSet400) which I use as input to a SQL table called tblDRAAS400. I use the following code to loop through the dataset to populate the table. This works fine until one of the fields in the dataset contains an single quote, i.e. “O’BRIEN”. Here’s my code:
Dim dsCounter As Integer
With MyDataSet400.Tables(0)
For dsCounter = 0 To .Rows.Count - 1 'loop
Sql2 = "INSERT INTO tblDRAAS400 ([Account Code], [Credit
Limit], [Payment Terms], [Oldest Invoice]) VALUES ('" & MyDataSet400.Tables(0).Rows(dsCounter).Item(0) & "', '" & MyDataSet400.Tables(0).Rows(dsCounter).Item(1) & "', '" & MyDataSet400.Tables(0).Rows(dsCounter).Item(2) & "', '" & MyDataSet400.Tables(0).Rows(dsCounter).Item(3) & "')"
MyDataAdapter = New System.Data.SqlClient.SqlDataAdapter(Sql2, MyConnection)
MyDataAdapter.Fill(MyDataSet, "tblDRAAS400")
Next
End With
I’ve had this problem in the past with VB 6 and managed to get around it by using the replace command and adding an extra single quote to surround all of my fields. I’m not sure how to take the code about and somehow achieve the same in .NET.
Any ideas?
Thanks
Andrew
I have a dataset (MyDataSet400) which I use as input to a SQL table called tblDRAAS400. I use the following code to loop through the dataset to populate the table. This works fine until one of the fields in the dataset contains an single quote, i.e. “O’BRIEN”. Here’s my code:
Dim dsCounter As Integer
With MyDataSet400.Tables(0)
For dsCounter = 0 To .Rows.Count - 1 'loop
Sql2 = "INSERT INTO tblDRAAS400 ([Account Code], [Credit
Limit], [Payment Terms], [Oldest Invoice]) VALUES ('" & MyDataSet400.Tables(0).Rows(dsCounter).Item(0) & "', '" & MyDataSet400.Tables(0).Rows(dsCounter).Item(1) & "', '" & MyDataSet400.Tables(0).Rows(dsCounter).Item(2) & "', '" & MyDataSet400.Tables(0).Rows(dsCounter).Item(3) & "')"
MyDataAdapter = New System.Data.SqlClient.SqlDataAdapter(Sql2, MyConnection)
MyDataAdapter.Fill(MyDataSet, "tblDRAAS400")
Next
End With
I’ve had this problem in the past with VB 6 and managed to get around it by using the replace command and adding an extra single quote to surround all of my fields. I’m not sure how to take the code about and somehow achieve the same in .NET.
Any ideas?
Thanks
Andrew