本文介绍了的ExecuteScalar引发的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这code抛出一个的NullReferenceException 时,它调用的ExecuteScalar

  selectedPassengerID = 0;//的SqlCommand命令= GenericDataAccess.CreateCommand();
//第二次测试
字符串的connectionString =;
康涅狄格州的SqlConnection;的connectionString = ConfigurationManager中。
    的ConnectionStrings [ConnST-MHM]的ConnectionString。
康恩=新的SqlConnection(的connectionString);
的SqlCommand命令=新的SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.Connection =康恩;
command.CommandText =SearchForPassenger;的SqlParameter参数;参数= command.CreateParameter();
param.ParameterName =@name;
param.Value = PNAME; //会议();
param.DbType = DbType.String;
command.Parameters.Add(参数);参数= command.CreateParameter();
param.ParameterName =@flightDate;
param.Value =日期;
param.DbType = DbType.String;
command.Parameters.Add(参数);参数= command.CreateParameter();
param.ParameterName =@ticketNo;
param.Value = ticketNumber;
param.DbType = DbType.Int32;
command.Parameters.Add(参数);INT项目;command.Connection.Open();
项目=(INT)command.ExecuteScalar();


解决方案

我已经封装在一个DAL我的大多数SQL逻辑。其中的一个方法,DAL拉使用以下逻辑标量整型。它可能为你工作:

 对象TEMP = cmnd.ExecuteScalar();
  如果((临时== NULL)||(临时== DBNull.Value))返回-1;
  回报(INT)温度;

我知道你已经进入了很多code以上,但我认为这真的是你的问题的实质。祝你好运!

This code throws a NullReferenceException when it calls ExecuteScalar:

selectedPassengerID = 0;

//SqlCommand command = GenericDataAccess.CreateCommand();


// 2nd test
string connectionString = "";
SqlConnection conn;

connectionString = ConfigurationManager.
    ConnectionStrings["ConnST-MHM"].ConnectionString;
conn = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure ;
command.Connection = conn;
command.CommandText = "SearchForPassenger";

SqlParameter param;

param = command.CreateParameter();
param.ParameterName = "@name";
param.Value = pName; // Session[""];
param.DbType = DbType.String;
command.Parameters.Add(param);

param = command.CreateParameter();
param.ParameterName = "@flightDate";
param.Value = date;
param.DbType = DbType.String;
command.Parameters.Add(param);

param = command.CreateParameter();
param.ParameterName = "@ticketNo";
param.Value = ticketNumber;
param.DbType = DbType.Int32;
command.Parameters.Add(param);

int item;

command.Connection.Open();
item = (int)command.ExecuteScalar();
解决方案

I have encapsulated most of my SQL logic in a DAL. One of these DAL methods pulls scalar Ints using the following logic. It may work for you:

  object temp = cmnd.ExecuteScalar();
  if ((temp == null) || (temp == DBNull.Value)) return -1;
  return (int)temp;

I know that you have entered a lot of code above but I think that this is really the essence of your problem. Good luck!

这篇关于的ExecuteScalar引发的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:11