Perfect solution SqlDataReader specified conversion invalid

Source: Internet
Author: User
This article mainly for you to introduce the SqlDataReader specified conversion invalid solution, with a certain reference value, interested in small partners can refer to

SqlDataReader Specifies the resolution that the conversion is invalid, as follows


Gets the latest display order data for      string str = string. Format (@ "If exists (select Showorder from gis_funcdefaultlayer where gisfuncid = {0})                           select Max (showorder) as Showord Er from gis_funcdefaultlayer where gisfuncid ={0}                           Else select ' 0 ' as Showorder ", gisfuncid);       IDataReader DataReader = helper. ExecuteReader (CommandType.Text, str);      if (Datareader.read ())//Determine if the current function ID has data      {        //Read Assignment        try        {          Showorder = datareader.getint32 (0);                  }        catch (Exception ex)        {          Httpresponsemessage result = new Httpresponsemessage {Content = new stringcontent (ex. Message)};          return result;        }              } Datareader.close ();//Close

SqlDataReader GetInt32 (and other methods such as gerstring, etc.)

It just gets the columns of the corresponding data type in the database and does not have the function of type conversion, so it cannot be used

There are two ways of solving

1. If you need to return an int type, then the field in the database is defined as type int, then it is possible to use GetInt32
2. If the database does not define an int type and you want to return an int type, then use the corresponding type get out of the database and convert
such as Int. Parse (Selectunitidread. GetString (0))
If you do not need to return an int type, as you wrote above, the last is converted to string
And the type in the database is the varchar that corresponds to the string, so you can assign the following value directly
such as: Rmoutbackinfo. Unitid = Selectunitidread. GetString (0)
Instead of converting to string


Solve Showorder=int. Parse (datareader.getstring (0));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.