String concatenating for Dataset.compute method

G

Guest

I am trying to use the compute method on a dataset that uses a concatenated string as the filter. Here is my code

Dim myTable As DataTabl
myTable = CheckHrsVacDataSet1.Tables("PYCheckHistory"
Dim str1 As String = "((Employee = '
Dim str2 As String = "') AND (CheckDate >= '
Dim str3 As String = "'))
VacHrsThisYr.Text = myTable.Compute("Sum(TotalVacationHrs)", (String.Concat(str1, EmployeeID, str2, fiscalYrDate, str3))).ToStrin

The code works fine but I get an error when my str1 picks up employees with an apostrophe in their name (ie: O'neal). The compute method thinks that the O' is the end of the string. Does anyone know how I can code around this

Thank
 
J

Jay B. Harlow [MVP - Outlook]

JC,
You need to "quote" the quoted string.

The easiest way is to use String.Replace, something like:

VacHrsThisYr.Text = myTable.Compute("Sum(TotalVacationHrs)",
String.Concat(str1, EmployeeID.Replace("'", "''"), str2, fiscalYrDate,
str3))

Note String.Concat & String.Replace return strings, there is no need to call
ToString on the return value.

Hope this helps
Jay

JC said:
I am trying to use the compute method on a dataset that uses a
concatenated string as the filter. Here is my code:
Dim myTable As DataTable
myTable = CheckHrsVacDataSet1.Tables("PYCheckHistory")
Dim str1 As String = "((Employee = '"
Dim str2 As String = "') AND (CheckDate >= '"
Dim str3 As String = "'))"
VacHrsThisYr.Text = myTable.Compute("Sum(TotalVacationHrs)",
(String.Concat(str1, EmployeeID, str2, fiscalYrDate, str3))).ToString
The code works fine but I get an error when my str1 picks up employees
with an apostrophe in their name (ie: O'neal). The compute method thinks
that the O' is the end of the string. Does anyone know how I can code around
this?
 
M

mwazir

Have you tried replacing one apostrophe with two?

EmployeeID = EmployeeID.Replace(Chr(39), Chr(39) & Chr(39))
VacHrsThisYr.Text = myTable.Compute("Sum(TotalVacationHrs)",
(String.Concat(str1, EmployeeID, str2, fiscalYrDate, str3))).ToString

HTH

--
Wazir

JC said:
I am trying to use the compute method on a dataset that uses a
concatenated string as the filter. Here is my code:
Dim myTable As DataTable
myTable = CheckHrsVacDataSet1.Tables("PYCheckHistory")
Dim str1 As String = "((Employee = '"
Dim str2 As String = "') AND (CheckDate >= '"
Dim str3 As String = "'))"
VacHrsThisYr.Text = myTable.Compute("Sum(TotalVacationHrs)",
(String.Concat(str1, EmployeeID, str2, fiscalYrDate, str3))).ToString
The code works fine but I get an error when my str1 picks up employees
with an apostrophe in their name (ie: O'neal). The compute method thinks
that the O' is the end of the string. Does anyone know how I can code around
this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top