Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I'm currently working on a C# project that will insert data from a webpage into a connected database. I have a data access layer(DAL) that handles all of the insert and update SQL functionality.

I've debugged my code multiple times and don't see where any values is being set to null. Two fields (ConnectionTypeDesc and CreatedBy) are being populated with correct data. The last field (Created Date) is handled in SQL using the GetDate().
C#
public int InsertConnectionType(string connectionTypeDesc, string createdBy)
       {
           var sqlStatement = new StringBuilder();

           sqlStatement.Append(" INSERT INTO dbo.ConnectionType");
           sqlStatement.Append("   (ConnectionTypeDesc, CreatedBy, CreatedDate) ");
           sqlStatement.Append(" VALUES");
           sqlStatement.Append("   (@ConnectionTypeDesc, @CreatedBy, GetDate());");
           sqlStatement.Append(" SELECT @@Identity");

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


           sqlParams.Add(new SqlParameter() { ParameterName = "ConnectionTypeDesc", SqlDbType = SqlDbType.VarChar, Size = 100, Value = connectionTypeDesc });

           sqlParams.Add(new SqlParameter() { ParameterName = "@CreatedBy", SqlDbType = SqlDbType.VarChar, Size = 200, Value = createdBy });

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

           return DBAccess.SQLServer.GetInteger(DBAccess.SQLServer.GetConnectionString("ConnectionTypeID"), sqlCommand);//Exception Thrown Here
       }

Any suggestions on how to tackle this issue would be greatly appreciated. I've marked the code and line above where the exception is thrown.
Posted
Comments
Afzaal Ahmad Zeeshan 7-Jul-15 16:21pm    
I think your configuration doesn't include a connection string named ConnectionTypeID. Can you debug your application?
Member 11820531 7-Jul-15 18:25pm    
You were correct, my connection string was being implemented incorrectly. Thank you, I've now fixed my error.
Afzaal Ahmad Zeeshan 7-Jul-15 18:40pm    
I'm glad I helped you out.
[no name] 7-Jul-15 16:23pm    
Debug it. Your attempt to get your connection string is probably failing.
Member 11820531 7-Jul-15 17:45pm    
I have debugged it and received the error posted in the title. If it is a problem with my GetConnectionString function, is there something specific that you would recommend? Thanks for the feedback.

1 solution

First, this line:
sqlParams.Add(new SqlParameter() { ParameterName = "ConnectionTypeDesc", SqlDbType = SqlDbType.VarChar, Size = 100, Value = connectionTypeDesc });

has the wrong parameter name in it. It should be "@ConnectionTypeDesc".

Second, this type of error is easy to debug but you've made it difficult to do so because you're not putting anything into variables. You're mashing 3 statements into a single line of code. You really can't see which part of the line is throwing the exception. In your return statement, you've got 3 separate things going on.

To make your code easier to debug, put the result of the call to GetConnectionString() into a variable. Put the result of the call to GetInteger() into another variable then return this variable.

This is how you find out which part of that line of code is throwing the exception and it'll tell you where you need to look.
 
Share this answer
 
Comments
Member 11820531 8-Jul-15 12:17pm    
Do you mind providing a brief snippet of how I could put GetInteger() into a variable, for debugging purposes?
Dave Kreskowiak 8-Jul-15 12:29pm    
You don't. You put the result of the call to GetInteger into a variable.

int result = DBAccess.SQLServer.GetInteger(...);

return result;

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