简体   繁体   中英

how to insert to database 00/00/0000 in datetime column when datetime picker checkbox not checked C# and SQL SERVER?

I would like to be able to insert to database 00/00/0000 or any other value except today's date value for datetimepicker if checkbox property not checked , I tried more than one solution from this site but its always inserting the datetime value for today even if checkbox not checked .

This is the save button code :

private void btnSave_Click(object sender, EventArgs e)
        {
            
            if (dateOpened.Checked==true)
            {
                dateOpened.Enabled = true;
                dateOpened.Format = DateTimePickerFormat.Short;
             
            }
            else
            {
                dateOpened.Enabled = false;
                dateOpened.Format = DateTimePickerFormat.Custom;
                dateOpened.CustomFormat = "00/00/0000";
            }

            if (dateFinished.Checked== true)
            {
                dateFinished.Enabled = true;
                dateFinished.Format = DateTimePickerFormat.Short;
            }
            else
            {
                dateFinished.Enabled = false;
                dateFinished.Format = DateTimePickerFormat.Custom;
                dateFinished.CustomFormat = "00/00/0000";
            }

            if (add==1)
            {
                test.ADD_INVENTORIES(Convert.ToInt32(comboTest.SelectedValue),txtLotNo.Text,
                                     Convert.ToInt32(txtNoOfTests.Text),
                                     DateTime.Parse(dateReceived.Value.ToString()),
                                     DateTime.Parse(dateExpiry.Value.ToString()),
                                     DateTime.Parse(dateOpened.Value.ToString()),
                                     txtReceivedfrom.Text,
                                     DateTime.Parse(dateFinished.Value.ToString()),
                                     
                                     Convert.ToInt32(comboDepts.SelectedValue),
                                     Convert.ToInt32(txtRemaning.Text),
                                     Convert.ToInt32(comboCompany.SelectedValue),
                                     Convert.ToInt32(comboType.SelectedValue));

                MessageBox.Show("Stock Added Successfully", "New Stock", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cleardata();
            }
}

This is the stored procedure code :

CREATE proc [dbo].[ADD_INVENTORIES]
@testid int,
@lot_no nvarchar(20),
@No_Of_Tests int,
@Received_Date datetime,
@Expiry_Date datetime,
@Opened_Date datetime,
@Received_From nvarchar(50),
@Finished_Date datetime,
@deptid int,
@Remaining_Tests int,
@Company_id int,
@reagent_type int



as 
INSERT INTO [dbo].[Inventory_Details]
           ([testid]
           ,[lot_no]
           ,[No_Of_Tests]
           ,[Received_Date]
           ,[Expiry_Date]
           ,[Opened_Date]
           ,[Received_From]
           ,[Finished_Date]
           ,[deptid]
           ,[Remaining_Tests]
           ,[Company_id]
           ,[reagent_type])
     VALUES
           (@testid
           ,@lot_no
           ,@No_Of_Tests
           ,@Received_Date
           ,@Expiry_Date
           ,@Opened_Date
           ,@Received_From
           ,@Finished_Date
           ,@deptid
           ,@Remaining_Tests
           ,@Company_id
           ,@reagent_type)

I want to insert to database 00/00/0000 for datetime columns (Opened_Date and Finished_Date) if checkbox not checked for datetimepicker. But its inserting the date and time for today's , I dont know where is the missing maybe I need to change the insert way and this way not correct :

DateTime.Parse(dateOpened.Value.ToString()),
DateTime.Parse(dateFinished.Value.ToString()),

How to solve this issue ?

I changed the way to deal with this case , I removed the columns Opened_Date and Finished_Date from insert statement because I dont use it first , I need to fill these dates later on when use these products that time I will create update stored procedure to update the open date when use the products and enter finish date when products finished.

CREATE proc [dbo].[ADD_INVENTORIES]
@testid int,
@lot_no nvarchar(20),
@No_Of_Tests int,
@Received_Date datetime,
@Expiry_Date datetime,
@Received_From nvarchar(50),
@deptid int,
@Remaining_Tests int,
@Company_id int,
@reagent_type int



as 
INSERT INTO [dbo].[Inventory_Details]
           ([testid]
           ,[lot_no]
           ,[No_Of_Tests]
           ,[Received_Date]
           ,[Expiry_Date]
           ,[Received_From]
           ,[deptid]
           ,[Remaining_Tests]
           ,[Company_id]
           ,[reagent_type])
     VALUES
           (@testid
           ,@lot_no
           ,@No_Of_Tests
           ,@Received_Date
           ,@Expiry_Date
           ,@Received_From
           ,@deptid
           ,@Remaining_Tests
           ,@Company_id
           ,@reagent_type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM