System.Data.OleDb.OleDbException:'条件表达式中的数据类型不匹配。在C#中

时间:2018-11-21 11:11:17

标签: c#

我在TXT_Firm_Pincode.TextTXT_Firm_Bank_Ac_No.Text中提供数字数据时,在插入和更新时遇到错误,当我离开这两个字段时,它们的完美表现可以帮助任何人。

MC.Cmd_SQL = new OleDbCommand($@"Update TBL_Firm set 
    Firm_Owner = '{TXT_Firm_Owner.Text}', 
    Firm_Name = '{TXT_Firm_Name.Text}', 
    Firm_Address = '{TXT_Firm_Address.Text}', 
    Firm_City = '{TXT_Firm_City.Text}', 
    Firm_Pincode = @Firm_Pincode, 
    Firm_State= '{CB_Firm_State.Text}', 
    Firm_Con_Person = '{TXT_Firm_Con_Person.Text}', 
    Firm_Con_No = {TXT_Firm_Con_No.Text}, 
    Firm_GSTN = '{TXT_Firm_GSTN.Text}', 
    Firm_Bank_Name = '{TXT_Firm_Bank_Name.Text}', 
    Firm_Bank_Ac_No = @Firm_Bank_Ac_No,
    Firm_Bank_Branch = '{TXT_Firm_Bank_Branch.Text}',
    Firm_Bank_IFSC= '{TXT_Firm_Bank_IFSC.Text}',
    Firm_MailID = '{TXT_Firm_MailID.Text}' 
    where Firm_ID = {TXT_Firm_ID.Text}", MC.DB_Connection);

MC.Cmd_SQL.CommandType = CommandType.Text;

MC.Cmd_SQL.Parameters.AddWithValue("@Firm_Pincode" , TXT_Firm_Pincode.Text == "" ? DBNull.Value : (object)"Null");

MC.Cmd_SQL.Parameters.AddWithValue("@Firm_Bank_Ac_No", TXT_Firm_Bank_Ac_No.Text == "" ? DBNull.Value : (object)"Null"); 

MC.Cmd_SQL.ExecuteNonQuery();

1 个答案:

答案 0 :(得分:0)

condition ? operand1 : operand2为真和假时,三元运算符operand1返回condition-否则。

您的operand1语句可以,但是您需要更改operand2以便在其中添加文本框值:

MC.Cmd_SQL.Parameters.AddWithValue("@Firm_Pincode" , 
  TXT_Firm_Pincode.Text == "" ? DBNull.Value : TXT_Firm_Pincode.Text);

MC.Cmd_SQL.Parameters.AddWithValue("@Firm_Bank_Ac_No", 
  TXT_Firm_Bank_Ac_No.Text == "" ? DBNull.Value : TXT_Firm_Bank_Ac_No.Text);