-
Nov 24th, 2017, 09:30 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value
Hi,
I've created a custom DateTimePicker which shows empty If Databinded to DBNull.Value, with a trick of setting CustomFormat. Only problem I'm facing is to return DBNull.Value from DateTimePicker when I save changes to DB. Here is my code:
Code:
public object MyValue
{
//Binding with DBNull
get
{
if (this.Format == DateTimePickerFormat.Custom)
{
return DBNull.Value;
}
else
{
return this.Value;
}
}
set
{
if (value == DBNull.Value)
{
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
//DateTime? dt =DBNull.Value; //error here
//Value = (DateTime)dt;
}
else
{
this.Format = DateTimePickerFormat.Short;
this.Value = (DateTime)value;
}
}
}
I bind to DateTimePicker simply as this: DTP.DataBindings.Add("MyValue", dt, "SOME_COLUMN");
Error I'm facing is "Nullable object must have a value?".
Can somebody help me If there is any solution to this ? Currently I just manually set DB.NULL in place where stored procedure is executed, and I want to get rid of these lines.
-
Nov 24th, 2017, 09:40 AM
#2
Thread Starter
Fanatic Member
Re: DateTimePicker - custom DataBinding to return DBNull.Value
Solved.
Only problem was that I was referencing "DTP.Value" in my stored procedure, but I should reference "DTP.MyValue" instead. Saving to DB now works, with DBNull.Value At least someone could benefit a little in future when dealing with DateTimePicker like this
-
Nov 25th, 2017, 03:08 AM
#3
Re: [RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value
Don't use a name like MyValue for a property. Something like BindableValue would be more appropriate.
-
Nov 25th, 2017, 05:13 AM
#4
Thread Starter
Fanatic Member
Re: [RESOLVED] DateTimePicker - custom DataBinding to return DBNull.Value
Hi jm, thanks for that. It's not MyValue in real actually, It's some word in my language so I'm pretty sure CLR won't mix It with something else
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|