Answered by:
jpg extension get change in sql after inserting

Question
-
User-367318540 posted
i am inserting path of image,after inserting path into database then extension get change like (~/images/77.jpg163727) ,so how to resolve this issue below is my code
aspx code
protected void Button1_Click(object sender, EventArgs e) { float c = Convert.ToInt64(TextBox3.Text); product p = new product(); p.pro_name = TextBox1.Text; p.pro_date = TextBox2.Text; p.pro_price = c; p.pro_img = img_upload(FileUpload1, Session["admin_id"].ToString()); p.pro_catid_fk = Convert.ToInt32(DropDownList1.SelectedValue.ToString()); p.pro_adminid_fk = Convert.ToInt32(Session["admin_id"].ToString()); insert i = new insert(); string k = i.insert_tblproduct(p); if (k == "-1") { Label2.Text = "TRY AGAIN ...."; } else { Label2.Text = "Ad successfully posted!"; } } public string img_upload(FileUpload FileUpload1, string id) { string s = " "; if (FileUpload1.HasFile) { // Get the file extension string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName); if (fileExtension.ToLower() != ".png" && fileExtension.ToLower() != ".jpg") { Label2.ForeColor = System.Drawing.Color.Red; Label2.Text = "Only files with .doc and .docx extension are allowed"; } else { // Get the file size int fileSize = FileUpload1.PostedFile.ContentLength; // If file size is greater than 2 MB if (fileSize > 2097152) { Label2.ForeColor = System.Drawing.Color.Red; Label2.Text = "File size cannot be greater than 2 MB"; } else { Random r = new Random(); int x = r.Next(0, 100000); s = "~/images/" + FileUpload1.FileName + id.ToString() + x.ToString(); // Upload the file FileUpload1.SaveAs(Server.MapPath(s)); Label2.ForeColor = System.Drawing.Color.Green; Label2.Text = "File uploaded successfully"; } } } else { Label2.ForeColor = System.Drawing.Color.Red; Label2.Text = "Please select a file"; } return s; } } }
Class code
public string insert_tblproduct(product p) { string k = "1"; SqlConnection con = new SqlConnection(cs); try { SqlCommand cmd = new SqlCommand("sp_insert_product", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@pro_image", SqlDbType.NVarChar).Value = p.pro_img; cmd.Parameters.Add("@pro_name", SqlDbType.NVarChar, 50).Value = p.pro_name; cmd.Parameters.Add("@pro_posted_date", SqlDbType.Date).Value = p.pro_date; cmd.Parameters.Add("@pro_price", SqlDbType.Float).Value = p.pro_price; cmd.Parameters.Add("@pro_fk_ad", SqlDbType.Int).Value = p.pro_adminid_fk; cmd.Parameters.Add("@pro_fk_cat", SqlDbType.Int).Value = p.pro_catid_fk; con.Open(); cmd.ExecuteNonQuery(); } catch (Exception) { k = "-1"; } finally { con.Close(); } return k; } }
Tuesday, October 1, 2019 6:46 PM
Answers
-
User475983607 posted
i am inserting path of image,after inserting path into database then extension get change like (~/images/77.jpg163727) ,so how to resolve this issue below is my codeThe code clearly appends two string to the end of the file name.
s = "~/images/" + FileUpload1.FileName + id.ToString() + x.ToString();
Perhaps try using the Visual Studio debugger.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 1, 2019 7:00 PM -
User-1716253493 posted
string filenameonly = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName); string extension = System.IO.Path.GetExtension(FileUpload1.FileName); s = "~/images/" + filenameonly + id.ToString() + x.ToString()+extension;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 2, 2019 12:41 AM -
User288213138 posted
Hi akhterr,
s = "~/images/" + FileUpload1.FileName + id.ToString() + x.ToString();
You can check the value of 's' by debugging your code. The 163727 should be composed of id and random number x.
The FileUpload1.FileName includes its name and extension.
Solution you can refer to oned_gk's answer.
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 2, 2019 2:46 AM
All replies
-
User475983607 posted
i am inserting path of image,after inserting path into database then extension get change like (~/images/77.jpg163727) ,so how to resolve this issue below is my codeThe code clearly appends two string to the end of the file name.
s = "~/images/" + FileUpload1.FileName + id.ToString() + x.ToString();
Perhaps try using the Visual Studio debugger.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 1, 2019 7:00 PM -
User-1716253493 posted
string filenameonly = System.IO.Path.GetFileNameWithoutExtension(FileUpload1.FileName); string extension = System.IO.Path.GetExtension(FileUpload1.FileName); s = "~/images/" + filenameonly + id.ToString() + x.ToString()+extension;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 2, 2019 12:41 AM -
User288213138 posted
Hi akhterr,
s = "~/images/" + FileUpload1.FileName + id.ToString() + x.ToString();
You can check the value of 's' by debugging your code. The 163727 should be composed of id and random number x.
The FileUpload1.FileName includes its name and extension.
Solution you can refer to oned_gk's answer.
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 2, 2019 2:46 AM