hi every body
i have a register web form where there are many textboxes and upload a photo for user it works fine when i upload photo but when i save without upload photo it gets me error how can i save data with or without upload photo ??
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 7:46 PM EDT
Regards,
Iftikar
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 27, 2013, 10:00 PM EDT
If your upload photo is not mandatory while registering the User details, then while saving the data just check like this
Note: I believe UploadPhoto filed in database is nullable
Regards,
Iftikar
alaa157 reputationPosted Jul 28, 2013, 12:53 AM EDT
if (FUpload.HasFile)
{
// do something
}
else
{
// do something
}
i ask if i can do something like this
if (FUpload.HasFile=true || FUpload.HasFile=false )
{
// my code to save data with or without photo path will run here
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 12:59 AM EDT
and share me your database design for the table where you are storing photo
Regards,
Iftikar
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 1:45 AM EDT
Regards,
Iftikar
alaa157 reputationPosted Jul 28, 2013, 1:56 AM EDT
{
string user = utility.ReadFromCookie("login", "U", Request);
UsersInfo reg = new UsersInfo();
// First we check to see if the user has selected a file
// if (FUpload.HasFile)
if(FUpload.PostedFile.ContentLength >= 0)
{
// Find the fileUpload control
string filename = FUpload.FileName;
// Check if the directory we want the image uploaded to actually exists or not
if (!Directory.Exists(MapPath(@"../Users/UsersImg/")))
{
// If it doesn't then we just create it before going any further
Directory.CreateDirectory(MapPath(@"Uploaded-Files"));
}
// Specify the upload directory
string directory = Server.MapPath(@"../Users/UsersImg/");
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(FUpload.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 160;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(directory +user+".jpg");
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
// Write a message to inform the user all is OK
string savepath = (@"../Users/UsersImg/" + user + ".jpg").ToString();
Label1.Text = "File Name: " + filename + "
";
Label1.Text += "Content Type: " + FUpload.PostedFile.ContentType + "
";
Label1.Text += "File Size: " + FUpload.PostedFile.ContentLength.ToString() + "";
// Display the image to the user
imgUser.Visible = true;
imgUser.ImageUrl = @"../Users/UsersImg/" + user +".jpg";
if (Session["addinfoarray"] != null)
{
ArrayList al2 = (ArrayList)Session["addinfoarray"];
if (reg.Register(CryptoUtil.DecryptTripleDES(user),(string)al2[0],(string)al2[1],(string)al2[2],(string)al2[3],(string)al2[4],(string)al2[5],(string)al2[6],(string)al2[7],(string)al2[8],(string)al2[9],(string)al2[10],(string)al2[11],(string)al2[12],savepath) == true)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('your data is saved')", true);
Response.Redirect("Profile.aspx?=n"+user);
}
}
} //end if 1
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('your profile isnt complete')", true);
Response.Redirect("Profile.aspx");
// Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "alert('No file uploaded!')", true);
}
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 2:16 AM EDT
I believe, here user is the user id and user will always upload .jpg file.
therefor it is not required to save any details related to photo to database.
Regards,
Iftikar
alaa157 reputationPosted Jul 28, 2013, 2:27 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 2:30 AM EDT
";
";
Regards,
Iftikar
alaa157 reputationPosted Jul 28, 2013, 2:57 AM EDT
but some imges gets me error
Attempted to divide by zero.
Line 96: int sngRatio = origWidth / origHeight;
Line 97: int newWidth = 160;
Line 98: int newHeight = newWidth / sngRatio;
whats the soulation for this error
be sure that before i post any thing i try more and raed about it to find answer ny self
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 28, 2013, 3:21 AM EDT
Regards,
Iftikar
alaa157 reputationPosted Jul 28, 2013, 5:29 AM EDT
and so how can i put soulation in code for this error using alret message or any suitable soulation