Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. System.InvalidOperationException : Please help

System.InvalidOperationException : Please help

Scheduled Pinned Locked Moved ASP.NET
helpdatabasequestion
4 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AprNgp
    wrote on last edited by
    #1

    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 function LoadFromSQL 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 function ExecuteReader 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 of SqlMember 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

    R 1 Reply Last reply
    0
    • A AprNgp

      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 function LoadFromSQL 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 function ExecuteReader 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 of SqlMember 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

      R Offline
      R Offline
      Rutvik Dave
      wrote on last edited by
      #2

      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() to ExecuteReader() because now you are getting more than one values.

      A 2 Replies Last reply
      0
      • R Rutvik Dave

        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() to ExecuteReader() because now you are getting more than one values.

        A Offline
        A Offline
        AprNgp
        wrote on last edited by
        #3

        thanx .... will try with the changes ...

        Apurv

        1 Reply Last reply
        0
        • R Rutvik Dave

          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() to ExecuteReader() because now you are getting more than one values.

          A Offline
          A Offline
          AprNgp
          wrote on last edited by
          #4

          Its working thanks .....

          Apurv

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups