I get this error randomly, sometimes it shows and sometimes not!
The Error:
Now it disappeared :S! but it will appear within the next hours!
Simply, It states that the parametrized query need a parameter ‘@PorfilePic’ which was not supplied, although that it’s supplied and the website works locally and also works on the server. I don’t change the code, the provider doesn’t change any settings.
I was told that I exceed my memory limit for the AppPool which is 250 MB but I think it should it appear once I exceed and after the pool rests it should work normally. but this is was the case a week ago. now it just works randomly for hours and randomly shows this error! so I hope maybe someone got the same error and could help to figure it out.
Btw, this is a shared windows hosting account. and I spoke with my providers, Honestly I think they are good in everything (Seriosuly) but in everything other than finding this specific error! 😀
The Code:
.Aspx Page:
<li>
Profile Picture:
<asp:FileUpload ID="ProfilePic_Upld" runat="server" ViewStateMode="Enabled" /><br />
</li>
Code Behind File:
if (ProfilePic_Upld.HasFile)
{
string fileType = ProfilePic_Upld.PostedFile.ContentType;
if (fileType == "image/bmp" || fileType == "image/gif" || fileType == "image/jpeg" || fileType == "image/png")
{
if (ProfilePic_Upld.PostedFile.ContentLength <= 10485760)
{
string ppFileName = ProfilePic_Upld.PostedFile.FileName;
string savedFileName = newCruise.Name + "_pp" + ppFileName.Substring(ppFileName.IndexOf('.'));
string cruisePpUploadPath = Server.MapPath(ConfigurationManager.AppSettings["cruisesPpUploadPath"].ToString());
ProfilePic_Upld.SaveAs(cruisePpUploadPath + savedFileName);
newCruise.ProfilePic = savedFileName;
}
}
}
else
{
newCruise.ProfilePic = "default.jpeg";
}
Web.config (ApplicationSettings Part):
<appSettings>
<add key="cruisesPpUploadPath" value="~\resouces\cruises\cruise_pp\" />
<add key="cruisesDpUploadPath" value="~\resouces\cruises\cruise_dp\" />
<add key="cruisesClUploadPath" value="~\resouces\cruises\cruise_cl\" />
<add key="cruiseItinerariesPpUploadPath" value="~\resouces\cruises\itineraries\iti_pp\" />
<add key="cruiseItinerariesMapsUploadPath" value="~\resouces\cruises\itineraries\iti_maps\" />
<add key="cruiseReviewDocumentsUploadPath" value="~\resouces\cruises\reviews\" />
</appSettings>
Cruise Class (newCruise)
public class Cruise
{
public int ID
{ get; set; }
public string Name
{ get; set; }
public int CountryID
{ get; set; }
public string ProfilePic
{ get; set; }
public string Description
{ get; set; }
public int OfficialRate
{ get; set; }
}
DAL Methode for creating new cruise
public static int AddNewCruise(Cruise NewCruise)
{
string commandText = "INSERT INTO Cruises (Name, CountryID, ProfilePic, Description, OfficialRate) VALUES (@Name, @CountryID, @ProfilePic, @Description, @OfficialRate); SELECT scope_identity();";
int newCruiseId;
Cruise newCruise = NewCruise;
string name = newCruise.Name;
int countryId = newCruise.CountryID;
string profilePic = newCruise.ProfilePic;
string desc = newCruise.Description;
int officialRate = newCruise.OfficialRate;
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
command.Parameters.AddWithValue("@Name", name);
command.Parameters.AddWithValue("@CountryID", countryId);
command.Parameters.AddWithValue("@ProfilePic", profilePic);
command.Parameters.AddWithValue("@Description", desc);
command.Parameters.AddWithValue("@OfficialRate", officialRate);
connection.Open();
newCruiseId = Convert.ToInt32(command.ExecuteScalar());
return newCruiseId;
}
}
}
N.B I’m running out of my deadline, so if anyone think that this is a problem with the server I hope you could suggest some good windows shared hosting providers.
N.B (2) The pictures that I upload doesn’t exceed 1 MB!
N.B (3) Guys, please this is a very important note. The website is working without any errors right now, and however you’re right about handling the uploaded file (which btw I updated to include else blocks for the Size and File Type if conditions by providing an error message and then return;) but the actual problem is that I use the same code, same website, same server and iis settings. so same application, and I also update the same image but I get the error randomly. this can’t be about the file handling! I upload a jpg image of size 105 KB. one day it works, the other day it won’t! so however you’re totally right about handling the file and I already followed your advice as this is a defect in my system, but this is not the cause of the problem. I really wish it could be that simple!
Conclusion:
First of all the problem was that I’m testing when developing on my personal PC where I use Firefox, Then Me and the client follow up on the work from our business PCs which uses Internet Explorer. the problem was in not handling the two conditions for the size and the type, so I don’t accept the uploaded file but I still call the database methode, so this was the first step. then I just followed the advise and returned whenever any of the conditions are not met, so I upload the same picture to two different instances of my application (one opened from IE and another from FireFox). Firfox uploads fine but IE now display type error. I did a little research and turns out that IE receive jpg and png as progressive types (PJPEG and X-PNG) so by adding in my type condition image/jpeg and image/x-png everything works fine.
Thanks for your time, Much appreciated =). I hope this question helps, I don’t if this is a famous thing in IE but for me I think it’s weird and I believe that other people are looking into the same issue!
It states that the parametrized query need a parameter '@PorfilePic' which was not supplied1This to me, indicates that you are storing the name/path of the uploaded file to a database, and the value it is attempting to insert (at that time) is a null string.
Edit
Looking at the code, if the file type or file size conditions are not met, then it is possible that
newCruise.ProfilePicwill not be set, resulting in a possible null reference.(It’s hard to tell without the code for the
newCruiseclass.)Edit
Based on your DAL code. It is definitely possible for what I describe above to occur.
If the file type does not match your conditions or the file size is too large, the code will cause the exception mentioned. (If, it calls the
AddNewCruisemethod regardless of the out come of the conditions)I suggest performing some validation on the
Cruiseobject before persisting it.At the very least you should not be invoking
AddNewCruisemethod, if the file type and size conditions above have not been satisfied, without providing some default values (such asnewCruise.ProfilePic = "default.jpeg";).