el ninho
el ninho

Reputation: 4233

Assignment inside IF statement

string Newfilename;
string Defaultfilename;

        protected void btnup_Click(object sender, EventArgs e)
    {
        if (ASPxUploadControl1.HasFile)
        {
            string fileExt =
                Path.GetExtension(ASPxUploadControl1.FileName);


            if (fileExt == ".xls" || fileExt == ".xlsx")

                try
                {
                    string extension = Path.GetExtension(ASPxUploadControl1.FileName);
                    string id = Guid.NewGuid().ToString();
                    string fileLocation = string.Format("{0}/{1}{2}", Server.MapPath("upload/"), id, extension);
                    ASPxUploadControl1.SaveAs( fileLocation );
                    StatusLabel.Text = "Upload status: File uploaded!";

                    Newfilename = fileLocation;
                    Defaultfilename = Path.GetFileName(ASPxUploadControl1.FileName);

                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            else
            {
                StatusLabel.Text = "Please choose excel file";
            }

        }
    }

I am trying to assign values to Newfilename and Defaultfilename (inside "try", after naming uploaded file), but they stay empty.

Where I'm wrong?

Upvotes: 0

Views: 1065

Answers (3)

MethodMan
MethodMan

Reputation: 18843

Refactor your code and think about the process that you want .. then Debug the Code.. Test it.. and if you have an Issue then edit your post.. that's what I suggest.. If statements should be wrapped with in a code block "{ }" same way that you have Try {} a good rule of thumb for even readability would be to wrap everthing around {} if you have If Else otherwise it makes if hard to read as well as lend assistance.

inside your code where you are declaring the following, make them variables within the method itself

  • string fileExt = string.Empty;
  • string extension = string.Empty;
  • string id = string.Empty;
  • string fileLocation = string.Empty;

so your method would look like this

protected void btnup_Click(object sender, EventArgs e)
{ 

    string fileExt  = string.Empty; 
    string extension = string.Empty;
    string id = string.Empty;
    string fileLocation = string.Empty;

    if (ASPxUploadControl1.HasFile)
    {              
         fileExt = Path.GetExtension(ASPxUploadControl1.FileName);
         if (fileExt == ".xls" || fileExt == ".xlsx")
         {
           try
           {
             extension = Path.GetExtension(ASPxUploadControl1.FileName);
             id = Guid.NewGuid().ToString();
             fileLocation = string.Format("{0}/{1}{2}", Server.MapPath("upload/"), id, extension);
             ASPxUploadControl1.SaveAs( fileLocation );
             StatusLabel.Text = "Upload status: File uploaded!";
             Newfilename = fileLocation;
             Defaultfilename = Path.GetFileName(ASPxUploadControl1.FileName);
            }                  
            catch (Exception ex)
            {
              StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
         }
         else
        {
          StatusLabel.Text = "Please choose excel file";
        }
    }
}

Upvotes: 1

AlanT
AlanT

Reputation: 3663

Can you step through the execution?

Does

NewFilename = fileLocation;

get executed?

If so, what are the values for NewFilename before and after?

This looks like ASP.Net code. If it is. Is the problem that when you try to use NewFilename elsewhere in the code-behind is is blank. If you are, then NewFilename may need to be saved to the session to allow you to use it.

hth,
Alan.

Upvotes: 1

Noel Frostpaw
Noel Frostpaw

Reputation: 3999

Path.GetExtension returns null if the passed value is null and returns string.Empty if the passed value doesn't have an extension. So please check if the value inside ASPxUploadControl1.FileName actually contains something usefull.

If this is not the case then you'll have to look up where the value is set and debug from there to find out why it's not set.

Upvotes: 1

Related Questions