Search code examples
c#mysqlsql-updatequery-string

Input string was not in a correct format error in MySQL query


Hi I am working on an application which works with MySQL database and C# 4.0.

I have below method to update a MySQL table. But while executing the below method I am getting error that Input string was not in a correct format.

Method call:

UpdateOnline(mcon, convertedXml, fileName, stmId);

Method definition:

private void UpdateOnline(MySqlConnection mcon, string convertedXml, string fileName,int stmtId)
    {
        var cmd = new MySqlCommand("update user_account_statement set statement_xml=@xml,"+
            " file_name=@fName, status='closed' where statement_id=@stmtId",mcon);
        cmd.Parameters.AddWithValue("@xml", MySqlDbType.LongText).Value = convertedXml;
        cmd.Parameters.AddWithValue("@fName", MySqlDbType.VarChar).Value = fileName;
        cmd.Parameters.AddWithValue("@stmtId", MySqlDbType.Int32).Value=stmtId;
        var query = cmd.ToString();
        cmd.ExecuteNonQuery();
    }

I checked many times for data types and all but not able to figure it out that where I am missing.

UPDATE

Table schema is below:

CREATE TABLE `user_account_statement` (
`statement_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`statement_xml` longtext NOT NULL,
`status` varchar(10) NOT NULL,
`file_name` varchar(60) NOT NULL,
PRIMARY KEY (`statement_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1084 DEFAULT CHARSET=latin1;

Solution

  • Below code worked for me. :

     private void UpdateOnline(MySqlConnection mcon, string convertedXml, MemoryStream statment, string fileName, int stmtId)
        {
            var convertedStmt = statment.ToArray();
            var cmd = new MySqlCommand("update user_account_statement set" +
             " file_name=@fName,statement_file=@stmt, statement_xml=@xml, status='closed' where statement_id=@stmtId", mcon);
            cmd.Parameters.Add("@fName", MySqlDbType.VarChar);
            cmd.Parameters.Add("@stmtId", MySqlDbType.UInt32);
            cmd.Parameters.Add("@xml", MySqlDbType.LongText);
            cmd.Parameters.Add("@stmt", MySqlDbType.LongBlob);
            cmd.Parameters["@fName"].Value = fileName;
            cmd.Parameters["@stmtId"].Value = stmtId;
            cmd.Parameters["@xml"].Value = convertedXml;
            cmd.Parameters["@stmt"].Value = convertedStmt;
            cmd.CommandTimeout = 1000;
            cmd.ExecuteNonQuery();
    
        }