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. DataReader throws exception

DataReader throws exception

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netdebugging
6 Posts 4 Posters 0 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.
  • J Offline
    J Offline
    jamie21cor
    wrote on last edited by
    #1

    Hi, I was wondering if anyone could help me with a DataTable problem. I am working in ASP.NET C#, The code that I have written is listed below. The moSqlDataReader can be viewed when in debug mode and the record that is asked for is seen, but when the reader is called into the DataTable using the .Load() function the DataReader shows a thrown exception of System.InvalidOperationException in the Depth and FieldCount properties when viewed in Debug mode Code: msUserName = Request.QueryString.Get("username"); LabelUserName.Text = msUserName; UserName.Value = msUserName; moMembershipUser = Membership.GetUser(msUserName); msEmailAddress = moMembershipUser.Email; moSqlConnection = new SqlConnection(); moSqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppSqlServer"].ConnectionString; moSqlCommand = new SqlCommand(); moSqlCommand.CommandText = "SELECT * From tblUserInfo WHERE UserID = @UserID "; moSqlCommand.CommandType = CommandType.Text; moSqlCommand.Connection = moSqlConnection; // UserID Parameter moSqlParameterUser = new SqlParameter(); moSqlParameterUser.ParameterName = "@UserID"; moSqlParameterUser.SqlDbType = SqlDbType.UniqueIdentifier; moSqlParameterUser.Direction = ParameterDirection.Input; moSqlParameterUser.Value = new Guid(moMembershipUser.ProviderUserKey.ToString()); moSqlCommand.Parameters.Add(moSqlParameterUser); moSqlCommand.Connection.Open(); moSqlDataReader = moSqlCommand.ExecuteReader(CommandBehavior.CloseConnection); moDataTable = new DataTable(); moDataTable.Load(moSqlDataReader); if (moDataTable.Rows.Count == 0){ msFirstName = ""; msLastName = ""; }else{ moDataTable.PrimaryKey = new DataColumn[] { moDataTable.Columns["UserID"] }; msFirstName = moDataTable.Rows.Find("FirstName"); msLastName = moDataTable.Rows.Find("LastName"); } FirstName.Text = msFirstName; LastName.Text = msLastName; EmailAddress.Text = msEmailAddress; moDataTable.Dispose(); moSqlCommand.Dispose(); moSqlConnection.Dispose(); I dont understand why the application is returning this, I have checked the internet and several books but have found no reason for this error. I would appreciate if anyone could help fix this. Thanks

    modified on Monday, April 14, 2008 9:44 AM

    H S 2 Replies Last reply
    0
    • J jamie21cor

      Hi, I was wondering if anyone could help me with a DataTable problem. I am working in ASP.NET C#, The code that I have written is listed below. The moSqlDataReader can be viewed when in debug mode and the record that is asked for is seen, but when the reader is called into the DataTable using the .Load() function the DataReader shows a thrown exception of System.InvalidOperationException in the Depth and FieldCount properties when viewed in Debug mode Code: msUserName = Request.QueryString.Get("username"); LabelUserName.Text = msUserName; UserName.Value = msUserName; moMembershipUser = Membership.GetUser(msUserName); msEmailAddress = moMembershipUser.Email; moSqlConnection = new SqlConnection(); moSqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppSqlServer"].ConnectionString; moSqlCommand = new SqlCommand(); moSqlCommand.CommandText = "SELECT * From tblUserInfo WHERE UserID = @UserID "; moSqlCommand.CommandType = CommandType.Text; moSqlCommand.Connection = moSqlConnection; // UserID Parameter moSqlParameterUser = new SqlParameter(); moSqlParameterUser.ParameterName = "@UserID"; moSqlParameterUser.SqlDbType = SqlDbType.UniqueIdentifier; moSqlParameterUser.Direction = ParameterDirection.Input; moSqlParameterUser.Value = new Guid(moMembershipUser.ProviderUserKey.ToString()); moSqlCommand.Parameters.Add(moSqlParameterUser); moSqlCommand.Connection.Open(); moSqlDataReader = moSqlCommand.ExecuteReader(CommandBehavior.CloseConnection); moDataTable = new DataTable(); moDataTable.Load(moSqlDataReader); if (moDataTable.Rows.Count == 0){ msFirstName = ""; msLastName = ""; }else{ moDataTable.PrimaryKey = new DataColumn[] { moDataTable.Columns["UserID"] }; msFirstName = moDataTable.Rows.Find("FirstName"); msLastName = moDataTable.Rows.Find("LastName"); } FirstName.Text = msFirstName; LastName.Text = msLastName; EmailAddress.Text = msEmailAddress; moDataTable.Dispose(); moSqlCommand.Dispose(); moSqlConnection.Dispose(); I dont understand why the application is returning this, I have checked the internet and several books but have found no reason for this error. I would appreciate if anyone could help fix this. Thanks

      modified on Monday, April 14, 2008 9:44 AM

      H Offline
      H Offline
      Herman T Instance
      wrote on last edited by
      #2

      Hi, did you run a DEBUG over this part of code? Can you place this code in a try...catch? it will tell you where something goes wrong.

      J 1 Reply Last reply
      0
      • H Herman T Instance

        Hi, did you run a DEBUG over this part of code? Can you place this code in a try...catch? it will tell you where something goes wrong.

        J Offline
        J Offline
        jamie21cor
        wrote on last edited by
        #3

        Hi, Thanks for replying. Yes I have Debugged the code in order to find out what exactly is wrong but everything seems fine up until the DataTable uses the Load function again it displays the exception of System.InvalidOperationException in the Depth and FieldCount properties when in Debug mode. I have not looked at trying the Try Catch methods yet. I will try these and see what l can find. Thanks

        H E 2 Replies Last reply
        0
        • J jamie21cor

          Hi, Thanks for replying. Yes I have Debugged the code in order to find out what exactly is wrong but everything seems fine up until the DataTable uses the Load function again it displays the exception of System.InvalidOperationException in the Depth and FieldCount properties when in Debug mode. I have not looked at trying the Try Catch methods yet. I will try these and see what l can find. Thanks

          H Offline
          H Offline
          Herman T Instance
          wrote on last edited by
          #4

          Got the Error: You us e a DataReader object instead of a DataTable object. The reader will give up data per row until there is no more to give. After loading a datareader you ask 2 things if (moSqlDataReader.HasRows) { WHILE (moSqlDataReader.Read()) { // your code here } } in your case it is better to do a DataSet dSet = new DataSet(); SqlDataAdapter adapter = new SqlDataAdapter("Query", con); adapter.SelectCommand.CommandType = CommandType.Text; etc .. adapter.Fill(dSet);

          1 Reply Last reply
          0
          • J jamie21cor

            Hi, Thanks for replying. Yes I have Debugged the code in order to find out what exactly is wrong but everything seems fine up until the DataTable uses the Load function again it displays the exception of System.InvalidOperationException in the Depth and FieldCount properties when in Debug mode. I have not looked at trying the Try Catch methods yet. I will try these and see what l can find. Thanks

            E Offline
            E Offline
            eyeseetee
            wrote on last edited by
            #5

            Double check your query is correct and maybe try another simple query and load that this will eliminate the possibility that it is your query which is the problem or it will highlight what the problem is

            1 Reply Last reply
            0
            • J jamie21cor

              Hi, I was wondering if anyone could help me with a DataTable problem. I am working in ASP.NET C#, The code that I have written is listed below. The moSqlDataReader can be viewed when in debug mode and the record that is asked for is seen, but when the reader is called into the DataTable using the .Load() function the DataReader shows a thrown exception of System.InvalidOperationException in the Depth and FieldCount properties when viewed in Debug mode Code: msUserName = Request.QueryString.Get("username"); LabelUserName.Text = msUserName; UserName.Value = msUserName; moMembershipUser = Membership.GetUser(msUserName); msEmailAddress = moMembershipUser.Email; moSqlConnection = new SqlConnection(); moSqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppSqlServer"].ConnectionString; moSqlCommand = new SqlCommand(); moSqlCommand.CommandText = "SELECT * From tblUserInfo WHERE UserID = @UserID "; moSqlCommand.CommandType = CommandType.Text; moSqlCommand.Connection = moSqlConnection; // UserID Parameter moSqlParameterUser = new SqlParameter(); moSqlParameterUser.ParameterName = "@UserID"; moSqlParameterUser.SqlDbType = SqlDbType.UniqueIdentifier; moSqlParameterUser.Direction = ParameterDirection.Input; moSqlParameterUser.Value = new Guid(moMembershipUser.ProviderUserKey.ToString()); moSqlCommand.Parameters.Add(moSqlParameterUser); moSqlCommand.Connection.Open(); moSqlDataReader = moSqlCommand.ExecuteReader(CommandBehavior.CloseConnection); moDataTable = new DataTable(); moDataTable.Load(moSqlDataReader); if (moDataTable.Rows.Count == 0){ msFirstName = ""; msLastName = ""; }else{ moDataTable.PrimaryKey = new DataColumn[] { moDataTable.Columns["UserID"] }; msFirstName = moDataTable.Rows.Find("FirstName"); msLastName = moDataTable.Rows.Find("LastName"); } FirstName.Text = msFirstName; LastName.Text = msLastName; EmailAddress.Text = msEmailAddress; moDataTable.Dispose(); moSqlCommand.Dispose(); moSqlConnection.Dispose(); I dont understand why the application is returning this, I have checked the internet and several books but have found no reason for this error. I would appreciate if anyone could help fix this. Thanks

              modified on Monday, April 14, 2008 9:44 AM

              S Offline
              S Offline
              Soumini Ramakrishnan
              wrote on last edited by
              #6

              Have you declared moSqlDataReader ?

              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