<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:PadmaSMSConnectionString %>" 
                    SelectCommand="SELECT [EmailID], [Password],[CnPassword] FROM [Registration]  WHERE [EmailID] = @EmailID" 
                    UpdateCommand="UPDATE [Registration] SET [Password] = @password WHERE [EmailID] = @EmailID">
                    <SelectParameters>
                        <asp:SessionParameter Name="EmailID" SessionField="User" />
                    </SelectParameters>
                    <UpdateParameters>
                        <asp:ControlParameter ControlID="txtnewpass" Name="password" 
                            PropertyName="Text" Type="String" />
                        <asp:Parameter Name="EmailID" />
                    </UpdateParameters>
                </asp:SqlDataSource>



   protected void BtnChangePass_Click(object sender, EventArgs e)
    {
        try
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["PadmaSMSConnectionString"].ConnectionString);
            com = new SqlCommand("select * from Registration where EmailID=@id", con);
            com.Parameters.AddWithValue("@id", Session["User"].ToString());

            con.Open();
            dataread = com.ExecuteReader();
            dataread.Read();
            if (dataread.GetString(6) == this.Txtoldpass.Text)
            {
                this.SqlDataSource1.Update();
                this.Label1.Text = "Your Password has been changed!";

            }
            else
            {

                this.Label1.Text = "Please Enter Correct Old Password";

            }

            con.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }

I need help, please tell me the problem of this code. Thank u

Recommended Answers

All 4 Replies

First up, you haven't explained what the problem you are having is (that always helps for future reference). But in your BtnChangePass method you are only selecting from the database and then saying the password has been changed. Where are you doing the actual update of the database?

First up, you haven't explained what the problem you are having is (that always helps for future reference). But in your BtnChangePass method you are only selecting from the database and then saying the password has been changed. Where are you doing the actual update of the database?

The problem is that the code which I have posted does not work. The else part is getting executed each time. I am very new to programming itself so I need your guidance and help. Instead of selecting the data from database can i run update query directly?

Nope error

Say, for example, that you let your user reset their passwords directly on your site (no emails get sent out or anything like that). Then, assuming your user is already logged in, you need two things - their email address or user ID (whatever makes them unique in your database) and the new password.
Then you can use an update statement directly.

UPDATE table_name SET password = new_password WHERE user_id = @user_id

The @user_id being a passed in parameter. And that will reset their password is the simplest way possible.

commented: Thank you, it worked! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.