嗨有人可以幫我解決這個問題嗎? 我想一個空值發送到從的DateTimePicker數據庫,但它不工作,它不給一個錯誤,但每次我靠近取消對的DateTimePicker日期仍然進入:(在checbox誰能請與本從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();
幫助
能否請您命名日期控制的其他東西,然後日期?您需要調試代碼並找出「purchdate」沒有被設置爲NULL的原因。我將猜測默認情況下DateTime不能等於NULL。 –