Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7765151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:05:58+00:00 2026-06-01T15:05:58+00:00

While trying to validate form data on my page i get the following error:

  • 0

While trying to validate form data on my page i get the following error:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.

my code for this page is:

@{
var db= Database.Open("Games");
var sqlQ = "SELECT * FROM Games";
var data = db.Query(sqlQ);
Page.Title = "Add Game"; 
}
@{
    var fileerrorMessage = "";
    var NameerrorMessage = "";
    var Gamefile = "";
    var GameName = "";
    var fileData = Request.Files[0];
    var fileName = Path.GetFileName(fileData.FileName);
    var fileSavePath = Server.MapPath("~/upload/" + fileName);
    GameName=Request["formName"];
    Gamefile=fileName;
    if (IsPost) {
    var isValid = true;
        if (Gamefile.IsEmpty()){
            fileerrorMessage = "Please upload a file.";
            isValid = false;
        }

        else if (GameName.IsEmpty()){
            NameerrorMessage = "Please give the game a name.";
            isValid = false;
        }

        if (isValid){

        fileData.SaveAs(fileSavePath);
        var SQLINSERT = "INSERT INTO Games (Name, file_path) " + "VALUES (@0, @1)";
        db.Execute(SQLINSERT, GameName, Gamefile);
        Response.Redirect("default.cshtml");
        }

        else
        {
            <p class="message error">Please correct the errors and resubmit the form.</P> 
        }

    }
}
<form action="" method="post" enctype="multipart/form-data">

  <input type="file" name="file" id="file" />
    @if(!fileerrorMessage.IsEmpty()) {
        <label for="file" class="validation-error">
            @fileerrorMessage
        </label>
    }
  <p><input type="text" name="formName" value="@GameName" />
     @if(!NameerrorMessage.IsEmpty()) {
        <label for="file" class="validation-error">
            @NameerrorMessage
        </label>
    }

  <input type="submit" value="Add Game" />
</form>

The error is apparently with line 12. which means there should be something wrong with: var fileData = Request.Files[0];

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:05:59+00:00Added an answer on June 1, 2026 at 3:05 pm

    If you try to reference Request.Files by index when there are no files uploaded, you will get this error. Your code runs when the page first loads, so the user has had no chance to upload a file, which means Request.Files is bound to be empty.

    Move if(IsPost) up above your declaration for fileData and check the count of Request.Files before attempting to reference anything in it:

    if(IsPost){
        if(Request.Files.Count > 0){
            var fileData = Request.Files[0];
            var fileName = Path.GetFileName(fileData.FileName);
            var fileSavePath = Server.MapPath("~/upload/" + fileName);
            //etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to validate my forms i get the following error: Expected a {
I'm trying to validate a form of a test. I get an error in
I'm trying to figure out the best way to validate data within a MVC
While trying out an experimental UINavigationController-based iPhone application, I ran into a problem when
While trying to host the CLR, I keep getting this: error C2440: 'function' :
While trying to execute the following lines only the last two statements are displayed(Here
I am trying to validate a form submission in Kohana 3. I have the
I am following alloy-complex-form-examples from github trying to learn nested forms and I just
I'm trying to validate an XML file with XSD, but I get a Could
I'm running my site through the W3C's validator trying to get it to validate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.