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 { }
}
}
ASKER
Hmm...tried that verbatim and still getting error. It says column RoomID cannot be null.
ASKER
My specific error:
ERROR [HY000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.30-comm unity]Colu mn 'RoomID' cannot be null
ERROR [HY000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.30-comm
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Man...tried that too and no dice.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you have to specify a parameter type.
Try something like this:
Open in new window