Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dealing with MSACCESS Date/Time Field 2

Status
Not open for further replies.

junwebhead

Programmer
Jun 13, 2003
41
QA
Hi all!

I hope that anyone can help me.
What I want to do is this:

When saving...
if the textbox = "", save null value in the date field.
otherwise save the date value.

When retrieving...
if the value in my field is null, put "" in the textbox.
otherwise get the date value and put it in the textbox.

I tried this code but got an error.
Code:
myDataRow["ExaminationDate"] = txtExaminationDate.Text == "" ? DBNull.Value : txtExaminationDate.Text;

I'm using MSAccess 2000. Thanks!
[smile]

Jun
 
Does the ExaminationDate column allow nulls?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for the response chip. There is no property in MS Access table design that says allow null. But I think it is allowed. In other language, "" can be saved in a date/time field. Maybe there is a implicit conversion performed there.

When I try the code above, I got this error.
Type of conditional expression can't be determined because there is no implicit conversion between 'class1' and 'class2'

I don't know how to fix this in C#. Thanks in advance.

Jun
 
I did a search in the help file for your error message, and it says this:
[tt]Conversions between classes are useful when you want objects of different classes to work with the same code. However, two classes that will work together cannot have mutual and redundant conversions.

To resolve CS0173, verify that there is one and only one implicit conversion between class1 and class2, regardless of which direction the conversion is in and regardless of which class the conversion is in.[/tt]

Your code:
Code:
myDataRow["ExaminationDate"] = txtExaminationDate.Text == "" ? DBNull.Value : txtExaminationDate.Text;
is going to do a type conversion because the indexer of the DataRow object (the Item property) is of type Object, and you're assigning either a System.DBNull.Value or a String (from the .Text property) to it.

One thing I noticed is that you're comparing the .Text property against a zero-length string. Instead of that, I would do something like this:
Code:
txtExaminationDate.Text.Trim().Length == 0
Additionally, since I don't use MS Access and am not entirely sure about your error, I would break up your conditional to several separate statements to see which one the compiler complains about.
Code:
if (txtExaminationDate.Text.Trim().Length == 0)
{
  myDataRow["ExaminationDate"] = System.DBNull.Value;
} 
else
{
  myDataRow["ExaminationDate"] = txtExaminationDate.Text;
}

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
1. The MSAccess database accepts NULL value in table fields.
Select a table in Design View ,General Tab-> Default Value -> Expression Builder ->Constant -> select NULL.
2. The error you get in your code is generated by the ? conditional operator.
3. Change the ExaminationDate column to accept NULL value in your MSAccess table.
4. Replace the conditional statement as chiph indicated in the previous reply and that is working 100%.

-obislavu-
 
Thanks Chiph and obislavu!
I did what Chiph said and it worked 100%. My code is just a little bit longer now but that's ok. Coz I think I can't use a method that would replace the ? conditional operator because I don't know what return type of that method will be (i.e., private (what type?) ExamDate(string strDateValue)) . I didn't change the table design coz the Date/Time field type already accept a null value.

Thanks again to you both! [smile]

Jun
 
junwebhead -

One additional tip. In MFC programming a convention is to place the constant part of a comparison (if any) on the left side of the comparison operator (==). In C/C++ what this does is cause a compiler error in case you forget an equals-sign, resulting in the assignment operator (=). So, instead of:
Code:
if (txtExaminationDate.Text.Trim().Length == 0)
You might consider doing it like this:
Code:
if (0 == txtExaminationDate.Text.Trim().Length)
C# is much better at flagging this common error, so it may not be absolutely necessary, but it might be good practice anyway. Leaving the second equals sign off with the 0 on the right results in:
[tt]
[tab]Cannot implicitly convert type 'int' to 'bool'
[tab]Property or indexer 'string.Length' cannot be assigned to -- it is read only
[/tt]
Leaving the second equals sign off with the 0 on the left results in:
[tt]
[tab]Cannot implicitly convert type 'int' to 'bool'
[tab]The left-hand side of an assignment must be a variable, property or indexer
[/tt]
Which, IMO anyway, is a clearer error message that you are trying to do an assignment where you intended to do a comparison.

BTW, if you try and shorten the statement to (in case you want to take advantage of type promotion like C and VB offer:
Code:
if (txtExaminationDate.Text.Trim().Length)
The compiler will flag it with:
[tt]
[tab]Cannot implicitly convert type 'int' to 'bool'
[/tt]
This is because of the much stronger type-checking. In VB they allow it as a time-saver. In C/C++ it's OK because the boolean type is really an int. C# has explicit types for ints and bools, so this results in a compiler error (which it should, from a strict interpretation of types).

Have a good weekend!
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top