Users Pricing

blog

home / developersection / blogs / retriving user password by using stored procedure in asp.net

Retriving User Password by Using Stored Procedure in ASP.NET

Anonymous User 25348 15 Feb 2011 Updated 18 Sep 2014

In this demonstration I will tell you how to retrieve user password by using Stored Procedure in ASP.NET. To perform this task I had created a UserLogin table in Sql Server and enter some values like following example demonstrate…

create table UserLogin 
(
      userName varchar(20) not null,
      userPassword varchar(20) not null,
      primary key(userName , userPassword)
)  
--Enter some values in UserLogin tables --
insert into UserLogin values('aaaa','aaaa@123')
insert into UserLogin values('bbbb','bbbb@123')
insert into UserLogin values('cccc','cccc@123')
insert into UserLogin values('dddd','dddd@123')


Create a parametrized procedure which return user password on the
basis of user name.

--Create an parametrize procedure to retrive user password from table based on username--

create procedure retriveUserPassword @userName varchar(20) 
as
begin
      select userPassword from UserLogin where userName=@userName
end

Then create a function in c# which display user Password in form alert box.

//For retriving password from ql server using procedure you have to use  
    //Two namespace named System.Data and System.Data.SqlClient.
    //At the click event of button write down following line of code.
 
    public void getUserPassword(string userName)
    {
        SqlConnection con = new SqlConnection();    //Create an SqlConnection object.
        //Pass the connection string to the SqlConnection object
        con.ConnectionString = "............";
        con.Open();  //Open the connection
        string procedureText = "retriveUserPassword";
        SqlCommand cmd = new SqlCommand(procedureText, con); //Create an SqlCommand object.
        cmd.CommandType = CommandType.StoredProcedure; //Change the behaviour of command text to stored procedure.
        cmd.Parameters.Add(new SqlParameter("@userName",userName));  //Pass value to the parameter that you have created in procedure.
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Response.Write("<script>alert('Password is  :" + dr[0].ToString() + "')</script>");
        }
        else
        {
            Response.Write("<script>alert('User Name does not exist')</script>");
        }
        con.Close();
    }

Then finally call this method at the click event of RecoverPassword button and you get password in form alert() message box.


I am a content writter !


7 Comments