dbReader creating error

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a function that is getting an error on my dbReader statment:

C:\VSProjects\ClassLibrary4\NewHire.cs(53): 'dbReader' denotes a 'variable'
where a 'method' was expected

Here is the function:
************************************************************************
private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject();
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
returnString = dbReader("FirstName") + "/n" + dbReader("LastName");
<-- error on first dbreader
};
dbReader.Close();
}
************************************************************************

What is wrong with this statement?

Thanks,

Tom
 
tshad said:
I have a function that is getting an error on my dbReader statment:

C:\VSProjects\ClassLibrary4\NewHire.cs(53): 'dbReader' denotes a
'variable' where a 'method' was expected

Here is the function:
************************************************************************
private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject();
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
returnString = dbReader("FirstName") + "/n" + dbReader("LastName"); <--
error on first dbreader
};
dbReader.Close();
}

The correct syntax is:

returnString = dbReader["FirstName"] ...
 
Wrong brackets : this should (at the least) use the indexer notation and
casting:

string returnString = ((string)dbReader["FirstName"]) + "/n" +
((string)dbReader["LastName"]);

You could lost some of the casting if you used string.Format:

string returnString = string.Format("{0}\n{1}",
dbReader["FirstName"],dbReader["LastName"]);

The ideal would be to use .GetString(), but this only takes an ordinal - not
worth doing if you only intend reading one line, but if you were reading
2000 it makes sense to use ordinal-based access, finding each ordinal once
(at the top).

Marc
 
Tom Porterfield said:
tshad said:
I have a function that is getting an error on my dbReader statment:

C:\VSProjects\ClassLibrary4\NewHire.cs(53): 'dbReader' denotes a
'variable' where a 'method' was expected

Here is the function:
************************************************************************
private static void GetNewHire(ref string returnString)
{
DbObject myDbObject = new DbObject();
SqlDataReader dbReader;

SqlParameter[] parameters = {
new SqlParameter("@ApplicantID",SqlDbType.Int)};

parameters[0].Value = 241;

dbReader = myDbObject.RunProcedure("GetNewHire", parameters);
if (dbReader.Read())
{
returnString = dbReader("FirstName") + "/n" + dbReader("LastName");
<-- error on first dbreader
};
dbReader.Close();
}

The correct syntax is:

returnString = dbReader["FirstName"] ...

That fixed it.

Thanks,

Tom
 

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

object error 6
Testing for null 3
dbReader closing prematurely 2
Reusing parameters 2
DataReader error 1
Reusing parameters 2
Reset SqlParameters 2
Not returning boolean correctly 8

Back
Top