Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I was wondering why i'm getting an empty string value when i try to retrieve a filename. To better understand what i meant. In order to get the filename from the AjaxFileUpload control, I would do this code(which is in class named CreateBrands):

What I have tried:

C#
string filename = Path.GetFileName(e.FileName);
ConnectionClassGuitarItems.stringInstrumentFileName1 = filename;
itemFileUpload1.SaveAs(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/") + filename);


As you can see, i passed the string filename to ConnectionClassGuitarItems.stringInstrumentFileName1, which is a property in a class called ConnectionClassGuitarItems. Here is a bit of code in the ConnectionClassGuitarItems. I did not include everything in there since it is not related to the question.

C#
public static class ConnectionClassGuitarItems
{

public static string stringInstrumentFileName1 { get; set; }
}


After that, I would go back to the class CreateBrands and I would use that property to add it in my database. Here is another piece of the code:

C#
 protected void Button1_Click(object sender, EventArgs e)
{

        if (itemType1.Checked)
        {
        try {

            string item_image1 = ConnectionClassGuitarItems.stringInstrumentFileName1;
            string item_image2 = ConnectionClassGuitarItems.stringInstrumentFileName2;

            ConnectionClassGuitarItems.AddStringInstrumentItems(item_image1,item_image2);


And here is where i finally add it into the database:

C#
public static void AddStringInstrumentItems(string itemimage1, string 
 itemimage2)
{
    MusicStoreDBEntities obj = new MusicStoreDBEntities();

    instrumentItem s = new instrumentItem();
    s.itemimage1 = itemimage1;
    s.itemimage2 = itemimage2;

    obj.instrumentItems.Add(s);
    obj.SaveChanges();
}


That's basically the flow of the code. My problem is, the filename would always be empty. Kindly advise or give solution on this if i'm doing something wrong that is causing it to pass a filename that is empty.
Posted
Updated 11-Aug-17 22:16pm

1 solution

You can easily get the file name by writing
string filename1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
 
Share this answer
 
v2
Comments
BebeSaiyan 12-Aug-17 8:24am    
this solution is giving me error. It is under AjaxFileUploadEventArgs. Even with just EventArgs, it still have error.

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