i think username in your table is primary key thats why exception came...
so do one thing ,efore actually inserting the data to table check whether the user exist with that name or not if exist intimate him ..
like this
private bool checkexistornot(string username)
{
con.Open();
cmd = new SqlCommand("select fname from LogIn where UserName="+username, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("UserName", txtuname.Text);
object obj=cmd.ExecuteScalar();
con.Close();
if(obj!=null)
return false;
else
return true;
}
protected void Button1_Click(object sender, EventArgs e)
{
if(checkexistornot())
{
con.Open();
cmd = new SqlCommand("insert into LogIn Values('" + txtfname.Text + "','" + txtlname.Text + "','" + txtuname.Text + "','" + txtpwd.Text + "','" + txtcpwd.Text + "')", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("FirstName", txtfname.Text);
cmd.Parameters.AddWithValue("LastName", txtlname.Text);
cmd.Parameters.AddWithValue("UserName", txtuname.Text);
cmd.Parameters.AddWithValue("PassWord", txtpwd.Text);
cmd.Parameters.AddWithValue("ConfirmPassWord", txtcpwd.Text);
cmd.ExecuteNonQuery();
da = new SqlDataAdapter("Select * from LogIn", con);
ds = new DataSet();
da.Fill(ds, "LogIn");
Response.Write("Registration Completed Successfully");
clear();
con.Close();
}
else
{
Response.Write("user with this already exist please select another name");
}
}