简体   繁体   English

数据类型不匹配条件表达式错误

[英]data type mismatch in criteria expression error

con.Close();

cmd = new OleDbCommand(@"INSERT INTO tbl_allTransactions VALUES 
     (@transDate, @transType, @transDesc, @transAmount, @transCategory, 
      @transFreq, @transPayVia, @transPaid, @transToPay, @transNextPay, 
      @transStatus, @transMonth, @transWeek, @transYear, @c_name ,@transMode)", con);

cmd.Parameters.AddWithValue("@transDate", firstPay.ToShortDateString());
cmd.Parameters.AddWithValue("@transType", cbtransType.Text);
cmd.Parameters.AddWithValue("@transDesc", txtDesc.Text);
cmd.Parameters.AddWithValue("@transAmount", txtMoney.Text);
cmd.Parameters.AddWithValue("@transCategory", cbCategory.Text);
cmd.Parameters.AddWithValue("@transFreq", cbFreq.Text);
cmd.Parameters.AddWithValue("@transPayVia", cbPay.Text);
cmd.Parameters.AddWithValue("@transPaid", i);
cmd.Parameters.AddWithValue("@transToPay", txtpayments.Text);

if (i <= (totPayments - 1))
{
    cmd.Parameters.AddWithValue("@transNextPay", nextPay.ToShortDateString());
}

if (i == totPayments)
{
    cmd.Parameters.AddWithValue("@transNextPay", firstPay.ToShortDateString());
}            

if (i <= (totPayments - 1))
{
    transStatus = "Uncleared";
    cmd.Parameters.AddWithValue("@transStatus", transStatus);
}

if (i == totPayments)
{
    transStatus = "Cleared";
    cmd.Parameters.AddWithValue("@transStatus", transStatus);
}   

cmd.Parameters.AddWithValue("@transMonth", whichMonth);
cmd.Parameters.AddWithValue("@transWeek", whichWeek);
cmd.Parameters.AddWithValue("@transYear", whichYear);
cmd.Parameters.AddWithValue("@c_name", cName);
transMode = cbDuration.Text;
cmd.Parameters.AddWithValue("@transMode", transMode);
con.Open();
int j = Convert.ToInt32(cmd.ExecuteNonQuery());

if (j == 1)
{
    j = 0;
    //  nextPay = firstPay = DateTime.Now;
}
con.Close();

Using AddWithValue means that the datatype for the parameter value is determined by the value itself. 使用AddWithValue意味着参数值的数据类型由值本身确定。 So, for example, the datatype of the parameter @transDate is a string, not a date because you use the ToShortDateString() conversion. 因此,例如,参数@transDate的数据类型是字符串,而不是日期,因为您使用ToShortDateString()转换。 Of course this requires that the field updated by the parameter is a string not a date. 当然,这需要由参数更新的字段是字符串而不是日期。

You need to check, if every parameter goes to update a compatible field type, Another example could be the parameter @transAmount . 您需要检查每个参数是否都将更新兼容的字段类型,另一个示例可能是参数@transAmount Is the database field a string? 数据库字段是字符串吗? You pass the Text property of a textbox, so it should be a string. 您传递文本框的Text属性,因此它应该是一个字符串。

There is another potential problem. 还有另一个潜在的问题。 You should be sure that the parameters @transNextDay and @transStatus are always added to the collection. 您应该确保将参数@transNextDay@transStatus始终添加到集合中。 The current code present the possibility that these parameters are not added (i > totPayments) 当前代码表示不添加这些参数的可能性(i > totPayments)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM