本文介绍了如何处理'System.Data.SqlClient.SqlException类型的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解决这个问题,即当我更改密码时,异常出现我的那部分

kindly solve this problem that is when i am changing the password the exception arise i that part

private bool ExecuteSP(string SPName, List SPParameters)
{
	string CS = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
	using (SqlConnection con = new SqlConnection(CS))
	{
		SqlCommand cmd = new SqlCommand(SPName, con);
		cmd.CommandType = CommandType.StoredProcedure;

		foreach (SqlParameter parameter in SPParameters)
		{
			cmd.Parameters.Add(parameter);
		}

		con.Open();
		return Convert.ToBoolean(cmd.ExecuteScalar());
	}
}



完整的鳕鱼在这里


full cod is here

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ChangePassword : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			if (!IsPasswordResetLinkValid())
			{
				lblMessage.ForeColor = System.Drawing.Color.Red;
				lblMessage.Text = "Password Reset link has expired or is invalid";
			}
		}
	}

	private bool IsPasswordResetLinkValid()
	{
		List paramList = new List()
		{
			new SqlParameter()
			{
				ParameterName = "@GUID",
				Value = Request.QueryString["uid"]
			}
		};
		return ExecuteSP("spIsPasswordResetLinkValid", paramList);
	}

	private bool ExecuteSP(string SPName, List SPParameters)
	{
		string CS = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		using (SqlConnection con = new SqlConnection(CS))
		{
			SqlCommand cmd = new SqlCommand(SPName, con);
			cmd.CommandType = CommandType.StoredProcedure;

			foreach (SqlParameter parameter in SPParameters)
			{
				cmd.Parameters.Add(parameter);
			}
			con.Open();
			return Convert.ToBoolean(cmd.ExecuteScalar());
		}
	}

	private bool ChangeUserPassword()
	{
		List paramList = new List()
		{
			new SqlParameter()
			{
				ParameterName = "@GUID",
				Value = Request.QueryString["uid"]
			},
			new SqlParameter()
			{
				ParameterName = "@Password",
				Value = FormsAuthentication.HashPasswordForStoringInConfigFile(txtNewPassword.Text, "SHA1")
			}
		};

		return ExecuteSP("spChangePassword", paramList);
	}

	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (ChangeUserPassword())
		{
			lblMessage.Text = "Password Changed Successfully!";
		}
		else
		{
			lblMessage.ForeColor = System.Drawing.Color.Red;
			lblMessage.Text = "Password Reset link has expired or is invalid";
		}
	}
}



存储程序更改使用的密码


Stored Procedure to change password that wass i using

Create Proc spChangePassword
@GUID uniqueidentifier,
@Password nvarchar(100)
as
Begin
	Declare @UserId int

	Select @UserId = UserId
	from tblResetPasswordRequests
	where Id= @GUID

	if(@UserId is null)
	Begin
		-- If UserId does not exist
		Select 0 as IsPasswordChanged
	End
	Else
	Begin
		-- If UserId exists, Update with new password
		Update tblUsers set
		[Password] = @Password
		where Id = @UserId

		-- Delete the password reset request row 
		Delete from tblResetPasswordRequests
		where Id = @GUID

		Select 1 as IsPasswordChanged
	End
	End

推荐答案


这篇关于如何处理'System.Data.SqlClient.SqlException类型的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 02:47