指定的演员表无效错误消息 [英] specified cast is not valid error message

查看:95
本文介绍了指定的演员表无效错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我收到指定的强制转换无效错误.我不知道是什么原因造成的.我已经通读了其他主题,也没有运气来寻找解决方案.

Hi
I''m getting a specified cast not valid error. I have no idea what''s causing it. I''ve read through other threads and have had no luck in finding a solution.

public Quote GetMaxQuoteNo()
 {
  Quote objex = new Quote();
  DataAccessLayer.DAL objdal = new DataAccessLayer.DAL();
  using(System.Data.IDataReader dr = objdal.executespreturndr("GetMaxQuote"))
     {
       while(dr.Read())
        {
          populatereader(dr, objex);
        }
      }
     return objex;
  }

private void populatereader(System.Data.IDataReader dr, Quote objex)
 {
   objex.QouteNo = dr.GetInt16(0); // the error occurs here 
   throw new NotImplementedException();
 }



上面是用于从数据库中的maxqouteno中检索的类方法



The above is the class methods used to retrieve from the maxqouteno from the database

Create Procedure GetMaxQuote
As
Begin
Select Max(QuoteNo) from Quote
End



这是使用的存储过程:

预先谢谢您



Here''s the stored procedure used:

Thank you in advance

推荐答案

如果您查看MSDN上GetInt16的定义:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint16.aspx [ ^ ]明确指出该参数必须是Int32.

在不知道数据库中QuoteNo的类型的情况下,无法确定存储过程的返回值是什么,而是尝试进行强制转换:
If you look at the definition of GetInt16 on MSDN: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getint16.aspx[^] it clearly states that the parameter must be an Int32.

Without knowing the type of QuoteNo in your DB it is not possible to be sure what the return value of the stored procedure is, but try a cast instead:
objex.QouteNo = (int16) dr[0];


尝试:
objex.QouteNo = dr.GetDecimal(0); 


并确保您的QouteNo具有匹配的数据类型(十进制).在数据库和代码之间具有不同的数据类型不是一个好主意.


And make sure your QouteNo has a matching datatype (decimal) It''s not a good isea to have different datatypes between database and code.


这篇关于指定的演员表无效错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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