1

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;
}
1
  • Unrelated tips: SqlConnection, SqlCommand and SqlDataReader are all IDisposable so each should be in a using block. Also, beware constructing queries using string concatenation because it makes your code vulnerable to SQL injection attacks: use SQL parameters. Commented Jul 30, 2018 at 8:38

5 Answers 5

3

Remove New from SqlDataReader Declaration.

Try this:

Change this

SqlDataReader readAssess;
    readAssess = new SqlDataReader();

to

SqlDataReader readAssess;

SqlDataReader

Sign up to request clarification or add additional context in comments.

Comments

2

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;
}

Comments

1

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

Comments

1

Chang your

 readAssess = new SqlDataReader ();

 readAssess = cmdReadAssess.ExecuteReader();

to

 SqlDataReader readAssess= cmdReadAssess.ExecuteReader();

SqlDataReader Class

2 Comments

This won't compile because readAssess is already defined with the proper type. The exception doesn't even occur at this line
@PanagiotisKanavos thankyou i have updated .Please have a look
-1
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.