Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have registration asp.net C# application that is used by multiple users.

In this web application I have a global variable with name FarmIdOld that supposed to take its value from a label in the same page with id LblFarmID so FarmIdOld = LblFarmID.Text; . this variable is used in a function for uploading files/attachments, so there will be a folder created with the name of this global variable FarmIdOld , and then the same variable is used to create sub-folder and also to be a part of the file name.

Usually it works ok with no problems and it stores the correct value of the label , and the folders, sub-folders, files names in FTP are created correctly with the correct names

but after a period of time, I discovered that there are some folders/sub-folders/files are created with the name of the session/(the login username) , and some other files are created with no names/empty names

what happened? and how come that the variable stores wrong values? how the variable take the session value while I didn't assign the session value to this variable before? Note: I didn't ever assign this variable to any value but the lable FarmIdOld = LblFarmID.Text;

here is my code, where is the mistake in the code? do I have to put any additional code to prevent storing wrong values in the variable?

C#
public partial class MultiOwners : System.Web.UI.Page
    {

        string FarmIdOld; // here declare the global variable
        public void uploadingFilesFunction() // this is the function that use it
        {

            //----------------- Start Get File Size --------------------

            FarmIdOld = LblFarmID.Text; // here I assign the value to the varbaile, it is the text of the lable

            Int32 vOldCard = 1;
            Int32 vAgriculturalCard = 1;


            //---------- End Get File Size ----------------------

            try
            {
                // Start Create Folder ----------------------->



                FolderPath = @"~/attachments/" + FarmIdOld; //here I'm creating the folder with the name of the variable FarmIdOld

                bool exists = System.IO.Directory.Exists(Server.MapPath(FolderPath));

                if (!exists)
                    System.IO.Directory.CreateDirectory(Server.MapPath(FolderPath));

            }
            catch (Exception ee)
            {
                LblCatchError.Text = ee.ToString();
            }

            // End Create Folder ---------------------------------------->

            try
            {
                // Start Create SubFolder ---------------------------------------->

                SubFolderPath = @"~/attachments/" + FarmIdOld + @"/" + FarmIdOld; // here I'm creating the sub folder path with the name of the varaible FarmIdOld



                bool exists = System.IO.Directory.Exists(Server.MapPath(SubFolderPath));

                if (!exists)
                    System.IO.Directory.CreateDirectory(Server.MapPath(SubFolderPath));


            }
            catch (Exception ee)
            {
                LblCatchError.Text = ee.ToString();
            }

            // End Create Folder --------------------------------->

            try
            {
                //Start Uploading files --------------->

                //upload files OldCard
                if (FileUploadOldCard.HasFile)
                {
                    // Start Old Card Length
                    vOldCard = FileUploadOldCard.PostedFile.ContentLength;
                    double vCalculateOldCard = vOldCard / 1048576;
                    double vOldCardSize = Math.Round(vCalculateOldCard, 2);
                    // End Old Card Length

                    if (FileUploadOldCard.HasFile && vOldCard < 2100907)
                    {
                        String ext = System.IO.Path.GetExtension(FileUploadOldCard.FileName);

                        if (ext != ".pdf")
                        {
                            LblFileOldCardSize.Text = "Only PDF type allowed";
                            FileUploadOldCard.Enabled = true;

                        }
                        else
                        {
                            string relativePath = SubFolderPath + @"/OldCard_" + FarmIdOld + ext;


                            FileUploadOldCard.SaveAs(Server.MapPath(relativePath));
                            FileUploadOldCard.Enabled = false;

                        }

                    }
                    else if (FileUploadOldCard.HasFile && vOldCard > 2100907)
                    {
                        // Display the length of the OldCard file in a label.    
                        LblErrorOldCardSize.Text = "File must be less than 2MB";
                        LblFileOldCardSize.Text = "current size is " + vOldCardSize.ToString() + " MB";
                        FileUploadOldCard.Enabled = true;
                    }

                }
                else { }
                //end upload files OldCard

                //upload files Agricultural Card

                if (FileUploadAgriculturalCard.HasFile)
                {
                    // Start Agricultural Card Length
                    vAgriculturalCard = FileUploadAgriculturalCard.PostedFile.ContentLength;
                    double vCalculateAgriculturalCard = vAgriculturalCard / 1048576;
                    double vAgriculturalCardSize = Math.Round(vCalculateAgriculturalCard, 2);
                    // End Agricultural Card Length

                    if (FileUploadAgriculturalCard.HasFile && vAgriculturalCard < 2100907)
                    {
                        String ext = System.IO.Path.GetExtension(FileUploadAgriculturalCard.FileName);
                        if (ext != ".pdf")
                        {
                            LblFileAgriculturalCardSize.Text = "Only PDF type allowed";
                            FileUploadAgriculturalCard.Enabled = true;
                        }
                        else
                        {
                            string relativePath = SubFolderPath + @"/MarketingCard_" + FarmIdOld + ext;


                            FileUploadAgriculturalCard.SaveAs(Server.MapPath(relativePath));
                            FileUploadAgriculturalCard.Enabled = false;
                        }
                    }

                    else if (FileUploadAgriculturalCard.HasFile && vAgriculturalCard > 2100907)
                    {
                        // Display the length of the Agricultural file in a label.    
                        LblErrorAgriculturalCardSize.Text = "File must be less than 2MB";
                        LblFileAgriculturalCardSize.Text = "current size is " + vAgriculturalCardSize.ToString() + " MB";
                        FileUploadAgriculturalCard.Enabled = true;
                    }
                }
                else { }
                //end upload files Agricultural Card


                //End Uploading files --------------------------->

            }
            catch (Exception ee)
            {
                LblCatchError.Text = ee.ToString();
            }
        }


protected void BtnAttachments_Click(object sender, EventArgs e)
        {
            try
            {
                uploadingFilesFunction(); // here I retreive the function by Clicking on button
}
            catch (Exception ee)
            {
                LblCatchError.Text = ee.ToString();
            }

        }
}
Posted
Updated 7-May-15 13:30pm
v4
Comments
Wombaticus 7-May-15 17:45pm    
Well, start by searching your code for any statements that set LblFarmID.Text
amal50 7-May-15 17:57pm    
I double searched now, everything is correct LblFarmID.Text is set by retrieving the FarmID from the database, this is the only code that I wrote for setting LblFarmID.Text

