protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); string query = "select Username from [Login] where Username = '" + ((TextBox)DetailsView1.FindControl("TextBox1")).Text + "'"; SqlCommand cmd = new SqlCommand(query, con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); if (dr.HasRows == true) { Response.Write("<script>alert('Username is Already Exists')</script>"); } else { Response.Write("<script>alert('SuccessFully Saved')</script>"); } con.Close(); }