Link to home
Start Free TrialLog in
Avatar of 5281
5281

asked on

convert vb.net to C#

Who can help me convert this code from VB.Net to C#?

objCmd = New SqlCommand(strSQL, objConn)
strSQL = "select count (id) from tblUsers where username='" & strArray(1) & "'"
objCmd.CommandText = strSQL

Dim objReader As SqlClient.SqlDataReader
objReader = objCmd.ExecuteReader

objReader.Read()
If objReader(0) > 0 Then
     ......
    objReader.Close()
End If
Avatar of dstanley9
dstanley9

       objCmd = new SqlCommand(strSQL, objConn);
        strSQL = ("select count (id) from tblUsers where username=\'"
                    + (strArray(1) + "\'"));
        objCmd.CommandText = strSQL;
        SqlClient.SqlDataReader objReader;
        objReader = objCmd.ExecuteReader;
        objReader.Read();
        if ((objReader(0) > 0))
        {
            ...
            objReader.Close();
        }
Avatar of 5281

ASKER

dstanley9,

strArray(1)  -->  This line gives error, if I change to strArray[1], then it is fine.

SqlClient.SqlDataReader objReader;  --> This line gives error, if I change to SqlDataReader objReader, then it is fine.

if ((objReader(0) > 0))   --> this line gives error when compile,  'objReader' denotes a 'variable' where a 'method' was expected, I still don't know how to change this line.  

Thanks.

use

if (objReader[0] > 0)

To be fair, I just threw in the result of the converter listed above; I guess I should have checked it myslef first :(
Avatar of 5281

ASKER

when I compile, it give this error message for line if (objReader[0] > 0)
Operator '>' cannot be applied to operands of type 'object' and 'int'
How to solve that problem?
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 5281

ASKER

Thanks dstanley9.