无法将类型为“system.data.sqlclient.sqlcommand”的对象强制转换为“system.iconvertible” [英] Unable to cast object of type 'system.data.sqlclient.sqlcommand' to type 'system.iconvertible'

查看:272
本文介绍了无法将类型为“system.data.sqlclient.sqlcommand”的对象强制转换为“system.iconvertible”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这个错误。请帮我解决。



I'm getting this error. Please help me to solve it.

Unable to cast object of type 'System.Data.SqlClient.SqlDataReader' to type 'System.IConvertible'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Data.SqlClient.SqlDataReader' to type 'System.IConvertible'.

Source Error: 


Line 76:                     cmd.Parameters.Add(parameter);
Line 77:                 }
Line 78:                 return Convert.ToBoolean(cmd.ExecuteReader());
Line 79:             }
Line 80:         }





我尝试过:





What I have tried:

<pre>using Authentication_Model.DAL;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Authentication_Model.Accounts
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if(!IsPasswordResetLinkValid())
                {
                    Response.Write("<script>alert('Password reset link has expired or is invalid')</script>");
                }
            }
        }

        protected void btnResetChange_Click(object sender, EventArgs e)
        {
            if(ChangeUserPassword())
            {
                Response.Write("<script>alert('Password changed successfully')</script>");
            }
            else
            {
                Response.Write("<script>alert('Password reset link has expired or is invalid')</script>");
            }
        }
        public bool ChangeUserPassword()
        {
            List<SqlParameter> paramList = new List<SqlParameter>()
            {
                new SqlParameter()
                {
                    ParameterName = "@GUID",
                    Value = Request.QueryString["uid"]
                },
                new SqlParameter()
                {
                    ParameterName = "@Password",
                    Value = FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Text, "SHA1")
                }
            };
            return ExecuteSP("sp_ChangePassword", paramList);
        }
        public bool IsPasswordResetLinkValid()
        {
            List<SqlParameter> paramList = new List<SqlParameter>()
            {
                new SqlParameter()
                {
                    ParameterName = "@GUID",
                    Value = Request.QueryString["uid"]
                }
            };
            return ExecuteSP("sp_IsPasswordResetLinkValid", paramList);
        }
        public bool ExecuteSP(string SP_Name, List<SqlParameter> SP_Parameters)
        {
            using (cmd = new SqlCommand())
            {
                cmd.Connection = Connection.GetConnection();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "sp_IsPasswordResetLinkValid";
                foreach (SqlParameter parameter in SP_Parameters)
                {
                    cmd.Parameters.Add(parameter);
                }
                return Convert.ToBoolean(cmd.ExecuteReader());
            }
        }
    }
}

推荐答案

为什么你会认为< a href =https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executereader?view=netframework-4.7.2> SqlCommand.ExecuteReader [< a href =https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executereader?view=netframework-4.7.2\"target =_ blanktitle =新窗口> ^ ]会以任何方式,形状或形式返回任何类似bool值的东西吗?

它会返回 SqlDataReader [ ^ ]可以用来浏览查询结果,而不是真/假结果。
Why would you assume that SqlCommand.ExecuteReader[^] would return anything that resembled a bool value in any way, shape or form?
It returns an SqlDataReader[^] that can be used to browse through the query results, not a "true / false" result.


这篇关于无法将类型为“system.data.sqlclient.sqlcommand”的对象强制转换为“system.iconvertible”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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