Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的文本框验证事件中的代码导致错误';条件表达式中的数据类型不匹配';_C#_Ms Access - Fatal编程技术网

C# 我的文本框验证事件中的代码导致错误';条件表达式中的数据类型不匹配';

C# 我的文本框验证事件中的代码导致错误';条件表达式中的数据类型不匹配';,c#,ms-access,C#,Ms Access,Visual C#2008 Express。Access 2010数据库。当我使用以下代码时,我收到一个错误“条件表达式中的数据类型不匹配”,但如果我取出txt85x11bw_验证事件的代码,它就可以正常工作。我试图了解日期类型不匹配的原因 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syste

Visual C#2008 Express。Access 2010数据库。当我使用以下代码时,我收到一个错误“条件表达式中的数据类型不匹配”,但如果我取出txt85x11bw_验证事件的代码,它就可以正常工作。我试图了解日期类型不匹配的原因

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

            }
        }


    }

}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Data.OleDb;
命名空间GisReceipts
{
公共部分类:表单
{
公共收入()
{
初始化组件();
}
私有void cmdExit_单击(对象发送方,事件参数e)
{
这个。关闭();
}
私有void cmdAdd_单击(对象发送方,事件参数e)
{
string strSQL=“插入表1(名称1,日期1,CollectedBy,85x11BW)值(@Name,@Date,@CollectedBy,@85x11BW)”;
//OleDbConnection是一个表示到数据源的开放连接的类
OleDbConnection myConnection=新的OleDbConnection(“Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:\\Temp\\GISTest.accdb”);
//OleDbCommand是一个类,表示要对数据源执行的SQL语句或存储过程。(负责将查询传递到数据库。
OleDbCommand myCommand=新的OleDbCommand(strSQL,myConnection);
myCommand.Parameters.AddWithValue(“@Name”,txtName.Text);
myCommand.Parameters.AddWithValue(“@Date”,maskedTextBoxDate.Text);
myCommand.Parameters.AddWithValue(“@CollectedBy”,txtclectedby.Text);
myCommand.Parameters.AddWithValue(@85x11BW”,txt85x11bw.Text);
尝试
{
myConnection.Open();
myCommand.ExecuteOnQuery();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
最后
{
myConnection.Close();
//MessageBox.Show(“添加的条目”);//即使在出现错误时也会显示此信息&当没有数据添加到表中时
}
}
私有无效GISU加载(对象发送方、事件参数e)
{
maskedTextBoxDate.Text=DateTime.Today.ToString(“MM/dd/yyyy”);
txt85x11bw.Validating+=新的CancelEventHandler(txt85x11bw_Validating);
}
私有作废GISReactions\u FormClosing(对象发送方,FormClosingEventArgs e)
{
日期时间值;
//DateTime.TryParse将日期的srting表示形式转换为其等效的DateTime&返回一个值,该值指示转换是否成功。
如果(!DateTime.TryParse(maskedTextBoxDate.Text,out值))
{
maskedTextBoxDate.Text=DateTime.Today.ToSortDateString();
}
}
私有void txt85x11bw_验证(对象发送方,CancelEventArgs e)
{
if(string.IsNullOrEmpty(txt85x11bw.Text))
{
//无所事事
}
其他的
{
//初始化int类型的变量numberEntered
整数编码;
//int=integer TryParse=将字符串转换为整数(值,输出结果)的方法
if(int.TryParse(txt85x11bw.Text,输出编号))
{
//如果数字小于1或大于1000
如果(numberEntered<1 | | numberEntered>1000)
MessageBox.Show(“您必须输入有效数字”);
txt85x11bw.Clear();
}
//如果转换失败
其他的
{
MessageBox.Show(“您必须输入一个数字”);
txt85x11bw.Clear();
}
}
}
}

}

这一行添加了一个带有数据类型文本的参数,我假设,在数据库中,列85x11BW是数字类型(查看验证代码的int)

试着用这种方式换线

    myCommand.Parameters.AddWithValue("@85x11BW", Convert.ToInt32(txt85x11bw.Text));
通过这种方式,您可以强制AddWithValue方法添加一个参数,该参数的
DbType
属性设置为
Int32
。如果相对数据表列为
DateTime
类型,则参数
@Date
也可能存在同样的问题

编辑如果txt85x11BW为空,则需要决定在数据表中接受该列的默认值。如果接受NULL,则可以编写如下内容

        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);
int numberEntered=0;
int.TryParse(txt85x11bw.Text,输出编号);
如果(NumberRentered>=1&&NumberRentered 1000)
{
MessageBox.Show(“您必须输入有效数字”);
txt85x11bw.Clear();
}
}

要使显示“Entry Added”的messagebox即使在发生错误时也停止显示,请将messagebox.Show()函数移出finally子句,并移至try语句的末尾(在myCommand.ExecuteNonQuery之后),因为如果出现错误或成功,您的finally语句将始终执行。谢谢。这是我稍后将处理的另一个问题。此问题的数据库列是数字列,但当我尝试您的解决方案时,该行将突出显示为“FormatException未处理。输入字符串的格式不正确”。可能是是由txt85x11bw中的空白值引起的。是否接受此列的datatable中的null值?我接受null值,但我一直在使用一个数字对其进行测试,但仍然得到一个数据类型错误。我已在access中检查了我的表中的类型格式,并且它是整数类型。我想我将尝试的下一件事是将输入限制为仅数字He’你试过更新吗?对不起,我今天很忙,所以我不经常检查。
        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);
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();
    }
}