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)
        {

        }
    }
}

use @user instead of @[User] ..

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.