protected void Button1_Click1(object sender, EventArgs e)
{
      string sql = "INSERT INTO user(CustomerName,Age,Sex,Email address,State,City,Country) VALUES (@Val1,@Val2,@val3,@val4,@val5,@val6,@val7,@val8,@val9,@val10)";
       try
        {
SqlConn.Open();
SqlCommand cmd = new SqlCommand(sql, SqlConn);
cmd.Parameters.AddWithValue("@Val1", txtcname.Text.ToString().Trim());
cmd.Parameters.AddWithValue("@Val2", txtAge.Text.ToString().Trim());
cmd.Parameters.AddWithValue("@Val3", txtsex.Text.ToString().Trim());
cmd.Parameters.AddWithValue("@Val4", txtEmail.Text.ToString().Trim());
cmd.Parameters.AddWithValue("@Val5", txtaddress.Text.ToString().Trim());
cmd.Parameters.AddWithValue("@val6", DrpState.SelectedItem.ToString().Trim());
cmd.Parameters.AddWithValue("@Val7", DrpCity.SelectedItem.ToString().Trim());
cmd.Parameters.AddWithValue("@Val7", DrpCountry.SelectedItem.ToString().Trim());
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);

    }
    finally
    {
        SqlConn.Close();


    }
}

this is my coding..
it is working fine ..
but problem is same customer register twice and it stored into table twice.. i need to solve this problem..

if user name already taken by another user i need to receive error message.(user name already exists pls provide another user name )
pls help me where can i change my coding..
Thanks in advance..

so a user can register twice as long as they choose a different username ?

Best thing to do to stop the same username being chosen more than once is to make the column UNIQUE in the definition of the table in the DB.

The alternative if to take the username the user wants and do a SELECT COUNT() something like this:

"SELECT COUNT(columnName) FROM TableName WHERE columnName = '" + requestedName + "'"; 

which will count the amount of times the requestedName is in the column of the DB stated in the COUNT() part of the statement.

Before that do the select query, to check if this customerName exits of not. If does not exist, do Insert, else show a message box of existance.
Will it go?
It should be like:

protected void Button1_Click1(object sender, EventArgs e)
{
      string sql2 = @"SELECT CustomerName FROM user WHERE CustomerName = @name";
      SqlConn.Open();
      SqlCommand cmd = new SqlCommand(sql2, SqlConn);
      try
      {
          object objName = cmd.ExecuteScalar();
          if(objName == null)
          {
                string sql = "INSERT INTO user(CustomerName,Age,Sex,Email address,State,City,Country) VALUES (@Val1,@Val2,@val3,@val4,@val5,@val6,@val7,@val8,@val9,@val10)";       
                cmd = new SqlCommand(sql, SqlConn);
                cmd.Parameters.AddWithValue("@Val1", txtcname.Text.ToString().Trim());
                cmd.Parameters.AddWithValue("@Val2", txtAge.Text.ToString().Trim());
                cmd.Parameters.AddWithValue("@Val3", txtsex.Text.ToString().Trim());
                cmd.Parameters.AddWithValue("@Val4", txtEmail.Text.ToString().Trim());
                cmd.Parameters.AddWithValue("@Val5", txtaddress.Text.ToString().Trim());
                cmd.Parameters.AddWithValue("@val6", DrpState.SelectedItem.ToString().Trim());
                cmd.Parameters.AddWithValue("@Val7", DrpCity.SelectedItem.ToString().Trim());
                cmd.Parameters.AddWithValue("@Val7", DrpCountry.SelectedItem.ToString().Trim());
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            else
                MessageBox.Show("User already exists!");
      }
    catch (System.Data.SqlClient.SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        SqlConn.Close();
    }
}
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.