Image has not been uploaded code returns.
Why Data is not save in table.
Default.aspx
<asp:Image ID="Image1" runat="server" style="border:1px solid Green; width:120px; height:120px;" /><br />
<asp:FileUpload ID="FU_Img_1" runat="server" CssClass="Button" style="display:block; width:130px;" /><br />
<asp:Button ID="btnImg1" runat="server" Text="Upload Img 1" CssClass="ButtonSave" OnClick="btnImg1_Click" />
Stored Procedure (MS SQL)
Create PROCEDURE [dbo].[Update_Product_Img_1]
(
@ProductId bigint,
@ProductCode nvarchar(50),
@Img1_Path nvarchar(120),
@Img1_Name nvarchar(120),
@Img1_Upload_UserID nvarchar(120),
@StatementType nvarchar(20) = ''
)
AS
BEGIN
IF @StatementType = 'Update'
DECLARE @DocsUploadStatus nchar(1)
DECLARE @Img1_Upload_DT datetime
BEGIN
UPDATE Product_Add
SET @Img1_Path=@Img1_Path, @Img1_Name=@Img1_Name, @Img1_Upload_UserID=@Img1_Upload_UserID, @Img1_Upload_DT=GETDATE()
WHERE ProductId = @ProductId and ProductCode = @ProductCode
END
end
protected void btnImg1_Click(object sender, System.EventArgs e)
{
////saving file in the physical folder;
string virtualFolder = "~/Uploads/Product_Imgs/";
//Set file extension for Photo and sign
string file_ext = Path.GetExtension(FU_Img_1.PostedFile.FileName).ToLower();
if (FU_Img_1.HasFile)
{
if (file_ext == ".JPEG" || file_ext == ".jpg" || file_ext == ".png" || file_ext == ".bmp")
{
FU_Img_1.SaveAs(Server.MapPath(virtualFolder) + "\\" + lblProductCode.Text + "_F" + file_ext);
string Prod_Img_1 = lblProductCode.Text + "_F" + file_ext;
string physicalFolder = Server.MapPath(virtualFolder);
Image1.Visible = true;
Image1.ImageUrl = Server.MapPath(virtualFolder) + @"\" + lblProductCode.Text + "_F" + file_ext;
cnn.Open();
SqlCommand cmd = new SqlCommand("Update_Product_Img_1", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StatementType", "Update");
cmd.Parameters.AddWithValue("@ProductId", SqlDbType.VarChar).Value = lblProductId.Text;
cmd.Parameters.AddWithValue("@ProductCode", SqlDbType.VarChar).Value = lblProductCode.Text;
cmd.Parameters.AddWithValue("@Img1_Path", SqlDbType.VarChar).Value = "";
cmd.Parameters.AddWithValue("@Img1_Name", SqlDbType.VarChar).Value = Prod_Img_1;
cmd.Parameters.AddWithValue("@Img1_Upload_UserID", SqlDbType.VarChar).Value = "";
cmd.ExecuteNonQuery();
cnn.Close();
{
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Product's Front Image for Product. - " + lblProductCode.Text + " has been uploaded successfully.');", true);
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Only Images ('.JPG, .PNG and .BMP format') can be uploaded')");
}
}
}