i have this code on button click when there is no record between selected dates therfore nothing is to sum as below i shown i got error "Specified cast is not valid." how i can do that if there is no record show a messag
private void button2_Click(object sender, EventArgs e)
{
label4.Text = "";
try
{
my.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select * from add_investor Where date>='" + dateTimePicker1.Value + "'AND date<='" + dateTimePicker2.Value + "' ", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
using (SqlCommand cmd = new SqlCommand("SELECT SUM(investor_amount) FROM add_investor WHERE date>='" + dateTimePicker1.Value + "'AND date<='" + dateTimePicker2.Value + "' ", my))
{
my.Open();
int result = (int)cmd.ExecuteScalar();----------------------------------------error
label4.Text = result.ToString();
my.Close();
}
}
catch
{
throw;
}
Loading

VulpesRank #55 · 29.2K reputationPosted Oct 2, 2013, 10:16 AM EDT
VulpesRank #55 · 29.2K reputationPosted Oct 2, 2013, 4:17 AM EDT
ta mu198 reputationPosted Oct 2, 2013, 4:27 AM EDT
result = (int)temp; Specified cast is not valid.
result value is zero
VulpesRank #55 · 29.2K reputationPosted Oct 2, 2013, 4:38 AM EDT
If you're summing investor_amounts, I'd have thought that double or decimal would be more likely.
ta mu198 reputationPosted Oct 2, 2013, 5:05 AM EDT
VulpesRank #55 · 29.2K reputationPosted Oct 2, 2013, 5:31 AM EDT
ta mu198 reputationPosted Oct 2, 2013, 9:04 AM EDT
now how i can manage it
thanks for reply