Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6059011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:39:48+00:00 2026-05-23T08:39:48+00:00

SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString); SqlCommand cmd = new SqlCommand(sb.ToString(), conn); cmd.Parameters.Add(@ThreadsID, SqlDbType.Int).Value =

  • 0
      SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString);

    SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
    cmd.Parameters.Add("@ThreadsID", SqlDbType.Int).Value = commentIDe;
    cmd.Parameters.Add("@CommentsID", SqlDbType.Int).Value = commentIDe;
    try
    {
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (dr != null && dr["Comments"] != null && dr["Name"] != null && dr["Date"] != null && dr["UserID"]!=null)//> Invalid attempt to read when no data is present.
        {
            Comment = dr["Comments"].ToString();
            UserName = dr["Name"].ToString();
            Reputation=Int32.Parse(dr["Reputation"].ToString());
            Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString());
            UserID = (Guid)dr["UserID"];
        }
        dr.Close();
    }
    finally
    {
        if (conn.State != ConnectionState.Closed)
        {
            conn.Close();
        }
    }

Note: I do iterate over that piece of code with while(dr.Read){}… it isnt shown here.

Why do i get that exception and how do i get rid of it

UPDATE:

                 while (reader.Read())//Command runs through all the ThreadIDs that i have!
                {
                    Comments allQ = new Comments((int)reader["CommentsID"]);
                    allComments.Add(allQ);
                }

Comments is the class that the code is in, and it has a method inside the constructor that runs the code that i presented.

It could be that if the loop runs too many times.. Then the exception is being thrown am i right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:39:48+00:00Added an answer on May 23, 2026 at 8:39 am

    The following part of your code is illegal

    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    
    if (dr != null && dr["Comments"] ...)
    

    You have to call the Read method once (and verify that it returns true) before accessing the indexer of the SqlDataReader, as the documentation states:

    The default position of the SqlDataReader is before the first record. Therefore, you must call Read to begin accessing any data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionstring].ConnectionString)) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) {
Assume the following code: using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand
Given this code: using (var conn = new SqlConnection(...)) { conn.Open(); using (var cmd
i have this code using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); DataTable Databases
i have the following code string connString = ConfigurationManager.ConnectionStrings[XXXConnectionString].ConnectionString; SqlConnection conn = new SqlConnection(connString);
In VB.NET I can use: Protected Conn As New SqlConnection(ConfigurationManager.ConnectionStrings(Active).ConnectionString) However, when I do
private void FillInvoiceList() { DataTable distinctInvoice = new DataTable(); using (SqlConnection conn = new
protected void Button2_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(Data Source=KING;Initial Catalog=semasinter;Integrated
SqlConnection conn = new SqlConnection(Data Source=DDPRO8-WIN7X86\\SQLEXPRESS;Initial Catalog=mp3bytes;Persist Security Info=True;Integrated security=true; User ID=; Password=;); SqlCommand
using (SqlConnection conn = new SqlConnection(Data Source=SARAN-PC\SQLEXPRESS;Initial Catalog=ERP;integrated security=SSPI)) {} when i include this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.