0
private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
            string Query = "SELECT Barcodes, Name, EDate, Quantity, Price FROM Products where Name='" + cbxProducts.Text + "' ; ";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
            SqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();


                    string sBarcode = myReader["Barcodes"].ToString();
                    string sName = myReader["Name"].ToString();
                    var sDate = myReader["EDate"];
                    string sQuantity = myReader["Quantity"].ToString();
                    string sPrice = myReader["Price"].ToString();
                    tbxBar.Text = sBarcode;
                    tbxName.Text = sName;
                    sDate = dateDate.Value;
                    tbxPrice.Text = sPrice;
                    tbxQua.Text = sQuantity;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

When i try to use this code i just get the error message "An invalid attempt was made to read when no data was available" I do have data inside of my databse but it still doesn't work.

  • duplicate question....https://stackoverflow.com/questions/1147615/invalid-attempt-to-read-when-no-data-is-present – Abhay Sep 22 '17 at 08:43
  • Exactly which line throws the error? What is the value of `myReader` when you debug? Isn't `myReader` supposed to be a list of values? – praty Sep 22 '17 at 08:43
  • Possible duplicate of [Invalid attempt to read when no data is present](https://stackoverflow.com/questions/1147615/invalid-attempt-to-read-when-no-data-is-present) – MatSnow Sep 22 '17 at 09:27

1 Answers1

2

Reader object fetches rows one by one and we need to tell it to bring the next row by using the Read() method.

You need to call Read() method of SqlDataReader object to read each row, if there is single row expected then you can do via an if otehrwise you would have to do in a while loop like:

while(myReader.Read())
{

                string sBarcode = myReader["Barcodes"].ToString();
                string sName = myReader["Name"].ToString();
                var sDate = myReader["EDate"];
                string sQuantity = myReader["Quantity"].ToString();
                string sPrice = myReader["Price"].ToString();
                tbxBar.Text = sBarcode;
                tbxName.Text = sName;
                sDate = dateDate.Value;
                tbxPrice.Text = sPrice;
                tbxQua.Text = sQuantity;
}

another thing is that you should not be doing string concatenation when creating queries, please consider using the parameterized queries as your code is open to SQL Injection. You can read at How to: Execute a Parameterized Query know how to write parameterized queries.

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160