Skip to content

Front page in c#.net& login&registration page

zareef ahmed

Asked Dec 12, 2012, 3:07 AM EST1 answer
Share
LinkedInX
How to make simple front page in c#.net project& how to connet login & registration form with front page?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Rohatash Kumar
    0

    1. Registration Form

    Step-1

    Create Table

    Now create a table in a SQL Server database with username, password and email fields. The table looks like this.

    create table registrationtab

    (

    Username varchar(100),

    Email varchar(100),

    Password varchar(20)

    )

    Step-2


    Create stored procedure

    Now create a stored procedure for registration. That means the values will be inserted into the table using the stored procedure. The stored procedure looks like:

    create procedure [dbo].[storlogin134]

    (

    @username varchar(40),

    @email varchar(50),

    @password varchar(20)

    )

    as

    insert into registrationtab  values(@username,@email,@password )

    Step-3

    Create form for registration

    Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.



    Figure1

    Now double-click on the register me button and add following code.

    Java
    protected void Buttonregisterme_Click(object sender, EventArgs e)
            {
                string strcon ="Data Source=.;uid=sa;pwd=Password$2;database=master";
                SqlConnection con = new SqlConnection(strcon);
    
                SqlCommand com = new SqlCommand("storlogin134", con);
                com.CommandType = CommandType.StoredProcedure;
                SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);
                SqlParameter p2 = new SqlParameter("email", TextBoxemail.Text);
                SqlParameter p3 = new SqlParameter("password", TextBoxpassword.Text);
                com.Parameters.Add(p1);
                com.Parameters.Add(p2);
                com.Parameters.Add(p3);
                con.Open();
                com.ExecuteNonQuery();
                Labelinfo.Text = "registered successful.";
    
            }

     

    Now run the application and enter the username, email and password and then click on the register me button to save the values to the database.



     

    Figure 2

     

    Open the database and check the registrationtab table.

    2. Login form

    Step-4

    Create stored procedure

    Now create a stored procedure for login. That means the values will be selected from the table using a stored procedure. The stored procedure looks like:

    create PROCEDURE CheckUser

    (

    @username as varchar(50),

    @password as varchar(50)

    )

    AS

    SELECT * FROM registrationtab WHERE username=@username AND password=@password
    Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.

    login4.gif

    Figure3

    Step-5

    Create form for login

    Now create a form in ASP.Net with the username and password field which are defined in the table. The form looks like the following figure.

    Figure 4

    Now double-click on the login button and add the following code.

    protected void Buttonlogin_Click(object sender, EventArgs e)

            {

                string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";

                SqlConnection con = new SqlConnection(strcon);

     

                SqlCommand com = new SqlCommand("CheckUser", con);

                com.CommandType = CommandType.StoredProcedure;

                SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);

                SqlParameter p2 = new SqlParameter("password", TextBoxpassword.Text);

                com.Parameters.Add(p1);

                com.Parameters.Add(p2);

                con.Open();

                SqlDataReader rd = com.ExecuteReader();

                if (rd.HasRows)

                {

                    rd.Read();

                   Labelinfo.Text = "Login successful.";

                }

     

                else

                {

                    Labelinfo.Text = "Invalid username or password.";

     

                }

            }

    Now run the application and enter the username and password and then click on the login button to retrieve the values from the database. Now suppose we enter the wrong username and password, as in:


    login3.gif

     

    Figure4

     

    Now enter the correct username and password:


    login5.gif

     

    Figure 5

    0