1

I have this codes to allow users to upload csv files and the contents will be displayed in the GridView, the problem is, if any of the rows/columns is empty, I need to display an error message, can anyone help me with this? I'm uncertain with using asp.net c# cos I'm new at this. This is my codes:

    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(ConfigurationManager.AppSettings + appDataPath + "\\" + strFileNameOnServer);
                fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploaded"));
                //string appPath = HttpContext.Current.Request.ApplicationPath;
               // string physicalPath = HttpContext.Current.Request.MapPath("~/MajorProject");
                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("~/Uploaded"))  
          .Select(line => line.Split(','))    
          .Select(columns => new {GuestName = columns[0], Guest_ID = columns[1], IC_Number = columns[2]}); 
        myGridView.DataSource = data; 
        myGridView.DataBind();











    }

Please help!

1 Answer 1

1

Create a method called value or error message and pass the columns[index] to that

.Select(columns => new {GuestName = ValueOrErrorMessage(columns[0]), Guest_ID = ValueOrErrorMessage(columns[1]), IC_Number = ValueOrErrorMessage(columns[2])}); 
      ...

private string ValueOrErrorMessage(string input){
   if(!string.IsNullOrEmpty(input))
      return input;
   }
   return "no value"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! But where should I put the private string ValuOrErrorMessage method? Sorry
just put it under the btnUpload_Click method
done that, but now it displayed errors at GuestName=ErrorMessage(columns[0]), the error is cannot apply indexing with[], what is this error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.