Form subimission with Database (upload image, text field) - Webmatrix

Posted by Shasseb under C# on 12/16/2012 | Points: 10 | Views : 1957 | Status : [Member] | Replies : 1
I have the following code that should upload data into a database and I get the following error at : Not sure what is the problem....

The index was out of range - Must be non - negative and less than t he collection size.

Parameter name index

var uploadedFile = Request.Files[0];

int id = 0;
var fileName = "";
var fileMime = "";
if (IsPost) {
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
if(fileName != String.Empty)

var Info2 = Request.Form["Info"];
fileMime = uploadedFile.ContentType;
var fileStream = uploadedFile.InputStream;
var fileLength = uploadedFile.ContentLength;

byte[] fileContent = new byte[fileLength];
fileStream.Read(fileContent, 0, fileLength);
var db = Database.Open("Gallery2");
var sql = "INSERT INTO Photo2 (FileName, FileContent, MimeType, Info ) VALUES (@0,@1,@2,@3)";

db.Execute(sql, fileName, fileContent, fileMime, Info2);

id = (int)db.GetLastInsertId();

<!DOCTYPE html>

<html lang="en">
<meta charset="utf-8" />
<form id="form1" method="post">


<input name="Info" type="text" size="50"
<td><input type="submit" value="Submit"/></td>




Posted by: Shasseb on: 12/16/2012 [Member] Starter | Points: 25

I want to save an image which is saved in the folder to the database but couldn't find the way how to resolve this issue... kindly help me pleasseeee....

Shasseb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response