Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace projectDressDesign
{
    public partial class registration : System.Web.UI.Page
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void registerBtn_Click1(object sender, EventArgs e)
        
            {
                Page.Validate();
                if (Page.IsValid)
                {
                    myConnection.Open();

                    string userInvalid = "The username entered is invalid, please choose another.";

                    string checkDatabase = "SELECT * FROM Users WHERE Email_Id = @em AND Password = @pas";
                    SqlCommand command = new SqlCommand(checkDatabase, myConnection);
                    command.Parameters.AddWithValue("@em", email.Text);
                    command.Parameters.AddWithValue("@pas", password.Text);
                    command.ExecuteNonQuery();
                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.HasRows)
                    {
                        outputlabel.Text = userInvalid;
                        myConnection.Close();
                    }
                    
                
                else
                {
                   
                    outputlabel.Text = "Succesfully Registered";

                    string query = "Insert into Users (FirstName,LastName,Gender,DateOfBirth,Email_Id,Password) Values (@fn,@ln,@gen,@dob,@em,@pas)";
                    myConnection.Open();
                    SqlCommand insertCommand = new SqlCommand(query, myConnection);
                    insertCommand.Parameters.AddWithValue("@fn", fname.Text);
                    insertCommand.Parameters.AddWithValue("@ln", lname.Text);
                    insertCommand.Parameters.AddWithValue("@gen", gender_dd.SelectedItem.Text);
                    insertCommand.Parameters.AddWithValue("@dob", date.Text);
                    insertCommand.Parameters.AddWithValue("@em", email.Text);
                    insertCommand.Parameters.AddWithValue("@pas", password.Text);

                   

                    insertCommand.ExecuteNonQuery();

                    myConnection.Close();
                    Response.Redirect("login.aspx");
                }

                    
            }

        }
        }
    }
Posted
Updated 29-Aug-15 9:47am
v2
Comments
Michael_Davies 29-Aug-15 16:41pm    
Why execute a nonquery then immediately execute a reader? Waste of time. Also trying to follow your logic if the reader HasRows then the "user" is valid.

To aid people in helping you run the code in the debugger and see where it stops and then tell us which line.
F-ES Sitecore 29-Aug-15 16:42pm    
On what line?
Patrice T 29-Aug-15 17:49pm    
No repost please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900