Search code examples
c#ms-accessoledb

C# to Access connection in Visual Form OleDb No data exists for the row/column OR invalid output


I am trying to get a Text value from a single cell in an Access form, but this code gives me System.Data.OleDb.OleDbDataReader as an output instead. When I change Sell to *, and Convert.ToString(cusReader) to Convert.ToString(cusReader.getValue(1)), I get the error in the title. How do I fix this? Thanks, Jack.

 OleDbCommand cmd = new OleDbCommand("SELECT Sell FROM Product WHERE ID = " + Count, conn);
            OleDbDataReader cusReader = cmd.ExecuteReader();

            if (Count != 0)
            {
                labelInsertedExtra.Visible = true;
                labelInserted.Visible = true;
                labelInsertedExtra.Text = Convert.ToString(cusReader);
            }
            else
            {
                labelInsertedExtra.Visible = false;
                labelInserted.Visible = false;
            }
            cusReader.Close();

Solution

  • Indice start to 0

       Convert.ToString(cusReader.getValue(0)) 
    

    you should test dbnullvalue too

          while (cusReader.Read())
          {
    
            if (Count != 0 && cusReader["Sell"] != DBNull.Value)
            {
                labelInsertedExtra.Visible = true;
                labelInserted.Visible = true;
                labelInsertedExtra.Text = cusReader["Sell"].ToString(); 
            }
            else
            {
                labelInsertedExtra.Visible = false;
                labelInserted.Visible = false;
            }
    
            break;
          }