从datetimepicker向数据库发送空值

本文关键字:空值 数据库 datetimepicker | 更新日期: 2023-09-27 18:06:39

嗨,有人能帮我解决这个问题吗?我想从datetimepicker发送一个空值到数据库,但它不起作用,它不会给出一个错误,但每次我取消选中datetimepicker附近的复选框,日期仍然进入:(任何人都可以帮助这个

    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();

从datetimepicker向数据库发送空值

参数 purchasdate 似乎没有在任何地方使用。

可以试试这样写:

   //cmd.Parameters.Add("@purchasedate", SqlDbType.DateTime).Value = Date.Text;
   if (Date.Checked)
        cmd.Parameters.AddWithValue("@purchasedate", Date);
   else
        cmd.Parameters.AddWithValue("@purchasedate", 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);

您在if之外设置@purchasedate参数,因此它总是被设置。

如果这不是问题,你应该发布UI代码,让我们有更多的信息来回答。