简体   繁体   中英

sending null values from datetimepicker to the database

hi can someone please help me with this issue? i want to send a null value to the database from datetimepicker but it doesnt work, it doesnt give an error but everytime i uncheck the checbox near on the datetimepicker the date still enters :( can anyone please help with this

    private void BtnSave_Click(object sender, EventArgs e)
    {

        using (SqlConnection sqlConn = new SqlConnection("Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True"))
        {
            string sqlQuery = @"INSERT INTO cottonpurchase VALUES(@slipNo, @purchasedate, @farmercode, @farmername, @villagename, @basicprice, @weight, @totalamountbasic, @premium, @totalamountpremium, @totalamountpaid,  @certstatus)";



            using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
            {
                cmd.Parameters.Add("@slipNo", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtSlipNo.Text)) ? int.Parse(TxtSlipNo.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@purchasedate", SqlDbType.DateTime).Value = Date.Text;
                if (Date.Checked)
                    cmd.Parameters.AddWithValue("purchdate", Date);
                else
                    cmd.Parameters.AddWithValue("purchdate", DBNull.Value);
                 cmd.Parameters.Add("@farmercode", SqlDbType.Int).Value = TxtFarmerCode.Text;

                cmd.Parameters.Add("@farmername", SqlDbType.VarChar, 100).Value = TxtFarmerName.Text;
                cmd.Parameters.Add("@villagename", SqlDbType.VarChar, 100).Value = TxtVillageName.Text;
                cmd.Parameters.Add("@basicprice", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtBasicPrice.Text)) ? int.Parse(TxtBasicPrice.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@weight", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtWeight.Text)) ? int.Parse(TxtWeight.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@totalamountbasic", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountBasic.Text)) ? int.Parse(TxtTotalAmountBasic.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@premium", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtPremium.Text)) ? int.Parse(TxtPremium.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@totalamountpremium", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountPremium.Text)) ? int.Parse(TxtTotalAmountPremium.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@totalamountpaid", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountPaid.Text)) ? int.Parse(TxtTotalAmountPaid.Text) : (object)DBNull.Value;
                //d.Parameters.Add("@yeildestimates", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtYeildEstimates.Text)) ? int.Parse(TxtYeildEstimates.Text) : (object)DBNull.Value;
                cmd.Parameters.Add("@certstatus", SqlDbType.VarChar, 100).Value = comboBox1.Text;
                sqlConn.Open();

It doesn't look like parameter purchdate is used anywhere.

Maybe try something like this:

   //cmd.Parameters.Add("@purchasedate", SqlDbType.DateTime).Value = Date.Text;
   if (Date.Checked)
        cmd.Parameters.AddWithValue("@purchasedate", Date);
   else
        cmd.Parameters.AddWithValue("@purchasedate", DBNull.Value);

Doesn't it have anything to do with the following lines?

cmd.Parameters.Add("@purchasedate", SqlDbType.DateTime).Value = Date.Text;
if (Date.Checked)
    cmd.Parameters.AddWithValue("purchdate", Date);
else
    cmd.Parameters.AddWithValue("purchdate", DBNull.Value);

You are setting the @purchasedate param outside the if, so it's always setted.

If this is not the problem, you should post the UI code, for us to have some more info to answer.

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