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();
    } 
}
4
  • 1
    where is the null reference occuring? Commented Jun 20, 2016 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? Commented Jun 20, 2016 at 11:01
  • kindly mention by mistake why i am getting dateObject null. Commented Jun 20, 2016 at 11:03
  • 1
    SQL Injection alert - you should not concatenate together your SQL statements - use parametrized queries instead to avoid SQL injection Commented Jun 20, 2016 at 11:21

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks its works. but now i am getting invidual name of "Jun" when i am entering 19-June-2016 on input felid.
This is most likely a problem with the Culure you have set. see: msdn.microsoft.com/en-us//library/5hh873ya(v=vs.90).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.