Hi,
Good day. I just want to ask some advice for my problem. Currently, I'm trying to update an oracle blob by getting an image from file. The whole process is being executed. But the problem is that the image is changed to null. any idea? by the way here is my code.
public static string UpdateVisitorPhotos()//(string Capacity, string Event)
{
string sRet = "";
string conStr = ConfigurationSettings.AppSettings["conStr"];
OdbcConnection con = new OdbcConnection(conStr);
OdbcCommand cmdInsVistPhoto = new OdbcCommand();
OdbcCommand cmdInsVistPhoto1 = new OdbcCommand();
con.Open();
cmdInsVistPhoto.Connection = con;
cmdInsVistPhoto1.Connection = con;
OdbcTransaction trans = con.BeginTransaction();
cmdInsVistPhoto.Transaction = trans;
cmdInsVistPhoto1.Transaction = trans;
Console.WriteLine("Connected");
string incomingFolder = string.Format(@"{0}", ConfigurationSettings.AppSettings["PhotoPath"]);
DirectoryInfo incoming = new DirectoryInfo(incomingFolder);
foreach (FileInfo file in incoming.GetFiles())
{
Console.WriteLine("retrieving photos");
string fileName = file.Name;
string fullPath = string.Format(@"{0}\{1}", incomingFolder, fileName);
string passno = fileName.ToUpper().Replace(".JPG","").ToString();
if (File.Exists(fullPath))
{
try
{
byte[] photo = getPhoto(incomingFolder, passno);
if(photo!= null)
{
cmdInsVistPhoto.CommandText = "UPDATE TB_Visitors1 SET vistPhoto=? WHERE PASSNO=?";
cmdInsVistPhoto.Parameters.Add("@vistPhoto",OracleType.Blob,photoLength).Value = photo;
cmdInsVistPhoto.Parameters.Add("@passno",OdbcType.VarChar).Value = passno.Trim();
cmdInsVistPhoto.ExecuteNonQuery();
cmdInsVistPhoto.Parameters.Clear();
}
sRet = "";
trans.Commit();
}
catch (Exception ex)
{
sRet = ex.Message;
Console.WriteLine(ex.Message.ToString());
sRet = "Update operation unsuccessful";
trans.Rollback();
}
finally
{
con.Close();
}
}
}
return sRet;
}
private static Byte[] getPhoto(string incoming, string passno)
{
//Byte[] bRets = new Byte[256];
Byte[] bRets;
try
{
string fileName = incoming + passno + ".jpg";
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
photoLength = int.Parse(fs.Length.ToString());
bRets = new byte[fs.Length];
fs.Read(bRets,0,System.Convert.ToInt32(fs.Length));
fs.Close();
return bRets;
}
catch(Exception ex)
{
return null;
}
}
Thanks,
Jay
Jorge L FernandezPosted Nov 5, 2009, 11:59 AM
I think this may be raising an exception in some cases since the property Length of FileStream returns a Long not an Int. Then trying to parse an exception will be raised, catched and then a null will be returned instead of your byte[].
Please, play with this and see if this problem is solved. If this answer your question please mark as answered.