Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i have a crictical problam in c# winapp with mysql backend...Actually i am saving image in database using filestreme with the conversion into binary format and its successfully running but after saving when i am retriving it from database(MySql Longblob) by this code "int filesize = data.GetInt32(data.GetOrdinal("columne name"));(data is object of mysqldatareader)" there is error like "Object must implement IConvertible. and Invalid Cast Exception was cought".........can anyone help me......tommrow is ma project submision and its erreting error for me......so plz help
Posted
Comments
Tejas Vaishnav 2-Oct-12 4:02am    
i think you are trying to convert file stream data to int that's why you got that error.

so please provide your bit of code which will store your image data and also part of code which will retrieving your saved data from database. so we can some how figure out your problem and try to get resolve that.
Shubh Agrahari 2-Oct-12 6:40am    
1st i am retriving file from opendialouge box and saving in into mysql database.....using this code.....

private string curFileName = null;
OpenFileDialog openDlg = new OpenFileDialog();

openDlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";

string filter = openDlg.Filter;

openDlg.Title = "Open only Image Files";

if (openDlg.ShowDialog() == DialogResult.OK)
{

curFileName = openDlg.FileName;

textBox2.Text = curFileName;
}
FileStream fs = new FileStream(curFileName,

FileMode.OpenOrCreate, FileAccess.Read);
int file = (int)fs.Length;
byte[] rawData = new byte[file];

fs.Read(rawData, 0, (int)file);

fs.Close();
string str = "select * from Doc";
if (con.State != ConnectionState.Open)
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(str,con);
MySqlCommandBuilder mcb = new MySqlCommandBuilder(da);
DataSet ds = new DataSet("Doc");
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds,"Doc");
DataRow row=ds.Tables["Doc"].NewRow();
row["pfno"] = label7.Text;
row["depname"] = label8.Text;
row["docname"] = comboBox1.Text;
row["docdisc"] = textBox1.Text;
row["addedby"] = sd.uname;
row["depdoc"] =rawData;
ds.Tables["Doc"].Rows.Add(row);
da.Update(ds,"Doc");
if(con!=null)
{
if(con.State==ConnectionState.Open)
{
con.Close();
con.Dispose();
}
MessageBox.Show("Document Saved Successfully");
}
_____________________________________________________________________________________
it is running succesfully for saving but now the main fault when i am retriving that ID specific image on winform using this code....

try
{
string str1 = "select * from doc where pfno='" + label7.Text + "' and depname='"+label8.Text+"'";
con.Open();
MySqlCommand cmd = new MySqlCommand(str1, con);
MySqlDataReader data = cmd.ExecuteReader();
if (data.Read())
{
string name = data.GetString(data.GetOrdinal("depname"));
" error Ocurring on this line below and error is Invalid Cast Exception(Object must implement IConvertible.)"
int filesize = data.GetInt32(data.GetOrdinal("depdoc"));
byte[] rawData = new byte[filesize];
data.GetBytes(data.GetOrdinal("depdoc"), 0, rawData, 0, filesize);
FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write);
fs.Write(rawData, 0, filesize);
fs.Close();
pictureBox1.Image = new Bitmap(name);
}

}
catch(Exception ex)
{
MessageBox.Show("Document Test " + ex);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
con.Dispose();

}
anyone have solid answer....??

1 solution

Is "columne name" the column containing the size? And why do you a data.GetInt32(data.GetOrdinal("columne name")) instead of data.GetInt32("columne name")? Perhaps a GetInt64 would do the trick.
In case that "columne name" contains the image, you could use the OCTET_LENGTH function in a query to retrieve its length:
SQL
SELECT OCTET_LENGTH("columne name") FROM YOURTABLE WHERE ....
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900