I'm having trouble converting this vb into c#.
VB.NET:
Dim arrVals() as object 'or as string
Dim i As Integer
sqlStr = "SELECT PubID, title, url, CategoryID FROM Pub WHERE "
For i = 0 To UBound(arrVals)
If i = 0 Then
sqlStr = sqlStr & "PubID = " & arrVals(i)
Else
sqlStr = sqlStr & " OR PubID = " & arrVals(i)
End IF
Next
sqlStr = sqlStr & " ORDER BY CategoryID, title "
Response.Write(sqlStr)
sqlStr Output:
SELECT PubID, title, url, CategoryID FROM Pub WHERE PubID = 20 OR PubID = 1 OR PubID = 53 OR PubID = 94 ORDER BY CategoryID, title
C#:
object[] arrVals;
int i = 0;
sqlStr = "SELECT PubID, title, url, price, CategoryID FROM Pub WHERE ";
for (i = 0; i <= arrVals.GetUpperBound(0); i++) //arrVals.Length gives the same error
{
if (i == 0)
sqlStr = sqlStr + "PubID = " + arrVals.ToString()
;
else
{
sqlStr = sqlStr + " OR PubID = " + arrVals.ToString()
;
}
}
sqlStr = sqlStr + " ORDER BY CategoryID, title ";
Response.Write(sqlStr);
sqlStr Output:
SELECT PubID, title, url, CategoryID FROM Pub WHERE PubID = S OR PubID = y OR PubID = s OR PubID = t OR PubID = e OR PubID = m OR PubID = . OR PubID = S OR PubID = t ORDER BY CategoryID, title
As you can see, althought it's not giving me an compiler error it inserts wrong values into the integer i (looks like it's trying to say System.String....). I think the problem is the UBound conversion I tried .GetUpperBound and .Length but I'm getting the same error. What am I doing wrong Any thoughts please!

ubound conversion from vb to c#
robbyc
OutPut for: int[] arrVals = new int[]{21,1,53,94};
SELECT PubID, title, url, CategoryID FROM Pub where PubId in (21,1,53,94) ORDER BY CategoryID, title;
OutPut for: int[] arrVals = new int[]{};
SELECT PubID, title, url, CategoryID FROM Pub ORDER BY CategoryID, title;
GabrieleN