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