0

I am working on an accounting desktop application which gives many specific information about users.

The part that I am having trouble is here date = comboBoxEdit2.EditValue.ToString(); line should not be assigned as null as I did. But i could not find any better options and obviously, compiler gives me Object reference not set to an instance of an object.error. I'm using devexpress and found only .EditValue control to check info from my database.

Thank you.

Here's my complete method,

public void fillCombo2()
        {
            using (SqlConnection con = new SqlConnection("Server=DLL;Database=BSSERVIS;User Id=sa;Password=1;"))
            {
                con.Open();
                string date = ("select DATE from BSSERVIS.dbo.ENUMERATION");
                date = comboBoxEdit2.EditValue.ToString();
                System.Data.SqlClient.SqlDataReader dr = null;               
                SqlCommand cmd = new SqlCommand("select distinct(DATE) from BSSERVIS.dbo.ENUMERATION where WAREHOUSE_NR='" + date + "'");
                dr = cmd.ExecuteReader();
                dr.Read();
                while (dr.Read())
                {
                    string date = dr.GetString(0);
                    comboBoxEdit2.Properties.Items.Add(date);
                }

            } 
        }
oldcode
  • 1,669
  • 3
  • 22
  • 41
jrCoder
  • 9
  • 2
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Patrick Hofman May 15 '15 at 10:22
  • This might help: http://stackoverflow.com/help/mcve - reduce the problem to find the problem. Remove the date variable and hardcode it (temporarily) - if that works then edit the question to have only the *relevant* code. – freedomn-m May 15 '15 at 10:24

1 Answers1

0

You should check to ensure that comboBoxEdit2.EditValue contains a valid date value first, perhaps (and assuming this is C#; it isn't clear from the tags you have applied) using:

if (DateTime.TryParse(comboBoxEdit2.EditValue, out date))
{
    // Your query execution code
}

If it isn't a date, either don't execute the query, or set it up without the parameter.

Note also that your code is vulnerable to SQL Injection attacks, if a user enters malicious data into your text box. The above is better, but you should also look at using SQL parameters to make it more secure.

DateTime.TryParse will try to parse the expression as a DateTime object, and will return true if successful and false otherwise. If successful, the out parameter date will be populated with the DateTime object.

cf_en
  • 1,661
  • 1
  • 10
  • 18
  • It's a date, i'm trying to pick it up from my database. And yes it's a c# code. Sorry for bad tag choices – jrCoder May 15 '15 at 10:25
  • I mean you should check that the expression returns a date, using the code I quoted. If the user hasn't selected or entered a value, and therefore the expression is null, it won't parse as a date, and in your code you can react accordingly. – cf_en May 15 '15 at 10:28
  • Oh, now I understood your suggestion. – jrCoder May 15 '15 at 10:41