Reputation: 561
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;
}
Upvotes: 1
Views: 15798
Reputation: 111
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.
Upvotes: -1
Reputation: 13484
Chang your
readAssess = new SqlDataReader ();
readAssess = cmdReadAssess.ExecuteReader();
to
SqlDataReader readAssess= cmdReadAssess.ExecuteReader();
Upvotes: 1
Reputation: 28403
Remove New from SqlDataReader Declaration.
Try this:
Change this
SqlDataReader readAssess;
readAssess = new SqlDataReader();
to
SqlDataReader readAssess;
Upvotes: 3
Reputation: 1907
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
Upvotes: 1
Reputation: 14432
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;
}
Upvotes: 2