none
Inserting text in an Access DB Validation Rule problem RRS feed

  • Question

  •  

    Hi guys.

     

    I have an Access DB that has a Course table with a primery key CourseSignature which has a validation rule 

     Like "[A-Z][A-Z][A-Z][A-Z]###". For example CSCB343. When I try to enter a value in the DB with

     

    OleDbCommand insertCommand = new OleDbCommand();

    insertCommand.CommandText = "INSERT INTO Course(CourseSignature, CourseName, Credits) VALUES('" + signature + "', '" + name + "', " + credits + ")";

    insertCommand.Connection = conn.getOleConn();

    insertCommand.ExecuteNonQuery();

     

    it gives me an exception that the CourseSignuter is not in the format  Like "[A-Z][A-Z][A-Z][A-Z]###". Why!!?!

    Sunday, February 10, 2008 8:51 PM

Answers

  • Hi ADasfasdsafasdsad,

     

    I think the "[A-Z][A-Z][A-Z][A-Z]###" format is not match for the string “CSCB343”.

    And I want to give you a suggestion that remove the format "[A-Z][A-Z][A-Z][A-Z]###" in the database, and try to use the following code snippet to validate the CourseSignature.

    Code Snippet
    static void Main(string[] args)
            {
                string CourseSignature = "CSCB343";
                string regular_expression = "^[A-Z]{4}[\d]{3}$";
                bool ismatch = false;
                ismatch = System.Text.RegularExpressions.Regex.IsMatch(CourseSignature, regular_expression);
                if (ismatch)
                {
                    //do the insert db here
                }
            }

     

     

           
    Regards,
    Xun

    Tuesday, February 12, 2008 9:21 AM