I have an sql command for when you add a new name to the database it counts to see how many of the name entered to the textbox exist in the database. If the count is 0 then it will add the name to the table. Else it displays an error message.
This works fine for inserting a new name but on my update page where you may update the name I have the same code which on button click counts to see how many exist. But if you leave the textbox the same value and click the button the count obviously results in 1 and brings up an error message.
Is there a way I can do a count but not including the name that is currently the value of the textbox?
protected void UpdateSharedArea(object sender, EventArgs e) { SqlConnection connection =new SqlConnection(docShare_ConString);//Count the amount of area names that are the same as typed by user SqlCommand existCheck =new SqlCommand("SELECT COUNT(doc_area_name) FROM document_area WHERE doc_area_name = @.doc_area_name", connection); SqlParameter areaname =new SqlParameter("@.doc_area_name", SqlDbType.VarChar); areaname.Value = AreaText.Text; existCheck.Parameters.Add(areaname); connection.Open();int count = (int)existCheck.ExecuteScalar(); connection.Close();//If the area name does not exist within the tableif (count == 0) {//Update nameCheers, Mark
You will need to to filter your count by the ID of the record you are updating. So when you load the name to display in your textbox, also load the ID of the record and keep it somewhere (Session state or viewstate for example). Then when you do your count, filter on the ID, e.g.
SELECT COUNT(*) FROM NameList WHERE Name = @.Name AND ID != @.Id
Brilliant, thanks very much for the reply.
Mark
No comments:
Post a Comment