I'm looking to fetch sum of ALLOCATED_DEPOSIT from Allocations using Invoice_No. I'm getting all the details through datareader but only execute scalar is being a problem for me. I tried everything but using the following code it gives "data type mismatch error" always.
connection.Close();
try
{
connection.Open();
string cstrQuery = "SELECT SUM (ALLOCATED_DEPOSIT)FROM ALLOCATIONS WHERE INVOICE_NO= " + int.Parse(txt_Invoiceno.Text);
OleDbCommand cmd = new OleDbCommand(cstrQuery, connection);
int sum = Convert.ToInt32(cmd.ExecuteScalar());
//long sum = (long)dt.Compute("Sum(ALLOCATED_DEPOSIT)", "True");
textBox2.Text = sum.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
connection.Close();