user3107343
user3107343

Reputation: 2299

The null value cannot be assigned to a member with type System.DateTime in C#

I tried populate a gridview from LinqDataSource.When page run I get this error :

Can not be null member of type System.DateTime which is a value type can not be assigned a null value.

Where is my mistake ?

Aspx

    <dx:ASPxDateEdit ID="dtTranferDateFirst" runat="server" 
    DisplayFormatString="dd.MM.yyyy dddd" ></dx:ASPxDateEdit>

   <dx:ASPxButton ID="btnGetTransfers" runat="server" Text="Get Transfers" 
    OnClick="btnGetTransfers_Click"></dx:ASPxButton>

   <dx:ASPxGridView  ID="grdTransferOrder"....
   .....
    ....
   </dx:ASPxGridView>


     <asp:LinqDataSource ID="lnqTransferOrder" 
     ContextTypeName="MyApp.Classes.ModelDataContext"
     TableName="TransferOrders" EnableDelete="True"  EnableInsert="True" 
     EnableUpdate="True" 
     runat="server" ondeleting="lnqTransferOrder_Deleting" 
     OnInserting="lnqTransferOrder_Inserting" OnUpdating="lnqTransferOrder_Updating"
     Where="Statues==0" EntityTypeName="">

    </asp:LinqDataSource>

C#

    private DateTime dtLastDate = DateTime.MaxValue;

    private void BindGrid()
    {
        this.lnqTransferOrder.Where = "DateScheduled >= DateTime.Parse(\"" + 
        this.dtTranferDateFirst.Date.ToString() + "\")&&DateScheduled < 
        DateTime.Parse(\"" + this.dtLastDate.Date.ToString() + "\") && Statues!=5";
        this.grdTransferOrder.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {

            this.dtTranferDateFirst.Date = System.DateTime.Today;
        }
        this.dtLastDate = this.dtTranferDateFirst.Date.AddDays(1.0);
        this.BindGrid();
    }

    protected void btnGetTransfers_Click(object sender, System.EventArgs e)
     {

     }

Upvotes: 0

Views: 444

Answers (1)

Denys
Denys

Reputation: 21

I think you are trying to bind a table (TransferOrders), which contains a nullable datetime column (dtTransferDateFirst), to a grid with a non-nullable datetime column. Try to set the column on table to non nullable.

Upvotes: 0

Related Questions