嗨,有人可以帮我解决这个问题吗? 我想从datetimepicker向数据库发送一个空值,但它不起作用,它没有给出错误,但每次我取消选中datetimepicker附近的checbox日期仍然进入:(任何人都可以帮助这个
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();
答案 0 :(得分:1)
看起来不像参数 purchdate 在任何地方使用。
也许尝试这样的事情:
//cmd.Parameters.Add("@purchasedate", SqlDbType.DateTime).Value = Date.Text;
if (Date.Checked)
cmd.Parameters.AddWithValue("@purchasedate", Date);
else
cmd.Parameters.AddWithValue("@purchasedate", DBNull.Value);
答案 1 :(得分:0)
它与以下几行有什么关系吗?
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代码,以便我们获得更多信息来回答。