如何处理'System.Data.SqlClient.SqlException类型的异常? [英] how to handle An exception of type 'System.Data.SqlClient.SqlException?

查看:178
本文介绍了如何处理'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

推荐答案

这是不可能说的没有来自异常的更多信息但你的sp期望guid作为guid,你将它作为字符串传递。尝试使用Guid.Parse设置参数。



如上所述,我会明确设置它们的类型。



并且,我不会将密码存储为明文!
It's impossible to say without more information from the exception but your sp expects the guid as a guid, you are passing it as a string. Try doing a Guid.Parse where you set up the parameters.

And, as commented above, I'd explicitly set the type on them.

And, I wouldn't store the passwords as plaintext!


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

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