Question

I have issues with SqlDataReader. I get the error "The type 'System.Data.SqlClient.SqlDataReader' has no constructors defined" when I try running the page. My intention here is to return a string value 0 if the user has not had access or 1 when the user has assess. Below is my code snippet.

public string CheckAssess(string emailAddress, string columnName)
{
    string chkAssess;
    SqlDataReader readAssess;
    readAssess = new SqlDataReader();

    string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
    MgrAssessQry += " WHERE email ='" + emailAddress + "'";

    SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
    cn.Open();
    readAssess = cmdReadAssess.ExecuteReader();

    while(readAssess.Read())
    {
        // Add the rows
       chkAssess = readAssess["IsAssessMgr"].ToString();
    }

    return chkAssess;
}
Was it helpful?

Solution 3

Try something like:

public string CheckAssess(string emailAddress, string columnName)
{
    string chkAssess;

    string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
    MgrAssessQry += " WHERE email ='" + emailAddress + "'";

    SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
    cn.Open();
    SqlDataReader readAssess = cmdReadAssess.ExecuteReader();

    while(readAssess.Read())
    {
        // Add the rows
       chkAssess = readAssess["IsAssessMgr"].ToString();
    }

    return chkAssess;
}

I have changed your SqlDataReader instantiation to the line where you execute the query Same way like in this example from MSDN

http://msdn.microsoft.com/es-es/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx

OTHER TIPS

Remove New from SqlDataReader Declaration.

Try this:

Change this

SqlDataReader readAssess;
    readAssess = new SqlDataReader();

to

SqlDataReader readAssess;

SqlDataReader

The SqlDataReader class has no constructors, rewrite your code to following:

public string CheckAssess(string emailAddress, string columnName)
{
    string chkAssess;
    SqlDataReader readAssess;
    string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers WHERE email ='" + emailAddress + "'";
    SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);

    cn.Open();
    readAssess = cmdReadAssess.ExecuteReader();

    while(readAssess.Read())
    {
        chkAssess = readAssess["IsAssessMgr"].ToString();
    }

    return chkAssess;
}

Chang your

 readAssess = new SqlDataReader ();

 readAssess = cmdReadAssess.ExecuteReader();

to

 SqlDataReader readAssess= cmdReadAssess.ExecuteReader();

SqlDataReader Class

I had the same problem. I resolved by a simple way like this.

SqlDataReader read = null;
and to check 'read' has row or not
if(read.HasRows)
{
   while(read.Read()){}
}


So please see this your code.

public string CheckAssess(string emailAddress, string columnName)
{
    string chkAssess;
    SqlDataReader readAssess;
    readAssess = null; // this from my comments

    string MgrAssessQry = "SELECT '"+columnName+"' FROM tblAllUsers";
    MgrAssessQry += " WHERE email ='" + emailAddress + "'";

    SqlCommand cmdReadAssess = new SqlCommand(MgrAssessQry, cn);
    cn.Open();
    readAssess = cmdReadAssess.ExecuteReader();

    if(readAssess.HasRows && readAssess != null) // this from my comments
    {
       while(readAssess.Read())
       {
          // Add the rows
          chkAssess = readAssess["IsAssessMgr"].ToString();
       }
     }

     return chkAssess;
 }

 // I hope this code would help you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top