插入数据库时​​出现Null Reference Exception [英] Null Reference Exception when inserting into a database

查看:85
本文介绍了插入数据库时​​出现Null Reference Exception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个C#项目,它将数据从网页插入到连接的数据库中。我有一个数据访问层(DAL),处理所有插入和更新SQL功能。



我多次调试了我的代码,并没有看到任何值被设置为null的位置。正在使用正确的数据填充两个字段(ConnectionTypeDesc和CreatedBy)。最后一个字段(创建日期)使用GetDate()在SQL中处理。

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

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.

推荐答案

首先,这一行:

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



中包含错误的参数名称。它应该是@ConnectionTypeDesc。



其次,这种类型的错误很容易调试,但是你很难这样做,因为你没有放什么变成了变数。您将3个语句混合到一行代码中。你真的看不到该行的哪一部分抛出异常。在你的 return 语句中,你有3个单独的事情发生。



使你的代码更容易debug,将对GetConnectionString()的调用结果放入变量中。将调用结果放入另一个变量GetInteger()然后返回此变量。



这就是你如何找出该行代码的哪一部分抛出异常,它会告诉你需要查看的位置。


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.


这篇关于插入数据库时​​出现Null Reference Exception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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