Databound DateTime field question

J

justin

Hello all:

I have a FormView that has it's DataSourceID set to a SqlDataSource
that uses stored procedures for it's select, update, insert, and
delete. I want the FormView to have an "autosave" feature, so based on
certain intervals or user interactions I call the

FormView.UpdateItem(false); function in my C# code behind.

My problem is that a TextBox that I have in my FormView is bound to a
DateTime column in the database, and if no value is in the TextBox
then a date of Jan. 1, 1900 is saved to the database. The column
accepts null values. How can I get my TextBoxes that are bound to
DateTime columns save null when no value has been entered?

Any help or leads is greatly welcomed! Thank you in advance.
 
E

ed

Hi justin,

Assuming that your underlying SQL datatype is smalldatetime (1/1/1900
is the smalldatetime type's minimum value) it looks like the value
from the textbox is being treated as blank, not null. You're going to
have to jump in somewhere and swap that blank for a null when you
update.

Does that give you enough to work with? If not, I'd recommend posting
your function and/or your SP's -- perhaps even the non-default
properties to the bound textbox.

Hope this helps,
Ed
 
J

justin

Hi justin,

Assuming that your underlying SQL datatype is smalldatetime (1/1/1900
is the smalldatetime type's minimum value) it looks like the value
from the textbox is being treated as blank, not null. You're going to
have to jump in somewhere and swap that blank for a null when you
update.

Does that give you enough to work with? If not, I'd recommend posting
your function and/or your SP's -- perhaps even the non-default
properties to the bound textbox.

Hope this helps,
Ed

That makes sense, but I guess I'm not sure how to "jump in somewhere".
I don't see any events for the TextBox that would happen before
updating the database.

I can think of a few ways to replace the DateTime value if I wasn't
using the SqlDataSource and the update StoredProcedure, but what I'm
really looking for is something that will work with the DataSource. If
there was a default value for the TextBox or an event that fired
before an update on just the TextBox that would work, but I don't see
anything like that.
 
J

justin

What about in your update SP check for blank and substitute null

Lit -
Great idea, not exactly the angle I was going for but same result. In
my SQL I used a CASE and checked the value, for what it's worth,
here's the SQL

randomDateField = CASE WHEN @randomDateField = '' THEN NULL ELSE
@randomDateField

Works great, thanks everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top