Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm currently trying to debug the return statement for my last line of code. I'm currently receiving a System.FormatExceptionError thrown there. I've been told that the return statement, as is currently, is attempting to accomplish 3 things. In order to know where the error is being thrown, I believe I need to execute those 3 things in separate return statements. Here's the 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());

            //int result = DBAccess.SQLServer.GetInteger(DBAccess.SQLServer.GetConnectionString("AccountTracker"), sqlCommand);
            return DBAccess.SQLServer.GetInteger(DBAccess.SQLServer.GetConnectionString("AccountTrackerDB"), sqlCommand);
        }

My questions are:
1) What 3 things is my return statement trying to accomplish? (I believe it's attempting to get the ID, and then update the corresponding record within the database)
2)How would I write those 3 tasks into their own return statement? (I've taken one stab at it with the code that commented out) Thanks!
Posted
Comments
[no name] 9-Jul-15 10:24am    
Why are you using a StringBuilder? Just use a string. Use AddWithValue instead of your Adds
And your connection string is probably the culprit.

1 solution

I cannot see three but rather two actions that could be separated from the return statement:
C#
string connectionString = DBAccess.SQLServer.GetConnectionString("AccountTrackerDB");
int result = DBAccess.SQLServer.GetInteger(connectionString, sqlCommand);
return result;

This way you will have the ability to check the content of the variables before they are used and returned.
 
Share this answer
 
Comments
Member 11820531 9-Jul-15 10:38am    
Thanks that works for debugging purposes. I know for sure the connection string is working fine and result is being set = to 0. However, I'm still getting the exception thrown and my IsDisabled field is not being updated. Thanks for the code though it helped confirm that some things are acting as I want.

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