ubound conversion from vb to c#

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()Idea;
else
{
sqlStr = sqlStr + " OR PubID = " + arrVals.ToString()Idea;
}
}
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!



Answer this question

ubound conversion from vb to c#

  • robbyc

    StringBuilder sb = new StringBuilder("SELECT PubID, title, url, CategoryID FROM Pub");
    if (arrVals.Length > 0)
    {
        sb.Append(" Where PubId in (");
        for( i = 0; arrVals.Length - 1; i++)
            sb.Append(arrVals[ i ].ToString() + (i < arrVals.Length - 1 : ",",""));
        sb.Append(")");
    }
    sb.Append(" ORDER BY CategoryID, title");
    Console.WriteLine(sb.ToString());


    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

    Never mind. I figured it out... I had to remove the .ToString().
  • ubound conversion from vb to c#