This is my first time making a project about which I am a beginner
I have created a process by which I think I can validate user if user type is guest who is a half registered user.
This is my code for front end

here the user will submit the random code given tp him by mail after registration :

<table style="width: 500px;" id="verify">

<tr>
    <th>Enter Your Verification Code : </th>
    <td>
        <asp:TextBox ID="txtRandomCode" runat="server" Width="170px"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvRandomCode" runat="server" 
        ErrorMessage="This field must not be blank" ControlToValidate="txtRandomCode">
        </asp:RequiredFieldValidator></td>
</tr>

<tr>
    <td colspan="2" id="tdc">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
            CssClass="button_example" onclick="btnSubmit_Click"></asp:Button>
        </td>
</tr>

</table>

This is the code for Submit button :

protected void btnSubmit_Click(object sender, EventArgs e)
    {


        int randomCode=Convert.ToInt32(txtRandomCode.Text);

        Response.Redirect("~/BuyAlbum.aspx");
    }

The following code is for validating user :

public static User ValidateUser(string login, int rndCode)
    {
        string query;
        try
        {
            conn.Open();
            query = string.Format("SELECT Random_Code FROM Users where Name='{0}'", login);
            command.CommandText = query;
            string code = command.ExecuteScalar().ToString();
            int randomCode = Convert.ToInt32(code);
            if (randomCode == rndCode)
            {
                query = string.Format("UPDATE Users SET UserType='user' where Name='{0}'", login);
                command.CommandText = query;
                User user = null;
                user = new User(login, rndCode);
                return user;
            }
            else
            {   
                //code do not match
                return null;
            }

        }
        finally
        {

            conn.Close();
        }
    }

I have tried to copy the code for user login & tried to modified it so it can work to validate user identity.
But do no know how to implement this mechanism in the Verification page.

The actual code for user login is as follows :

public static User LoginUser(string login, string password)
    {//To check if user exists
        string query = string.Format("SELECT COUNT(*) FROM Users where Name='{0}'", login);
        command.CommandText = query;
        try
        {
            conn.Open();

            int amountoFUsers = (int) command.ExecuteScalar();
            if (amountoFUsers == 1)
            {
                query = string.Format("SELECT Password FROM Users where Name='{0}'", login);
                command.CommandText = query;
                string dbPassword = command.ExecuteScalar().ToString();
                if (dbPassword == password)
                {
                    query = string.Format("SELECT Email,UserType from Users where Name='{0}'", login);
                    command.CommandText = query;
                    SqlDataReader reader = command.ExecuteReader();
                    User user = null;
                    while (reader.Read())
                    {
                        string email = reader.GetString(0);
                        string type = reader.GetString(1);
                        user =new User(login,password,email,type);

                    }
                    return user;
                }
                else
                {   //Passwords do not match
                    return null;
                }
            }
            else
            {
                //User Exists
                return null;
            }

        }
        finally
        {

            conn.Close();
        }

    }

What should do to validate the user using the code

Recommended Answers

All 6 Replies

try to debug your codes.. to see what is the error of your codes..

I didn't get any errors
I want to know how to implement a mechanism for validating user by using a code from database by matching the code input by the user

Well I insist you try yourself so that you could learn.. I will give the logic.. I guess this random number is saved against your login details table in the database.. You need to check if the random number is present against that particular user in the database you have to redirect the user into the members' area.. Let me know if you have any problem regarding the same..

I have not added code for ValidateUser() in the verification.aspx.cs as I have not added it in like my login page.
Heh that was some sort silly mistake by me but finally the code works fine now with some little modifications

sorry but how did you solve the problem please show what change did you made for the code

I have done various addition deletion in my code
& now don't know where I have inserted the replacement code since still with what I have said after some few addition code worked properly.
I like to perform various ideas on my code to make it better & simpler but this time it became little difficult for me to understand so once I checked what updation I done to my code I will post it.

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.