Amrit Sharma
Amrit Sharma

Reputation: 1916

Comparing dataset value

In winform I am trying to compare the returned dataset value to defined value but I am getting following error.

Visual Studio Error

In the code below, I am passing the string from listbox to the GetStock method

public bool checkStock()
        {
            foreach (var listBoxItem in listBox1.Items)
            {
                if (Convert.ToInt32(GetStock(listBoxItem.ToString())) == 0)
                {
                    MessageBox.Show(listBoxItem.ToString() + " not in Stock!. Please delete the item before proceeding");
                    return false;
                }
            }
            return true;
        }

Code for GetStock()

 public DataSet GetStock(string product)
        {
            DataSet dataSet = new DataSet();
            OleDbConnection oleConn = new OleDbConnection(connString);

            try
            {
                oleConn.Open();
                string sql = "SELECT [Quantity] FROM [Product] WHERE [Product Name]='" + product + "'";
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                dataAdapter.Fill(dataSet, "Product");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                oleConn.Close();
            }
            if (dataSet.Tables["Product"].Rows.Count <= 0)
                return null;

            return dataSet;
        }

    }
}

In database the "Quantity" is in string format.

Upvotes: 2

Views: 449

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460038

GetStock returns a DataSet but you're passing it to Convert.ToInt32:

if (Convert.ToInt32(GetStock(listBoxItem.ToString())) == 0)

You probably want something like this:

int stockCount = GetStockQuantity(listBoxItem.ToString());
if (stockCount == 0)
{
    MessageBox.Show(listBoxItem.ToString() + " not in Stock!. Please delete the item before proceeding");
}

Here is the modified method(note that i'm using parameters)

public int GetStockQuantity(string product)
{
    int quantity = 0;
    string sql = "SELECT TOP 1 [Quantity] FROM [Product] WHERE [Product Name] = ?";
    using(var oleConn = new OleDbConnection(connString))
    using(var cmd = new OleDbCommand(sql , oleConn))
    {
        cmd.Parameters.AddWithValue("?", product);
        try
        {
            oleConn.Open();
            object obj_quantity = cmd.ExecuteScalar();
            if(obj_quantity != null)
                // "In database the "Quantity" is in string format." 
                // Change it to int if possible
                int.Parse((string)obj_quantity);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
    return quantity;
}

Upvotes: 4

spajce
spajce

Reputation: 7082

try this way:

var count = GetStock(listBoxItem.ToString());

if (count > 0)
{
  //TODO: other stuff
}
else
{
   MessageBox.Show(listBoxItem.ToString() + " not in Stock!. Please delete the item before proceeding");
}

Upvotes: 0

dan radu
dan radu

Reputation: 2782

The GetStock method returns a DataSet, but you need the actual int value. Try this:

Convert.ToInt32(GetStock(listBoxItem.ToString()).Tables["Product"].Rows[0]["Quantity"])

Upvotes: 1

Related Questions