Saturday, July 30, 2011

Check for duplicate when inserting a recod ( formView)

Ex.
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {

        TextBox Email = (TextBox)FormView1.FindControl("TextBoxEmail");
        string ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;"
                            + "Data Source=D:\\Database.mdb";
        OleDbConnection oConn = new OleDbConnection(ConnectionString);
        string strSQL = "Select Count(Email) as TheCount from Users where Email =? ";
        OleDbCommand cmd = new OleDbCommand(strSQL, oConn);
        cmd.Parameters.AddWithValue("",  Email.Text);
        int Result = (int)cmd.ExecuteScalar();
        if(Result > 0)
        {
             e.Cancel = true;
             Label Duplicate = (Label)FormView1.FindControl("lblDuplicate");
             Duplicate.Text = "That email address is already in use";
        }

    } 
reference: http://forums.asp.net/t/1116970.aspx

No comments:

Post a Comment