Click here to Skip to main content
16,019,619 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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());
            }
        }
    }
}
Posted
Updated 28-Feb-19 8:05am
v2

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900