Sunday, 13 August 2017

Image uploading and saving its path is database.



 protected void btn_submit_Click(object sender, EventArgs e)
    {
        bool flag = true;
        image.CenterImagePath = "";
        image.CenterImageID = Convert.ToInt32(lbl_CenterImageID.Text);

        if (FileUpload1.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png" || fileExt == ".jpeg")
            {
                image.CenterImagePath = @"~/CenterImage1/" + image.CenterImageID + Class_Additionalresources.Getextention(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath(image.CenterImagePath));
                flag = true;
            }
            else
            {

                flag = false;
                lblMsg.Text = "Only jpg,gif and png file are allowed";
                div_message.Visible = true;
                img_right.Visible = false;
                img_error.Visible = true;
                lblMsg.ForeColor = System.Drawing.Color.Red;

            }

        }

        if (flag)
        {

            image.NavigateUrl = txt_url.Text;
            image.Status = chk_Status.Checked;
            image.UpdateCenterImage1ByID();
            if (image.OperationStatus)
            {
                lblMsg.Text = "Image updated Successfully";
                div_message.Visible = true;
                img_right.Visible = true;
                img_error.Visible = false;
                lblMsg.ForeColor = System.Drawing.Color.Green;
                Div_Image.Visible = true;
                div_update.Visible = false;
                bindgrid();
            }
            else
            {
                lblMsg.Text = image.ErrorMessage;
                div_message.Visible = true;
                img_right.Visible = false;
                img_error.Visible = true;
                lblMsg.ForeColor = System.Drawing.Color.Red;

            }


        }

    }

=============================================================================


    public static string Getextention(string Filename)
    {
        string _extention;
        int index;
        index = Filename.IndexOf('.');
        _extention = Filename.Substring(index);
        return _extention;

    }
=============================================================================

  public void UpdateCenterImage1ByID()
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SP_UpdateCenterImage1ByID";
            cmd.Parameters.AddWithValue("@CenterImageID", CenterImageID);
            cmd.Parameters.AddWithValue("@CenterImagePath", CenterImagePath);
            cmd.Parameters.AddWithValue("@NavigateUrl", NavigateUrl);
            cmd.Parameters.AddWithValue("@Status", Status);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Connection;
            OpenConnection();
            CenterImageID = Convert.ToInt32(cmd.ExecuteScalar());
            if (CenterImageID == -1)
            {
                OperationStatus = false;
                ErrorMessage = "Error : Image does not Exist.";
            }
            else
            {
                OperationStatus = true;
            }
        }
        catch (Exception ex)
        {
            ErrorMessage = "Error : " + ex.Message;
            OperationStatus = false;
        }
        finally
        {
            CloseConnection();
        }
    }

=============================================================================

CREATE PROCEDURE [dbo].[SP_UpdateCenterImage1ByID]                
@CenterImageID int,                
@CenterImagePath nvarchar(200),     
@NavigateUrl nvarchar(max),      
@Status bit                
as                
begin         
  if(@CenterImagePath = '')        
  update tbl_CenterImage1 set NavigateUrl=@NavigateUrl,Status = @Status               
  where CenterImageID = @CenterImageID       
  else      
  begin      
     update tbl_CenterImage1 set CenterImagePath = @CenterImagePath,NavigateUrl=@NavigateUrl,Status = @Status               
  where CenterImageID = @CenterImageID         
  end             
end 

=============================================================================

No comments:

Post a Comment