user2474358
user2474358

Reputation: 7

Data fetched from table count-1

I'm having an problem that my c# project when fetching data from data table it always fetches data -1. For example if my table has 3 data when i print data in output console it prints only 2 data(First data row won't get fetched). What is the reason for this scenario.I attached the code part with this.

String connectionString = "server = localhost; user id = xxx; password = xxx; database = xxxr";
        MySqlConnection connection = new MySqlConnection(connectionString);
        connection.Open();
        String caseId = textBox1.Text;
        String searchString = "SELECT * FROM xxx" ;
        if (textBox1.Text != "")
        {
            using (MySqlCommand mySqlCommand = new MySqlCommand(searchString, connection))
            {

                using (MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader())
                {
                    if (mySqlDataReader.Read() && mySqlDataReader[0] != DBNull.Value)
                    {
                        while(mySqlDataReader.Read()){
                            Debug.WriteLine(mySqlDataReader.GetString(0));
                            Debug.WriteLine(mySqlDataReader.GetString(1));
                            Debug.WriteLine(mySqlDataReader.FieldCount.ToString());
                        }

                    }

                }
            }

                connection.Close();

Thanks.

Upvotes: 0

Views: 33

Answers (1)

krivtom
krivtom

Reputation: 24916

When you call

if (mySqlDataReader.Read() && mySqlDataReader[0] != DBNull.Value)

you fetch first row of the result. You do not use this result anywhere when outputting the data and this result is effectively lost.

Upvotes: 2

Related Questions