Answered by:
Image not uploaded into database

Question
-
User-1994446809 posted
I have a code behind a web form that is suppose to store image into the database;
protected void Button1_Click(object sender, EventArgs e) { // Read the file and convert it to Byte Array string filePath = FileUpload1.PostedFile.FileName; string filename = Path.GetFileName(filePath); string ext = Path.GetExtension(filename); string contenttype = string.Empty; int size = FileUpload1.FileBytes.Length; //Set the contenttype if (ext == ".jpg") { contenttype = "image/jpg"; } if (contenttype != string.Empty) { Stream fs = FileUpload1.PostedFile.InputStream; BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); //insert the file into database string sql = "INSERT INTO DocTbl(Name, Contyp,Size, CardData)" + " VALUES(@Name, @ContentType,@Size, @CardData);SELECT @@IDENTITY"; SqlParameter[] parameters = new[] { new SqlParameter("@Name",filename ), new SqlParameter("@ContentType",contenttype ), new SqlParameter("@Size",size ), new SqlParameter("@CardData",bytes ) }; int id = ExecuteScalar(sql, parameters); Session["CardId"] = id; if (id != -1) { lblMessage.ForeColor = System.Drawing.Color.Green; lblMessage.Text = "File Uploaded Successfully"; // Return a QR pic BindDocQR(); } else { lblMessage.ForeColor = System.Drawing.Color.Red; lblMessage.Text = "File Uploaded Failed"; } }
And in my Database Table, I have four columns, namely: Id, Name, Contyp, Size, CardData.
Id Name Contyp Size CardData The data type of these columns are set as nvarchar(MAX). When I try to upload any image, it tells me "File Uploaded Failed". I tried to resolve this by removing the "Id" column from the table, in order to match with the parameters and I got this error in the browser;
Exception Details: System.InvalidCastException: Object cannot be cast from DBNull to other types.
Source Error:Line 175: if (obj != null) Line 176: { Line 177: result = Convert.ToInt32(obj); Line 178: } Line 179: }
When i got this error, I tried to set the columns to "Allow Nulls", but it did not work. I have also tried to set the Data Type of "Contyp" column to varbinary(MAX) to see if this was the problem but it did not work too.
What should I do please?
Saturday, May 16, 2020 5:41 AM
Answers
-
User-1994446809 posted
I have resolved this. the issue was with my database. I changed the "CardData" Data Type to nvarBinary(MAX) and it is working.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 16, 2020 6:57 PM
All replies
-
User-1994446809 posted
Hello,
I later made changes in my database; I changed the Identity Specification of the primary key "Id" in the properties section to True, and Allowed Nulls on other columns. Then this was the error that was displayed (error is shown in the captured imagelink below).
https://imgur.com/gallery/oiRIXfa
I'm having tough time resolving this
Saturday, May 16, 2020 5:49 PM -
User-1994446809 posted
I have resolved this. the issue was with my database. I changed the "CardData" Data Type to nvarBinary(MAX) and it is working.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 16, 2020 6:57 PM