7
Answers

invalid cast

ta mu

ta mu

10y
1.1k
1
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;


            }
Answers (7)