Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to set the value of Numeric up down control from the database,
i m using this code. but it gives me "Object reference not set to an instance of an object. " error.

NumericUpdown1.Value = Convert.ToDecimal(dr[0].ToString());


anyone please help me

Thanks in advance!
Posted
Comments
Sergey Alexandrovich Kryukov 14-Feb-12 16:34pm    
Could you run it under Debugger? asking such questions makes no sense.
--SA

Either dr[0] is null or NumericUpdown1 is null. I would suspect it's dr[0], confirm your query returns a value or check it for being null prior to converting it and assigning the value to your updown control.
 
Share this answer
 
Comments
jeet786 14-Feb-12 9:24am    
yes my query returns value from database.
and minimum and maximum value of numeric updown is 1 and 30 respectively
Sergey Alexandrovich Kryukov 14-Feb-12 16:39pm    
Don't you see that the whole idea makes no sense, even if it was not null?! Converting to string and then back to numeric type...

Please see my answer.
--SA
Paul E Davies 15-Feb-12 2:28am    
As SA suggests run it under a debugger and put a break point on that line (assuming you're using VS) you can then use a watch or locals to see which object is null.
The whole idea makes no sense. You have something potentially numeric (dr[0] was not numeric, you could not expect numeric on parsing the string, but in fact it is null), try to convert it to string using ToString, and then try to parse this string back to numeric type Decimal. It tells only one thing: you have no idea what are you doing.

—SA
 
Share this answer
 
Comments
Paul E Davies 15-Feb-12 2:20am    
Whilst the question does have problems your "solution" does not answer the question. Sure once we've established why he's getting a null reference exception we can advise on good practise. To say "you have no idea what you are doing" does not help, except perhaps to massage your ego.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900