Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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<sqlparameter> paramList = new List<sqlparameter>()
{
new SqlParameter()
{

ParameterName = "@GUID",
Value = Request.QueryString["uid"]
}
};

return ExecuteSP("spIsPasswordResetLinkValid", paramList);
}
private bool ExecuteSP(string SPName, List<sqlparameter> 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<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("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
Posted

1 solution

Handling the error would be done in code.
For e.g.
C#
private bool ChangeUserPassword()
{
try
{
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);
}
catch (SqlException sqlEx)
{
  //Handle the SQL exception
}
catch (Exception ex)
{
  //Handle general exception
}
}


The next part would be how fix this exception.
Looks like you are trying to insert a string longer than the field length in the database.
This could be throwing the exception.
 
Share this answer
 
Comments
Muhammad0001 26-Jan-15 6:55am    
how this exception will be handle??
Muhammad0001 26-Jan-15 6:58am    
kindly solve this problem that is when i am changing the password the exception arise i that part private bool ExecuteSP(string SPName, List<sqlparameter> 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());
}
}

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