C#:Error:Object reference set to an instance of object -


i working on accounting desktop application gives many specific information users.

the part having trouble here date = comboboxedit2.editvalue.tostring(); line should not assigned null did. not find better options , obviously, compiler gives me object reference not set instance of object.error. i'm using devexpress , found .editvalue control check info database.

thank you.

here's 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 bsservis.dbo.enumeration");                 date = comboboxedit2.editvalue.tostring();                 system.data.sqlclient.sqldatareader dr = null;                                sqlcommand cmd = new sqlcommand("select distinct(date) bsservis.dbo.enumeration warehouse_nr='" + date + "'");                 dr = cmd.executereader();                 dr.read();                 while (dr.read())                 {                     string date = dr.getstring(0);                     comboboxedit2.properties.items.add(date);                 }              }          } 

you should check ensure comboboxedit2.editvalue contains valid date value first, perhaps (and assuming c#; isn't clear tags have applied) using:

if (datetime.tryparse(comboboxedit2.editvalue, out date)) {     // query execution code } 

if isn't date, either don't execute query, or set without parameter.

note code vulnerable sql injection attacks, if user enters malicious data text box. above better, should @ using sql parameters make more secure.

datetime.tryparse try parse expression datetime object, , return true if successful , false otherwise. if successful, out parameter date populated datetime object.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -