1

1. Here you can see I am using Count(*) query and I am getting error there is a picture with the code

private void List_Load(object sender, EventArgs e)
{

    try
    {
        if (Sqlcon.State == ConnectionState.Closed)
        {
            Sqlcon.Open();
        }
        string query = "Select Count(*) from AddCars ";
        SqlDataAdapter sdr = new SqlDataAdapter(query, Sqlcon);
        sdr.Fill(dt);
        totaltbl.Text = dt.Rows[0][0].ToString();



    }
    catch (Exception ex)
    {
        MessageBox.Show("Error" + ex);
    }

}

Image of error

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Noman Hassan
  • 239
  • 2
  • 10
  • The exception says it all. You're calling `DbDataAdapter.FillData()`, but the value you passed for the `dataTable` parameter is `null`. Don't do that. See duplicate for how to figure out why `dt` is null and how to fix that. – Peter Duniho Jun 26 '21 at 00:30
  • By the way, for a query with a single result, you can just use `SqlCommand.ExecuteScalar` – Charlieface Jun 27 '21 at 02:26

0 Answers0