System.InvalidOperationException : Please help
-
I am reading some data from the database, but its giving
System.InvalidOperationException:
my code is as follows :LoadInfo l = new LoadInfo();
l.LoadFromSQL(Session["username"].ToString());the class
LoadInfo
has the member functionLoadFromSQL
as follows,public void LoadFromSQL(string username)
{
SqlDataReader reader = null;
reader = SqlMember.ExecuteReader("SELECT UserId FROM Members WHERE UserName=@username", username);
m.LoadFromReader(reader);
}the
SqlMember
class has the static member functionExecuteReader
as following:static public SqlDataReader ExecuteReader(string qs,string username)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand(qs, conn);
cmd.Parameters.Add(new SqlParameter ("@username","a"));
conn.Open();
if (cmd.ExecuteScalar() == null)
{
int i;
i = 0;
}
return cmd.ExecuteReader();
}the
LoadFromReader
member function ofSqlMember
class is as follows:public void LoadFromReader(SqlDataReader reader)
{
this.UserId = (int)reader["UserId"];
this.FirstName = (string)reader["FirstName"];
this.LastName = (string)reader["LastName"];
this.CityId = (int)reader["CityId"];
this.CountryId = (int)reader["CountryId"];
this.Mood = (byte)reader["Mood"];
}The exception is occurring in the above function, while reading the data from the
reader
this.UserId = (int)reader["UserId"];
if this line is commented (//) the exception occurs in the next line. Error:Invalid attempt to read when no data is present.
:confused: The username provided by the session object is available in the database. Please help me to solve this problem. :( Is there any way to know, if the ExecuteReader returned any values ?
Apurv
-
I am reading some data from the database, but its giving
System.InvalidOperationException:
my code is as follows :LoadInfo l = new LoadInfo();
l.LoadFromSQL(Session["username"].ToString());the class
LoadInfo
has the member functionLoadFromSQL
as follows,public void LoadFromSQL(string username)
{
SqlDataReader reader = null;
reader = SqlMember.ExecuteReader("SELECT UserId FROM Members WHERE UserName=@username", username);
m.LoadFromReader(reader);
}the
SqlMember
class has the static member functionExecuteReader
as following:static public SqlDataReader ExecuteReader(string qs,string username)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand(qs, conn);
cmd.Parameters.Add(new SqlParameter ("@username","a"));
conn.Open();
if (cmd.ExecuteScalar() == null)
{
int i;
i = 0;
}
return cmd.ExecuteReader();
}the
LoadFromReader
member function ofSqlMember
class is as follows:public void LoadFromReader(SqlDataReader reader)
{
this.UserId = (int)reader["UserId"];
this.FirstName = (string)reader["FirstName"];
this.LastName = (string)reader["LastName"];
this.CityId = (int)reader["CityId"];
this.CountryId = (int)reader["CountryId"];
this.Mood = (byte)reader["Mood"];
}The exception is occurring in the above function, while reading the data from the
reader
this.UserId = (int)reader["UserId"];
if this line is commented (//) the exception occurs in the next line. Error:Invalid attempt to read when no data is present.
:confused: The username provided by the session object is available in the database. Please help me to solve this problem. :( Is there any way to know, if the ExecuteReader returned any values ?
Apurv
I think it is showing you the error because you forgot to call Read() also your query returns only UserID and you are reading whole row. so change the
Select UserID FROM Members WHERE UserName=@username
to
Select UserID,FirstName,LastName,CityId,CountryId,Mood FROM Members WHERE UserName=@username
in the LoadFromReader()
public void LoadFromReader(SqlDataReader reader)
{
if(reader.Read())
{
this.UserId = (int)reader["UserId"];
this.FirstName = (string)reader["FirstName"];
this.LastName = (string)reader["LastName"];
this.CityId = (int)reader["CityId"];
this.CountryId = (int)reader["CountryId"];
this.Mood = (byte)reader["Mood"];
}
else
// code for no row found for the specific user...
}also change the
ExecuteScalar()
toExecuteReader()
because now you are getting more than one values. -
I think it is showing you the error because you forgot to call Read() also your query returns only UserID and you are reading whole row. so change the
Select UserID FROM Members WHERE UserName=@username
to
Select UserID,FirstName,LastName,CityId,CountryId,Mood FROM Members WHERE UserName=@username
in the LoadFromReader()
public void LoadFromReader(SqlDataReader reader)
{
if(reader.Read())
{
this.UserId = (int)reader["UserId"];
this.FirstName = (string)reader["FirstName"];
this.LastName = (string)reader["LastName"];
this.CityId = (int)reader["CityId"];
this.CountryId = (int)reader["CountryId"];
this.Mood = (byte)reader["Mood"];
}
else
// code for no row found for the specific user...
}also change the
ExecuteScalar()
toExecuteReader()
because now you are getting more than one values. -
I think it is showing you the error because you forgot to call Read() also your query returns only UserID and you are reading whole row. so change the
Select UserID FROM Members WHERE UserName=@username
to
Select UserID,FirstName,LastName,CityId,CountryId,Mood FROM Members WHERE UserName=@username
in the LoadFromReader()
public void LoadFromReader(SqlDataReader reader)
{
if(reader.Read())
{
this.UserId = (int)reader["UserId"];
this.FirstName = (string)reader["FirstName"];
this.LastName = (string)reader["LastName"];
this.CityId = (int)reader["CityId"];
this.CountryId = (int)reader["CountryId"];
this.Mood = (byte)reader["Mood"];
}
else
// code for no row found for the specific user...
}also change the
ExecuteScalar()
toExecuteReader()
because now you are getting more than one values.