0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


namespace Practical1
{
    public partial class Exercise1 : System.Web.UI.Page
    {
        string cs = Global.CS;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string user = txtUser.Text;
                string Fname = txtFName.Text;
                string Lname = txtLName.Text;
                string Ic = txtICNumber.Text;
                string Phone = txtPhone.Text;
                string Gender = rblGender.Text;
                string State = ddlState.Text;
                string Address = txtAddress.Text;
                string Email = txtEmail.Text;

                string sql = @"INSERT INTO Customer ([User],Fname,Lname,Ic,Phone,Gender,State,Address,Email) 
                        VALUES (@[User],@Fname,@Lname,@Ic,@Phone,@Gender,@State,@Address,@Email)";

                SqlConnection con = new SqlConnection(cs);
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@[User]", user);
                cmd.Parameters.AddWithValue("@Fname", Fname);
                cmd.Parameters.AddWithValue("@Lname", Lname);
                cmd.Parameters.AddWithValue("@Ic", Ic);
                cmd.Parameters.AddWithValue("@Phone", Phone);
                cmd.Parameters.AddWithValue("@Gender", Gender);
                cmd.Parameters.AddWithValue("@State", State);
                cmd.Parameters.AddWithValue("@Address", Address);
                cmd.Parameters.AddWithValue("@Email", Email);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();



            }


        }
        protected void btnReset_Click(object sender, EventArgs e)
        {

            Server.Transfer("Register.aspx");

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string user = args.Value;

            string sql = "SELECT COUNT (*) FROM Customer WHERE [User] = @[User]";


            SqlConnection con = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@[User]", user);

            con.Open();
            int count =(int)cmd.ExecuteScalar(); // error here!!
            con.Close();

            if (count > 0)
            {
                args.IsValid = false;
            }

        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {

        }
    }
}

Edited by Anson.tay3

3
Contributors
2
Replies
12
Views
3 Years
Discussion Span
Last Post by Ranjitrathod15
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.