Link to home
Start Free TrialLog in
Avatar of baijajusav
baijajusav

asked on

mysql C# error on insert statement. Says column has no value when in debug it shows a value.


I have the following code listed below. In the code I'm simply trying to insert a row into table message. When I step through in debug, all @ fields have values; however, when the  result = myCmd.ExecuteNonQuery(); is run, an exception is thrown saying that RoomID cannot be null. It shouldn't be null as I'm giving it a value by the OdbcParameter.

I am using the Mysql 5.1 ODBC driver through a system dsn to make the connection to the database. I can successfully retrieve data from this same table... I apparently cannot insert data though.

By the way, RoomId and all other id's in the table are BIGINT(20). The datatype the value is coming from in C# is a long.
public static readonly String insertMessage =
        @"insert into message
        (RoomID, UserID, ToUserID, Text, Color)
        values
        (@RoomID,@UserID,@ToUserID,@Text, @Color)";
 
public static void insertMessage(Message m)
    {        
        OdbcConnection MyConn = DBConnection.getDBConnection();
        int result = -1;
        try
        {
            MyConn.Open();
            OdbcCommand myCmd = new OdbcCommand(mySqlQueries.insertMessage);
            myCmd.Connection = MyConn;
            myCmd.CommandType = CommandType.Text;
            //(roomid, userid, touserid, text, color)
            OdbcParameter RoomID = new OdbcParameter("@RoomID", m.RoomId);
            OdbcParameter UserID = new OdbcParameter("@UserID", m.UserId);
            OdbcParameter ToUserID = new OdbcParameter("@ToUserID", m.ToUserId);
            OdbcParameter Text = new OdbcParameter("@Text", m.Text);
            OdbcParameter Color = new OdbcParameter("@Color", m.Color);
            myCmd.Parameters.Add(RoomID);
            myCmd.Parameters.Add(UserID);
            myCmd.Parameters.Add(ToUserID);
            myCmd.Parameters.Add(Text);
            myCmd.Parameters.Add(Color);
            
            result = myCmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
 
        }
        finally
        {
            try
            {
                if (MyConn != null) MyConn.Close();
            }
            finally { }
        }
    }

Open in new window

Avatar of zstapic
zstapic
Flag of Croatia image

Hello,

you have to specify a parameter type.

Try something like this:

OdbcParameter RoomID = new OdbcParameter("@RoomID",OdbcType.BigInt);
myCmd.Parameters.Add(RoomID).Value = m.RoomId;

Open in new window

Avatar of baijajusav
baijajusav

ASKER


Hmm...tried that verbatim and still getting error. It says column RoomID cannot be null.
My specific error:

ERROR [HY000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.30-community]Column 'RoomID' cannot be null
SOLUTION
Avatar of zstapic
zstapic
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Man...tried that too and no dice.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial