如何插入null日期进入访问 [英] how to insert null date into access
问题描述
我想插入null日期到访问数据库如果用户付款选择作为现金然后检查日期应该插入为空的这个im使用掩蔽的文本框,我使用调试器,但每次调试器去条件和它的给我数据不匹配异常在这里是我给我的插入代码
i want to insert null date into access database if user payment choose as cash then cheque date should be insert as empty for this im using masked textbox , i use debugger also but every time debugger going else condition and for that its giving me data mismatch exception here is im giving my insert code
string bank = txtbankname.Text;
bank = "";
string cheque = txtchequeno.Text;
cheque = "";
string billno = txtbillno.Text;
billno = "";
string codecreate = txtcodecreator.Text;
codecreate = "";
string connetionString = null;
connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
cnn.ConnectionString = connetionString;
string SqlString = "Insert Into Billing([FormNo],[Date],[TruckNo],[Office_Code],[Party_Code],[Party_Code1],[Location],[Supplier],[Item],[Invoice_no],[Invoice_date],[Package],[Weight],[Invest_Amount],[Percentage],[Amount],[Total_Amount],[Payment_Amount],[Payment_Type],[Bank_Name],[Cheque_No],[Cheque_Date],[Bill_No],[Bill_Date],[Code_Create]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
using (cmd = new OleDbCommand(SqlString, cnn))
{
cnn.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FormNo", txtformno.Text);
cmd.Parameters.AddWithValue("@Date", txtdate.Text);
cmd.Parameters.AddWithValue("@TruckNo", txttruck.Text);
cmd.Parameters.AddWithValue("@Office_Code", txtofficecode.Text);
cmd.Parameters.AddWithValue("@Party_Code", txtpartycode.Text);
cmd.Parameters.AddWithValue("@Party_Code1", txtpartycode1.Text);
cmd.Parameters.AddWithValue("@Location", txtlocation.Text);
cmd.Parameters.AddWithValue("@Supplier", txtsupplier.Text);
cmd.Parameters.AddWithValue("@Item", txtitem.Text);
cmd.Parameters.AddWithValue("@Invoice_no", txtinvoice.Text);
cmd.Parameters.AddWithValue("@Invoice_date", DateTime.Parse(txtmaskinvoice.Text));
cmd.Parameters.AddWithValue("@Package", txtpackage.Text);
cmd.Parameters.AddWithValue("@Weight", txtwieght.Text);
cmd.Parameters.AddWithValue("@Invest_Amount", Convert.ToDouble(txtinvestamount.Text));
cmd.Parameters.AddWithValue("@Percentage", txtpercentage.Text);
cmd.Parameters.AddWithValue("@Amount", Convert.ToDouble(txtamount.Text));
cmd.Parameters.AddWithValue("@Total_Amount", Convert.ToDouble(txttotalamount.Text));
cmd.Parameters.AddWithValue("@Payment_Amount", Convert.ToDouble(txtpaymentamount.Text));
cmd.Parameters.AddWithValue("@Payment_Type", txtpaymenttype.Text);
if (txtbankname.Text == "")
{
cmd.Parameters.AddWithValue("@Bank_Name", bank);
}
else
{
cmd.Parameters.AddWithValue("@Bank_Name", txtbankname.Text);
}
if (txtchequeno.Text == "")
{
cmd.Parameters.AddWithValue("@Cheque_No", cheque);
}
else
{
cmd.Parameters.AddWithValue("@Cheque_No", txtchequeno.Text);
}
DateTime chequeDate;
var value = (object)DBNull.Value;
if (DateTime.TryParseExact(txtmaskchequedate.Text,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out chequeDate))
{
value = chequeDate;
cmd.Parameters.AddWithValue("@Cheque_Date", value);
}
else
{
cmd.Parameters.AddWithValue("@Cheque_Date",(txtmaskchequedate.Text));
}
if (txtbillno.Text == "")
{
cmd.Parameters.AddWithValue("@Bill_No", billno);
}
else
{
cmd.Parameters.AddWithValue("@Bill_No", txtbillno.Text);
}
DateTime BillDate;
var value1 = (object)DBNull.Value;
if (DateTime.TryParseExact(txtmaskbilldate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out BillDate))
{
value1 = BillDate;
cmd.Parameters.AddWithValue("@Bill_Date", value1);
}
else
{
cmd.Parameters.AddWithValue("@Bill_Date", (txtmaskbilldate.Text));
}
if (txtcodecreator.Text == "")
{
cmd.Parameters.AddWithValue("@Code_Create", codecreate);
}
else
{
cmd.Parameters.AddWithValue("@Code_Create", txtcodecreator.Text.ToString());
}
int n = cmd.ExecuteNonQuery();
cnn.Close();
if (n > 0)
{
MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
推荐答案
在文本框控件中输入的有效日期。但是如果不是你使用文本框的内容作为参数值。 - 这当然不对。
You check in if there is a valid date entered in your textbox control. But if it is not you use the content of the textbox as parameter value anyway. - This certainly is not right.
由于 value
初始化为DBNull,因此您可以在任何情况下添加参数并且不需要你的代码的else部分。
更改您的代码:
As value
is initialized to DBNull, you can add the parameter in any case and don't need the else part of your code at all.
Change your code:
DateTime chequeDate;
var value = (object)DBNull.Value;
if (DateTime.TryParseExact(txtmaskchequedate.Text,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out chequeDate))
{
value = chequeDate;
}
cmd.Parameters.AddWithValue("@Cheque_Date", value);
从文本框中添加文本c $ c> @Bill_Date 参数。根据我的示例,也重写该参数的代码。
The exact same problem with adding the text from textbox even though it is invalid, exists for the @Bill_Date
parameter. Rewrite the code for that parameter according to my sample as well.
这篇关于如何插入null日期进入访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!