0

I am trying to get data in gridview on the basis of the date that is entered in dateTimePicker. But, I am getting null reference runtime error on if condition where I have used equals function to compare two strings.

ReportFrom.cs

private void button1_Click(object sender, EventArgs e)
{
        string date = dateTimePicker.Value.ToShortDateString();
        reportLayer.MakeDailyReport(date, dataGridViewReport);
}

ReportLayer.cs

private SqlConnection con = new SqlConnection("Data Source=CHAMP-PC;Initial Catalog=ProcessSale;Integrated Security=True");
private SqlCommand cmd;
private SqlDataAdapter adapt;

public void MakeDailyReport(string givenDate, DataGridView view)
{
    try
    {
        con.Open();

        DataTable dt = new DataTable();

        cmd = new SqlCommand("SELECT Date FROM FinalSales where Date = @datePicker", con);
        cmd.Parameters.AddWithValue("@datePicker", givenDate);

        cmd.ExecuteNonQuery();

        object dateObject = cmd.ExecuteScalar();

        string dateObjectstring = Convert.ToString(dateObject);
        string givenDateString = Convert.ToString(givenDate);
        // string DBdate = dateObject.ToString();

        if (dateObject.Equals(givenDate))
        {
            adapt = new SqlDataAdapter("SELECT Date FROM FinalSales where Date = " + givenDate + "", con);

            if (adapt != null)
            { 
                adapt.Fill(dt);

                view.DataSource = dt;
            }
            else
            {
                MessageBox.Show("No Record found againts that date");
                con.Close();
            }
        }
        else
        {
            con.Close();
        }
    }
    catch (Exception a)
    {
        MessageBox.Show(a.Message);
        con.Close();
    } 
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    where is the null reference occuring? – Takarii Jun 20 '16 at 10:53
  • 1
    Clearly `dateObject` is null. You need to work out why that is. What does your first query return if you run it directly? – ChrisF Jun 20 '16 at 11:01
  • kindly mention by mistake why i am getting dateObject null. – Hassaan sohail Jun 20 '16 at 11:03
  • 1
    [SQL Injection alert](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - you should **not** concatenate together your SQL statements - use **parametrized queries** instead to avoid SQL injection – marc_s Jun 20 '16 at 11:21

1 Answers1

1

Have a look here: Handling ExecuteScalar() when no results are returned

Additionally: Be careful with the call to Equals(). Currently you are comparing two strings. One with a ShortDate value One with the default ToString().

Event if the dates are equal, this might return false.

A better solution would be handling both values as DateTime and use the == operator.

Thomas

Community
  • 1
  • 1
Thomas Voß
  • 1,145
  • 8
  • 20
  • Thanks its works. but now i am getting invidual name of "Jun" when i am entering 19-June-2016 on input felid. – Hassaan sohail Jun 20 '16 at 12:00
  • This is most likely a problem with the Culure you have set. see: https://msdn.microsoft.com/en-us//library/5hh873ya(v=vs.90).aspx – Thomas Voß Jun 20 '16 at 12:36