I’m trying to add a row into a mysql database table using visual studio 2010 and I haven’t had problems until now. I keep getting the error:
“Fatal error encountered during command execution.”
Table structure:
delimiter $$
CREATE TABLE `magazine_images` (
`image_id` int(11) NOT NULL AUTO_INCREMENT,
`MagazineKey` int(5) DEFAULT NULL,
`image_type` varchar(45) NOT NULL,
`image` blob,
`image_size` varchar(45) NOT NULL,
`image_ctgy` varchar(45) NOT NULL,
`image_name` varchar(50) NOT NULL,
PRIMARY KEY (`image_id`),
KEY `image_magazine` (`MagazineKey`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1$$
C Sharp query:
string image = "INSERT INTO magazine_images (MagazineKey, image, image_type, image_size, image_ctgy, image_name) " +
"VALUES(@magkey, @image, @image_type, @image_size, @image_ctgy, @image_name)";
this.cmd = new MySqlCommand(putImage, this.con);
this.read = this.cmd.ExecuteReader();
this.read.Read();
this.cmd.Parameters.AddWithValue("@magkey", int.Parse(labelKey.Text));
this.cmd.Parameters.AddWithValue("@image", textBoxImageFile.Text);
this.cmd.Parameters.AddWithValue("@image_name", textBoxImageName.Text);
this.cmd.Parameters.AddWithValue("@image_type", comboBoxImageType.Text);
this.cmd.Parameters.AddWithValue("@image_ctgy", textBoxImageCategory.Text);
this.cmd.Parameters.AddWithValue("@image_size", labelImageSize.Text);
this.read.Close();
Either your code is incomplete or you are currently adding your query parameters after you execute the query – you have to do it before the query executes.
Also you are not even using the
imagevariable – you are usingputImagewhich you haven’t shown. Thirdly you should not useExecuteReader()– this is for retrieving only, but you are doing an insertion, for that you needExecuteNonQuery().In summary: Either you have a lot of copy & paste errors or your sample does not reflect your real code.