Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a script that should select from table all rows, order by id but only one row per select. This selection is made when a button is pressed. But query is not working, it selects only first row. Why?
C#
private void select()
        {
            using (Conexiune.getConnection())
            {
                string select = "SELECT DISTINCT * FROM questions ORDER BY id ASC LIMIT 1";
                SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection());
                cmd.CommandType = CommandType.Text;
                SQLiteDataReader rdra = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                try
                {
                        while (rdra.Read())
                        {
                            textBox1.Text = rdra["question"].ToString();
                            textBox2.Text = rdra["answer1"].ToString();
                            textBox3.Text = rdra["answer2"].ToString();
                            textBox4.Text = rdra["answer3"].ToString();
                            r1 = (int)rdra["option1"];
                            r2 = (int)rdra["option2"];
                            r3 = (int)rdra["option3"];
                            SimulatorManager.Intrebare = textBox1.Text;
                        }
                }
                catch (InvalidOperationException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Posted
Comments
Thomas.D Williams 30-Mar-15 17:29pm    
What happens when you remove the LIMIT 1 from the query?
DPaul1994 31-Mar-15 1:17am    
If I remove LIMIT 1, nothing changes
Sascha Lefèvre 30-Mar-15 17:29pm    
Do you want each subsequent button-click to select and display the next row?
DPaul1994 31-Mar-15 1:16am    
Exactly!
Sascha Lefèvre 31-Mar-15 1:39am    
Please take a look at my solution (3)

 
Share this answer
 
Comments
Sascha Lefèvre 30-Mar-15 18:39pm    
Take a look at his code - it wouldn't make sense to not use LIMIT 1
Well you want to return all the rows of the table questions but why you're adding the clause LIMIT 1 ??

The query is correct !,but as we see the code you want to return all the rows and you're fetching them (rdra.Read() ) but the query will only return ONE ROW because you're using 'LIMIT 1'.


sorry my english is little bit ... lol
 
Share this answer
 
As clarified per comments, the goal is to have each subsequent button-click select and display the next record.

So basically what you want is so called "Paging" but just with 1 record. With your LIMIT-clause you have one neccessary part for paging but you need another part in order to not select the first record each time: You need to read the ID of the record, store it in a variable outside of your select-method so that its value is being retained (until you potentially reset it with another button) and include it into your query with a WHERE-clause:

C#
int lastID = -1; // <- new (assuming id is an int)
private void select()
{
    using (Conexiune.getConnection())
    {
        string select = String.Format("SELECT DISTINCT * FROM questions WHERE id > {0} ORDER BY id ASC LIMIT 1", lastID); // <- new
        SQLiteCommand cmd = new SQLiteCommand(select, Conexiune.getConnection());
        cmd.CommandType = CommandType.Text;
        SQLiteDataReader rdra = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        try
        {
            while (rdra.Read())
            {
                textBox1.Text = rdra["question"].ToString();
                textBox2.Text = rdra["answer1"].ToString();
                textBox3.Text = rdra["answer2"].ToString();
                textBox4.Text = rdra["answer3"].ToString();
                r1 = (int)rdra["option1"];
                r2 = (int)rdra["option2"];
                r3 = (int)rdra["option3"];
                lastID = (int)rdra["id"]; // <- new (assuming id is an int)
                SimulatorManager.Intrebare = textBox1.Text;
            }
        }
        catch (InvalidOperationException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}


Please note: The way I built the query-string is a bit quick&dirty and I don't recommend doing it like this (instead it should be done with an SQL-Parameter). Please take a look at an answer to another question where I show an exemplary good-practice database-access: how to loop sql server table to create a datagridview - sql table field matches csv field[^] (It's targeted at SQL-Server, for SQLite you would have to change how an SQL-Parameter is represented in a query-string but ad-hoc I don't know that myself, please google it if you don't know either.)
 
Share this answer
 
Comments
DPaul1994 31-Mar-15 2:08am    
I receive "Specified cast is not valid" and I don't understand why. `id` is an integer in table and `rows` is an int initialized with 0
Sascha Lefèvre 31-Mar-15 2:14am    
Hm.. try this:
lastID = Convert.ToInt32(rdra["id"]);
(instead of lastID = (int)rdra["id"]; )

I don't know what "rows" is supposed to be? It's not visible in the code you posted.
DPaul1994 31-Mar-15 2:18am    
Is just a var that I will have to use it later in program. Still got this problem...interesting
Sascha Lefèvre 31-Mar-15 2:20am    
At which line does the error occur?
DPaul1994 31-Mar-15 2:27am    
Aaa sorry, I have a previous error. I don't receive any error messages, is just, it selects only first row

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900