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 6530737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:47:31+00:00 2026-05-25T09:47:31+00:00

I have this codes: private string ErrorMessage(string input) { { if (!string.IsNullOrEmpty(input)) return input;

  • 0

I have this codes:

    private string ErrorMessage(string input)
    {
        {
            if (!string.IsNullOrEmpty(input))
                return input;
            BtnImport1.Visible = false; 
        }
        return "No value entered!";

    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strFileNameOnServer = fileUpload.PostedFile.FileName;
        string fileExt =
        System.IO.Path.GetExtension(fileUpload.FileName);

        if (fileUpload.PostedFile != null && fileExt == ".csv")
        {
            try
            {
                fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads"));
                Label1.Text = "File name: " +
                       fileUpload.PostedFile.FileName + "<br>" +
                       fileUpload.PostedFile.ContentLength + " kb<br>" +
                       "Content type: " +
                       fileUpload.PostedFile.ContentType;
            }
            catch (Exception ex)
            {
                Label1.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>.  " + ex.Message;
            }
            BtnImport1.Visible = true;
            Cancel.Visible = true;
            fileUpload.Visible = false;
            btnUpload.Visible = false;
        }
        else
        {

            Label1.Text = "Error - a file name must be specified/only csv files are allowed";
            return;

        }

        var data = File.ReadAllLines(Server.MapPath("~/Uploads"))
          .Select(line => line.Split(','))
          .Select(columns => new { GuestID = ErrorMessage(columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7]), Registration = ErrorMessage(columns[8]) });

        myGridView.DataSource = data; 
        myGridView.DataBind();

    }

Currently, if there are empty fields in the gridview, it will display “No value entered”, but, this is only for column[1] to column [7]. If I were to upload the csv file with column[8] and column[0] containing no value, the debugger would stop as there is an error. How do I avoid this? Please help!

  • 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-05-25T09:47:32+00:00Added an answer on May 25, 2026 at 9:47 am

    Change this line:

    var data = File.ReadAllLines(Server.MapPath("~/Uploads"))
                  .Select(line => line.Split(','))
                  .Select(columns => new { GuestID = ErrorMessage(columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7]), Registration = ErrorMessage(columns[8]) });
    

    To this:

    var data = File.ReadAllLines(Server.MapPath("~/Uploads"))
              .Select(line => line.Split(','))
              .Select(columns => new { GuestID = ErrorMessage(columns.Length<=8?"":columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7]), Registration = ErrorMessage(columns.Length<=8?"":columns[8]) });
    

    UPDATE: Expanding my answer to answer second question.

    Change your ErroMessage method to this:

    private string ErrorMessage(string input, string dataTypeExpected="string")   
    {
    
            switch (dataTypeExpected)
            {
                case "string": if (!string.IsNullOrEmpty(input))
                                return input;
                                break;
                case "int": 
                      int result=-1;
                      if (int.TryParse(input, out result))
                           return result.ToString();
                      else return "Error: value must be an integer";
    
             }                
    
             return "No value entered!";
     }
    

    And also change the var data… part to this (pay attention how I pass columns[7] & columns[8] to the ErrorMessage method):

    var data = File.ReadAllLines(Server.MapPath("~/Uploads"))
                  .Select(line => line.Split(','))
                  .Select(columns => new { GuestID = ErrorMessage(columns.Length<=8?"":columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7],"int"), Registration = ErrorMessage(columns.Length<=8?"":columns[8],"int") });
    

    Explanation: I rewrote the ErrorMessage function to receive an extra optional parameter (defaulted to “string”) called dataTypeExpected. You can use that parameter to indicate the data type that the input parameter should be in. If the data passed in is an empty string or a string that does not contain digits and you specify the dataTypeExpected parameter to be “int” (as I did above), the error message will say “Error: value must be an integer”;

    Notice that I removed the line BtnImport1.Visible = false; from the ErrorMessage method because you are executing that line once for every time the ErrorMessage is called when you only need to execute it once. So, move that line to some place else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code private void writeReport(IReport report, string reportName) { string reportString =
I have this snippet of code private Templates retrieveFromCache(String name) { TemplatesWrapper t =
I have this code: //Return null if the extension doesn't have the value, returns
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
I have a private project, and i want it hosted on google code. this
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
I have some C# code that generates google maps. This codes looks at all
I have some codes like this: cats = Category.objects.filter(is_featured=True) for cat in cats: entries
I have a list of error codes I need to reference, kinda like this:
i have done up a method to return a string values of a textfile

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.