Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:38:39+00:00 2026-06-18T07:38:39+00:00

Visual C# 2008 Express. Access 2010 database. When I use the following code I

  • 0

Visual C# 2008 Express. Access 2010 database. When I use the following code I receive an error ‘Data type mismatch in criteria expression’, but if I take out the code for the txt85x11bw_Validating event it works just fine. I’m trying to understand how there is a date type mismatch.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace GISReciepts
{
public partial class GISReceipts : Form
{
    public GISReceipts()
    {
        InitializeComponent();
    }

    private void cmdExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void cmdAdd_Click(object sender, EventArgs e)
    {
        string strSQL = "INSERT INTO Table1(Name1, Date1, CollectedBy, 85x11BW) VALUES(@Name, @Date, @CollectedBy, @85x11BW)";

        //OleDbConnection is a class that represents an open connection to a data source
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Temp\\GISTest.accdb");
        //OleDbCommand is a class that represents an SQL statement or stored procedure to execute against a data source.(takes care of passing queries to the database.
        OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection);
        myCommand.Parameters.AddWithValue("@Name", txtName.Text);
        myCommand.Parameters.AddWithValue("@Date", maskedTextBoxDate.Text);
        myCommand.Parameters.AddWithValue("@CollectedBy", txtCollectedBy.Text);
        myCommand.Parameters.AddWithValue("@85x11BW", txt85x11bw.Text);

        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            myConnection.Close();
           // MessageBox.Show("Entry Added");  //this is displaying even on errors & when no data is added to table
        }

    }

    private void GISReceipts_Load(object sender, EventArgs e)
    {
        maskedTextBoxDate.Text = DateTime.Today.ToString("MM/dd/yyyy");

        txt85x11bw.Validating += new CancelEventHandler(txt85x11bw_Validating);       
    }


    private void GISReceipts_FormClosing(object sender, FormClosingEventArgs e)
    {
        DateTime value;
        //DateTime.TryParse converts the srting representation of a date to its DateTime equivalent & returns a value that indicates whether the conversion succedded.
        if (!DateTime.TryParse(maskedTextBoxDate.Text, out value))
        {
            maskedTextBoxDate.Text = DateTime.Today.ToShortDateString();
        }

    }

    private void txt85x11bw_Validating(object sender, CancelEventArgs e)
    {
        if (string.IsNullOrEmpty(txt85x11bw.Text))
        {
            //do nothing
        }
        else
        {
            //initialize the variable numberEntered of type int
            int numberEntered;

            //int=integer TryParse=method that converts strings into integer (value, out result)
            if (int.TryParse(txt85x11bw.Text, out numberEntered))
            {
                //if number is less than 1 or more than 1,000
                if (numberEntered < 1 || numberEntered > 1000)
                    MessageBox.Show("You must enter a valid number");
                txt85x11bw.Clear();

            }
            //if conversion failed
            else
            {
                MessageBox.Show("You must enter a number");
                txt85x11bw.Clear();

            }
        }


    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:38:40+00:00Added an answer on June 18, 2026 at 7:38 am

    This line adds a parameter with a datatype text, instead, I assume, that in your database the column 85x11BW is of numeric type (an int looking at your validating code)

        myCommand.Parameters.AddWithValue("@85x11BW", txt85x11bw.Text);
    

    try to change the line in this way

        myCommand.Parameters.AddWithValue("@85x11BW", Convert.ToInt32(txt85x11bw.Text));
    

    in this way you force the AddWithValue method to add a parameter with its DbType property set to Int32. You could have the same problem for the parameter @Date, if the relative datatable column is of DateTime type.

    EDIT If the txt85x11BW is empty you need to decide which default value you accept for that column in the datatable. If you accept a NULL you could write something like this

            int numberEntered = 0;
            int.TryParse(txt85x11bw.Text, out numberEntered);
            if (numberEntered >= 1 && numberEntered <= 1000)
                 myCommand.Parameters.AddWithValue("@85x11BW", numberEntered);
            else
                 myCommand.Parameters.AddWithValue("@85x11BW", DBNull.Value);
    

    EDIT 2 In the validating event the code clears the content of the maskedit also if the value is correct. You need braces after the condition

    if (int.TryParse(txt85x11bw.Text, out numberEntered))
    {
        //if number is less than 1 or more than 1,000
        if (numberEntered < 1 || numberEntered > 1000)
        {
            MessageBox.Show("You must enter a valid number");
            txt85x11bw.Clear();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Visual Studio 2008 Express edition, and keep getting the following error: Cascadedisplay.h(4)
I'm using Visual Studio 2008 Express Edition to compile the following code in a
I'm using Visual C++ 2008 Express Edition and when i debug code: double x
ETA: I use visual studio 2008 express edition. If I override WndProc and mess
I'm trying to use the Visual Studio 2010 Express editions to set up an
I am writing a simple C++ database, using Visual Studio 2008 express edition, like
I keep getting this error when I try and use an MS Access database
I'm using Visual Studio 2008 Express, and I have this logging code: fprintf(fp,%s %s
I find it odd that in Visual C# 2008 Express edition, when you use
In Visual C++ 2008 Express Edition when adding forms all of the default handlers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.