6

(int) faultsGroup is 0 or 1 but i always get this error: Column 'FaultGroup' cannot be null

Does anyone tell me why? Syntax looks ok.

MySqlCommand cmdAdd = new MySqlCommand("INSERT INTO Faults (" +
        "  FaultGroup, Text, Date, IP" +
        ") VALUES (" +
        "  @FaultGroup, @Text, @Date, @IP" +
        ")", conn);

MySqlParameter paramFaultGroup = new MySqlParameter("@FaultGroup", MySqlDbType.Int32);
FaultsGroup faultsGroup = (FaultsGroup) Enum.Parse(typeof (FaultsGroup), myFault.FaultGroup);
paramFaultGroup.Value = (int) faultsGroup;
cmdAdd.Parameters.Add(paramFaultGroup);

cmdAdd.ExecuteNonQuery();
1
  • 1
    You should accept answers for your questions by clicking the hollow checkmark next to an answer. Commented Feb 23, 2010 at 22:38

1 Answer 1

4

I haven't used MySql for about 6 months (thankfully migrated off to Sql Server) but, try changing your @ symbols for ?'s, as if memory serves, that's the correct convention with MySql, so:

MySqlCommand cmdAdd = new MySqlCommand(
       "INSERT INTO Faults (FaultGroup, Text, Date, IP)"
       + " VALUES (?FaultGroup, ?Text, ?Date, ?IP)",
       conn);

MySqlParameter paramFaultGroup = new MySqlParameter("?FaultGroup", MySqlDbType.Int32);
FaultsGroup faultsGroup = (FaultsGroup) Enum.Parse(typeof (FaultsGroup), myFault.FaultGroup);
paramFaultGroup.Value = (int) faultsGroup;
cmdAdd.Parameters.Add(paramFaultGroup);

cmdAdd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, the '@' symbol works fine, at least for the MySQL ADO.Net connector (which I assume the OP is using).
problem was in version of ado.net connector. i was using 5.1.7 now i am using v6. Thx all for help
how can i know about the version ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.