I have made a store procedure in database for adding an image, but now the problem is, I need to get the image data on client side and convert into bytes to insert to database, Here is my code i'm using:
public static void AddImg (string asImgName, string asFilePath, string asUser, string asRemarks)
{
cmd = new SqlCommand("sp_InsertImage", SqlConnect.con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Image_Name", SqlDbType.NVarChar).Value = asImgName;
cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = File.ReadAllBytes(asFilePath);
cmd.Parameters.Add("@Uploader", SqlDbType.NVarChar).Value = asUser;
cmd.Parameters.Add("@Remarks", SqlDbType.NVarChar).Value = asRemarks;
cmd.Parameters.Add("@UploadDate", SqlDbType.DateTime).Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
cmd.ExecuteNonQuery();
}
Now I'm facing a problem:
On line 5 of the code
"cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = File.ReadAllBytes(asFilePath);"
It only reads the server side path, so what changes do I need to make so that I can upload the image?
Thanks
====================================== I solved this problem by using the upload file control in VWD
fileUpload1.FileBytes
Have you tried reading the byte array before setting up the command?
public static void AddImg (string asImgName, string asFilePath, string asUser, string asRemarks)
{
var bytes = File.ReadAllBytes(asFilePath);
...
cmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = bytes;
...
cmd.ExecuteNonQuery();
}
I'm assuming the client is the one calling the method above. Not positive this will solve your problem, but it's worth a shot. Reading the bytes before-hand is easier to read anyway and you should make this change.