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

  • SEARCH
  • Home
  • 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 9145385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:30:01+00:00 2026-06-17T10:30:01+00:00

I have a SQL Server database and I’m trying to query it. First I

  • 0

I have a SQL Server database and I’m trying to query it. First I retrieve all names and id’s from a table called Firms, use that to populate a ListBox. When I click a an item in that list box I want to populate another ListBox with employees that work for said firm.

I use this query (tested in SQL Server 2008)

SELECT
    employee.Name
From
    employee, Firm
WHERE
    Firm.Name = 'Some Firm' AND
    Firm.F_Id = employee.F_Id

This is the code for clicking on a list box item:

private void Firm_ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
        String Name = "";
        Int32 F_Id = -1;

        // This for would be useful later on in the program
        // For now it can be ignored
        for (int i = 0; i < Firms.Count(); i++)
        {
            if (Firms.ElementAt(i).GetFirmName().Equals(Firm_ListBox.SelectedItem.ToString()))
            {
                Name = Firms.ElementAt(i).GetFirmName();
                F_Id = Firms.ElementAt(i).GetF_Id();
            }
        }

        MessageBox.Show(Name.ToString() + "\n\t" + F_Id.ToString()); // first message box

        SqlCommand SqlCommand = new SqlCommand(
            "SELECT employee.Name From employee, Firm WHERE Firm.Name = " + "'" + 
             Name + "'" + " AND Firm.F_Id = employee.F_Id", SqlConnection);
        SqlDataReader SqlDataReader = SqlCommand.ExecuteReader();

        MessageBox.Show("Test: " + SqlDataReader.GetString(1)); // second message box

        Employee_ListBox.Items.Clear();
        while (SqlDataReader.Read())
        {
            Employee_ListBox.Items.Add(SqlDataReader.GetString(1));
            MessageBox.Show(SqlDataReader.GetString(1));
        }

        SqlDataReader.Close();
    }

First MessageBox works, but the second one does not appear. I have no idea what is going on here (not to mention my ListBox doesn’t get populated).

EDIT While debugging I realized that after the read operation, when I attempt to get string I get a horrible exception (I added a try-catch block). I can’t even begin to read it, except this

Invalid attempt to read when no data is present.

caught my eye. Yet I can’t explain. The connection works, the query I’ve tested. And even if it didn’t return anything … shouldn’t it just print nothing and move one ?

EDIT2 It seems I get an invalid operation exception, not at reader, but at the message box.

  • 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-06-17T10:30:03+00:00Added an answer on June 17, 2026 at 10:30 am

    In the second message box, you’ve got this as part of the message:

    SqlDataReader.GetString(1)
    

    Because you haven’t yet read anything from the data reader, this command will throw the exception you are seeing.

    The command

    SqlDataReader.Read()
    

    needs to appear prior to trying the GetString method. Additionally, you should verify that data was read before executing the GetString method.

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

Sidebar

Related Questions

I have SQL Server 2008 database with two tables. The first table is called
I have a SQL Server database table with a column called resources. Each cell
I i have a SQL Server database table called tbl_faults . In that table
I have a SQL Server database that holds a table where a varchar column
We have a SQL Server database table that consists of user id, some numeric
I have a sql query that I run against a sql server database eg.
I have a SQL Server database and I want to define a query which
I have a SQL server database design problem. I have an existing database table
I have SQL Server 2008 database with 2 tables: Table A has columns ID
I have an SQL SERVER database with table, functions, stored procedures. I want to

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.