SELECT FRM_ID from farms WHERE FRM_ID =" + "'" + SearchFarmIDtxt.Text + "'";
and then I set it by:
LblFarmID.Text = rdrCard[0].ToString();

there is no way to store the session of the username in the label LblFarmID.Text
it is so strange that my code created the folders with the name of the session value which I stored in it the login username instead of creating the folder with the value of LblFarmID.Text , as I assigned the value of LblFarmID.Text to the variable FarmIdOld

it is not always happen, the code usually works correctly , but sometimes it create folders with wrong names/session and sometimes doesn't create folders at all which causes losing the uploaded files
Sergey Alexandrovich Kryukov 7-May-15 18:12pm    
It can be done with 100% guarantee. I provided exact instruction and code modification to assist it. Please see Solution 1.
—SA

1 solution

There is no such thing as "global variable" in .NET. Your object FarmIdOld you refer to is the instance (non-static!), private(!) member of a non-static(!) class MultiOwners. It cannot be even close the anything which could possible be described as "global". You have as many instanced of this object as the instances of the class MultiOwners. Therefore, the question, as formulated makes no sense at all. The problem is also not described.

However, I can explain how you can track all the modifications of this object, if you lost ends. Replace it with some explicitly backed property (that is, not auto-implemented):
C#
public partial class MultiOwners : System.Web.UI.Page {

    string FarmIdOld {
        get { return farmIdOldInstanceBackingField; }
        set {
            farmIdOldInstanceBackingField = value; // break point here
        } //set FarmIdOld
    } //FarmIdOld

    string farmIdOldInstanceBackingField;

    //...
} //class MultiOwners
This will be your refactoring, some change in code which is guaranteed to keep the same semantic as before the change.
But now, you can use the debugger and set a breakpoint (please see the comment in the code sample above "break point here"). Each time you get the execution stopped at this break point, you can look at the debug window "Call stack" and see where the call comes from. This will be enough to find ends.

Now, you may need to solve one more debugging problem. As your FarmIdOld is something opposite to "global" :-), you can have several instanced of it.
You can mark all instances of all objects during debugging as unique objects. For this purpose in the debug "Watch" or "Autos" window, you can add the object to watch, if it is not "Auto", and use context menu when this object is selected. Then click "Make Object ID" menu items. From this moment, you can tell this object from other instances on next execution of the same line.

—SA
 
Share this answer
 
Comments
amal50 7-May-15 19:20pm    
I'm sorry for the wrong definitions, and thank you for correcting my understanding, I really appreciate your help and effort.

I meant in global that it can be recognized in every function without re-declare it.

debugging may help me if this error always happen

I will try to explain the problem again without writing the full code:

I have variables

String FarmIdOld = LblFarmID.Text;

string vsession = Session["username"].ToString();

in this line I create new folder with name = the value of the variable FarmIdOld

string FolderPath = @"~/attachments/" + FarmIdOld; //here I'm creating the folder with name "the value of the variable FarmIdOld"

98% of the transaction that done by my web application are correct, folders are created with names that are stored in FarmIdOld

1% of the transaction that done by my web application are not correct, folders are created with names equal to the values that are stored in vsession

but I didn't ever assign vsession to FarmIdOld and also I didn't ever wrote any code that create folder with name but FarmIdOld, also vsession variable is not used at all in the uploading file function.

how come that the folders are created with the name of values stored in vsession?

how the value of vsession are stored in FarmIdOld while I didn't ever assign vsession to FarmIdOld

1% of the transaction that done by my web application not correct, folders are not created and files stored in the root with names like MarketingCard_.pdf
while I wrote in my code to store the file as @"/MarketingCard_" + FarmIdOld + "pdf";

even with debugging, I will not find any error, because the code always work correctly and it stores the values correctly, it rare to happen and create folders with wrong names, the problem that when it happens, I lose files of around 300 application

is my explanation clear? I feel that I have problem in explaining and delivering information :/
I'm sorry for that
Sergey Alexandrovich Kryukov 7-May-15 19:23pm    
This kind of debugging is exactly what should show you how. You can also add break-point where you modify and read vsession.
—SA
amal50 7-May-15 19:31pm    
ok, I will try it, and thank you for help :)
Sergey Alexandrovich Kryukov 7-May-15 20:41pm    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900