格式异常错误 [英] Format Exception error

查看:73
本文介绍了格式异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(状态)下拉列表,有两个值可供选择:活动和非活动。



我的项目设计在不同的层次,因为很多如果项目的不同部分正在发生的事情。



这是Enum类&方法:



I have a (Status) dropdown that has 2 values to select from: Active and Inactive.

My project is designed in different layers and because of that many things are taking place if different parts of the project.

Here is the Enum class & method:

public class Enums
    {
        public enum ItemStatus
        {   Disabled = 1,
            Active = 0,
            Both = -1
        }




public static object GetIsDisabledByItemStatusValue(string itemStatusValue)
       {
if (itemStatusValue == "0")
            {
                return false;
            }
            else if (itemStatusValue == "1")
            {
                return true;
            }
            else
            {
                throw new Exception();
            }
          
       }



然后将在以下方法中使用ddlValue:


The ddlValue will then be used in the following method:

public static string GetYesNoValueByDDLValue(string ddlValue)
       {
           switch (ddlValue)
           {
               case "0":
                  return "0";
               case "1":
                   return "1";
               case "-1":
                   return "0,1";
           default:
                   return string.Empty;
           }
       }



最后,我使用以下代码将状态更改更新到数据库中:


Lastly I update that status change into a database using the following code:

public int UpdateConnectionType(int connectionTypeID, string lastUpdatedBy, string connectionTypeDesc, bool isDisabled )
        {
            var sqlStatement = new StringBuilder();

            sqlStatement.Append(" UPDATE dbo.ConnectionType");
            sqlStatement.Append("   SET");
            sqlStatement.Append(" LastUpdatedBy = @LastUpdatedBy, ");
            sqlStatement.Append(" ConnectionTypeDesc = @ConnectionTypeDesc,");
            sqlStatement.Append(" IsDisabled = @IsDisabled, ");
            sqlStatement.Append(" LastUpdatedDate = GetDate() ");
            sqlStatement.Append(" WHERE ");
            sqlStatement.Append("   ConnectionTypeID = @ConnectionTypeID");
            sqlStatement.Append(" SELECT @@Identity");

            SqlCommand sqlCommand = new SqlCommand(sqlStatement.ToString());
            var sqlParams = new List<SqlParameter>();

            sqlParams.Add(new SqlParameter() { ParameterName = "@ConnectionTypeID", SqlDbType = SqlDbType.Int, Value = connectionTypeID });
            sqlParams.Add(new SqlParameter(){ParameterName = "@IsDisabled", SqlDbType = SqlDbType.Bit, Value = isDisabled});
            sqlParams.Add(new SqlParameter() {ParameterName = "@LastUpdatedBy", SqlDbType = SqlDbType.VarChar, Size = 200, Value = lastUpdatedBy });
            sqlParams.Add(new SqlParameter() { ParameterName = "@ConnectionTypeDesc", SqlDbType = SqlDbType.VarChar, Size = 100, Value = connectionTypeDesc});

            sqlCommand.Parameters.AddRange(sqlParams.ToArray());



              DBAccess.SQLServer.GetInteger(DBAccess.SQLServer.GetConnectionString("AccountTrackerDB"), sqlCommand); // Format exception error is thrown here.
        }



随意提出问题,因为我可能很难说这个问题。本质上,我正在尝试在更新数据库时避免格式异常错误。谢谢。


Feel free to ask questions as I may have worded the question very poorly. Essentially I'm trying to avoid the format exception error when updating the database. Thanks.

推荐答案

只需使用如下的简单语句:

Just use a simple statement such as this:
bool val = ddlValue == "1"? false: true;





如果ddlValue等于1,则将val =设置为false,否则为true。或者只是改变它以适合您的需要。



If ddlValue equals "1" then set val = to false, otherwise true. Or just change it to fit what you need.


这篇关于格式异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