i found some codes on other forums concerning the cancellation of insert in formview if the record already exists. but its giving me an error stating that there is an incorrect sysntax near =?.... can you please tell me what's wrong with this syntax..i will post below the codes.

TextBox Client = (TextBox)FormView1.FindControl("ClientNameTextBox0");
        string ConnectionString = "Data Source=DDDWEB\\SQLEXPRESS;Initial Catalog=Database;Integrated Security=True";
        SqlConnection oConn = new SqlConnection(ConnectionString);
        string strSQL = "Select Count(Client) as TheCount FROM Client WHERE ClientName =? ";
        SqlCommand cmd = new SqlCommand(strSQL, oConn);
        cmd.Parameters.AddWithValue("", Client.Text);
        oConn.Open();
        int Result = (int)cmd.ExecuteScalar();
        if (Result > 0)
        {
            e.Cancel = true;
            Label Duplicate = (Label)FormView1.FindControl("lblDuplicate");
            Duplicate.Text = "There is already an existing record.";
        }
        oConn.Close();

im using sqldata source and if you have any idea on how to cancel insertion if the record already exist please feel free to write your post here..thanks in advance..i badly nedd your help..

there is a very easy solution to this problem. just modify the data type of the field that you dont want to duplicate as unique(no duplicates) in sql server. And wrap your data access code between try catch blocks. if the record duplicates the database will throw an exception and you can catch that exception and inform the user that the record should be unique.

Not sure about that serk. It'll work, but it's expensive. Might it be cheaper to have a stored procedure check for the record first, then insert if one is not found?

Not sure about that serk. It'll work, but it's expensive. Might it be cheaper to have a stored procedure check for the record first, then insert if one is not found?

Appearantly, performance is not a neccessity for him as his application is small. For these occations, open the shortest path first logic is always the best.

This article has been dead for over six months. Start a new discussion instead.