0
votes

I have a table in my database which is named Patients. The table has a column which is named DOB and has Allow Null value to true. Here is the table structure:

  • Id int IDENTITY(1,1) NOT NULL,
  • FirstName nvarchar(50) NULL,
  • LastName nvarchar(50) NULL,
  • Sex bit NULL,
  • Weight decimal(18, 0) NULL,
  • Height decimal(18, 0) NULL,
  • DOB date NULL,
  • PatientId nvarchar(50) NOT NULL

I'm using LinqToSql. The problem is that when I want to set a null for DOB column then Visual Studio tells that DOB is not nullable. Here is my code:

Patient tblPatient = new Patient();
if (txtYear.Text != "" && txtMonth.Text != "" && txtDay.Text != "")
                    tblPatient.DOB = Utility.ConvertPersianDateToGregorianDate(txtYear.Text + "/" + txtMonth.Text + "/" + txtDay.Text);
                else
                    tblPatient.DOB = null;  // The error is in this line
1
In your class Patient what is the type of DOB property? - Felipe Oriani
@FelipeOriani, it is DateTime. - Mohsen
The David's awnser will solve your problem :) - Felipe Oriani

1 Answers

3
votes

What is the type for the DOB property on Patient? Based on the description, presumably it's something like this (shortened for brevity):

public DateTime DOB { get; set; }

In order to set a null value, it needs to be a nullable type, such as:

public DateTime? DOB { get; set; }

When you generate the code from the data model, Linq to SQL should have done this for you. If the code is manually created, you'll need to update it accordingly. If it's auto-generated, you'll likely need to re-generate it. (It seems likely that it was generated against an older model of the data where the column was not nullable.)