0

i want to insert null date into access database, if user select payment option as cash then cheque_date value should automatically enter as null if user not provide bill then bill_date should be enter as null in database iam trying to do this but Data type mismatch in criteria expression exception is coming for bill|_date and cheque_date im using masked textbox and for chequeno. and bill no. im using textbox(checkno and billno. its can be store as empty only problem is storing null date into accessc database)

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

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

here im change my code but still same error showing me Data type mismatch expression not getting still how to solve

11
  • 1
    Are you looking for DBNull.Value? Btw. your code seems weird. For example if "Cheque_Date" really is a Date column, you're probably not allowed to feed it the contents of txtmaskchequedate.Text, which is a string, not a DateTime. Commented Apr 13, 2016 at 9:11
  • yes dbnull.value only but if user not enter anything then value should be null Commented Apr 13, 2016 at 9:27
  • i change my code if (DateTime.Parse(txtmaskchequedate.Text)==null) { cmd.Parameters.AddWithValue("@Cheque_Date", DBNull.Value); } else { cmd.Parameters.AddWithValue("@Cheque_Date", txtmaskchequedate.Text); } but its give me String was not recognized as a valid DateTime exception Commented Apr 13, 2016 at 9:47
  • That exception is thrown, because the content of txtmaskchequedate.Text cannot be parsed to a DateTime. Btw. DateTime.Parse will never return null. See DateTime.TryParse for how to try to parse a string that might represent a DateTime. Commented Apr 13, 2016 at 10:05
  • Possible duplicate of how to insert null date into access Commented Apr 13, 2016 at 10:05

1 Answer 1

0

i set format : shortdate for cheque_date field in access database and here executed following code which is executed successfully either field having value or not(thank you @phils and @corak)

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.