Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
kindly solve this problem that is when i am changing the password the exception arise i that part
C#
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
C#
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
SQL
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
Posted
Updated 26-Jan-15 8:13am
v2
Comments
PIEBALDconsult 26-Jan-15 11:23am    
That depends on the cause of the Exception and where it happens. Please use Improve question to add context and detail.
ZurdoDev 26-Jan-15 12:45pm    
What is the exact error?
Rob Philpott 26-Jan-15 16:45pm    
Yes, more information about the exception would be helpful! It *might* be that you are not specifying the type of your SqlParameters. May not be necessary, I always do this so don't know what happens if you don't.
John C Rayan 26-Jan-15 16:55pm    
I suspect the parameter @GUID. Make sure that you are passing GUID as expected. Alternative way is to declare @GUID parameter as string and then CONVERT() it to uniqueidentifier.

Turn on your SQL Profiler and check if your @GUID parameter is indeed a GUID.

1 solution

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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