0

I created database in ms access and it has GPA column. In listbox2 i need to list students which GPA is bigger than 2. how caN I do that?

command.CommandText = "SELECT * FROM Student WHERE GPA > 2";
while (reader.Read())
{
  listBox2.Items.Add............?
}

Help if you can.

2
  • 1
    What is wrong with the code that you have? Other than you never execute the command to get the reader.... Commented Dec 14, 2012 at 12:37
  • What keeps you from searching SO or Google to find one of the replies to simliar How do I display data from Access/Excel/SQL in a list view/list box/console/whatever questions? Commented Dec 14, 2012 at 12:54

2 Answers 2

2

I don't know if that's just pseudo code you've posted. But if not, you are reading a DataReader which is either not initialized or is not using the correct Command-sql since you are setting the CommandText one line before.

command.CommandText = "SELECT * FROM Student WHERE GPA > 2";
using(var reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        // assuming that there's a column with name: StudentName
        listBox2.Items.Add(reader.GetString(reader.GetOrdinal("StudentName")));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
string ConnectionString = "PUT YOU CONNECTION STRING HERE";

con = new SqlConnection(ConnectionString);
con.Open();
string CommandText = "SELECT * FROM Student WHERE GPA > 2";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
rdr = cmd.ExecuteReader();
lbx.Items.Clear();
while (rdr.Read())
{    
     lbx.Items.Add......
}

1 Comment

put this in a try catch and close the connection in finally block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.