Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:

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

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

The ddlValue will then be used in the following method:
C#
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:
C#
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.
Posted
Updated 8-Jul-15 11:20am
v3
Comments
[no name] 8-Jul-15 16:27pm    
Convert.ToBoolean will only work for strings that are "true" and "false". It won't work for strings that are 1 or 0.
Member 11820531 8-Jul-15 16:29pm    
That would explain why i'm getting an error. Thanks.

1 solution

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


If ddlValue equals "1" then set val = to false, otherwise true. Or just change it to fit what you need.
 
Share this answer
 
Comments
Member 11820531 8-Jul-15 17:19pm    
That may have work but I found a work around a small time before that. My changes are visible in the revised code in my original post. I'm now focusing on fixing a Format Exception Error. Thanks for your feedback!
ZurdoDev 8-Jul-15 19:14pm    
Format exception is going to be a similar issue. You need to make sure data is parsed correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900