Im developing an ASP.net project, Im Beginner for the ASP.NET, Im write the code for but its not data save for the table, i want to save all of data with uploaded image to the SQL database. but not work correctly, please help me how can i fix it.i think its problem my uploading part, please give me a image uploading example ,thanks for the help

namespace eKnittingSimple.WebForms.InquiryOrder
{
    public partial class DesignCode : System.Web.UI.Page
    {
        #region "---- Variables & ViewStates ----"

        clsCommonMethods clsComMethods = new clsCommonMethods();

        public DataTable dtDesignCodeHelp
        {
            get { return (DataTable)ViewState["dtDesignCodeHelp"]; }
            set { ViewState["dtDesignCodeHelp"] = value; }
        }

        public String CurrentMode
        {
            get { return (String)ViewState["CurrentMode"]; }
            set { ViewState["CurrentMode"] = value; }
        }

        public int SelectedDesignCodeId
        {
            get { return (int)ViewState["SelectedDesignCodeId"]; }
            set { ViewState["SelectedDesignCodeId"] = value; }
        }

        public int SelectedUserId
        {
            get { return (int)ViewState["SelectedUserId"]; }
            set { ViewState["SelectedUserId"] = value; }
        }

        public bool ViewRight
        {
            get { return (bool)ViewState["ViewRight"]; }
            set { ViewState["ViewRight"] = value; }
        }

        public bool CreateRight
        {
            get { return (bool)ViewState["CreateRight"]; }
            set { ViewState["CreateRight"] = value; }
        }

        public bool UpdateRight
        {
            get { return (bool)ViewState["UpdateRight"]; }
            set { ViewState["UpdateRight"] = value; }
        }

        #endregion

        DesignCodeCls DesignCls = new DesignCodeCls();

        #region Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
            if (!IsPostBack)
            {
                if ((Session["UserId"] == null))
                {
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/WebForms/Home/Login.aspx");
                }
                else
                {
                    SelectedUserId = int.Parse(Session["UserId"].ToString());

                    string pageName = "designcode";

                    DataTable dtUp = UserPermission(pageName);
                    if (dtUp.Rows.Count > 0)
                    {
                        ViewRight = Convert.ToBoolean(dtUp.Rows[0]["isViewable"]);
                        CreateRight = Convert.ToBoolean(dtUp.Rows[0]["isCreatable"]);
                        UpdateRight = Convert.ToBoolean(dtUp.Rows[0]["isEditable"]);
                    }

                    if (ViewRight != true)
                    {
                        Response.Redirect("~/WebForms/Home/AccessDenied.aspx");
                    }
                    else
                    {
                        SetSavePermission(CreateRight);
                        SetEditPermission(UpdateRight);
                    }
                }

                ClearControls();
                EnableControls(false);
                LoadQualityReference();

            }
        }

        private void SetSavePermission(bool EnableStatus)
        {
            btnNew.Enabled = EnableStatus;
            btnSave.Enabled = EnableStatus;
        }

        private void SetEditPermission(bool EnableStatus)
        {
            btnUpdate.Enabled = EnableStatus;
            btnSave.Enabled = EnableStatus;

        }

        #endregion

        private DataTable UserPermission(string mPageCode)
        {
            int UserId = int.Parse(Session["UserId"].ToString());
            DataTable dtPermission = new DataTable();

            dtPermission = clsComMethods.GetUserWisePermissions(UserId, mPageCode);

            return dtPermission;
        }

        #region Enable Disable Controls
        private void EnableControls(bool Status)
        {
            txtDesigncode.Enabled = Status;
            txtDesignCodename.Enabled = Status;
            ddlQlty.Enabled = Status;

            if (Status == true)
            {
                txtRemarks.Disabled = false;
            }
            else
            {
                txtRemarks.Disabled = true;
            }
            chkStatus.Enabled = Status;
            btnDesignCodehelp.Enabled = Status;
        }
        #endregion

        private void LoadQualityReference()
        {
            QualityReferenceCls objQualityReference = new QualityReferenceCls();

            DataTable dtQualityReference = objQualityReference.QualityReference_SelectAll();

            ddlQlty.DataSource = dtQualityReference;
            ddlQlty.DataTextField = "Name";
            ddlQlty.DataValueField = "ID";
            ddlQlty.DataBind();

        }



        #region Button Disable
        private void DisableButtons()
        {
            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            //btnDelete.Enabled = true;
            btnInquiry.Enabled = true;
            //btnConfirm.Enabled = false;
            btnCancel.Enabled = false;
        }
        #endregion

        #region Clear Controls
        private void ClearControls()
        {
            txtDesigncode.Text = "";
            txtDesignCodename.Text = "";
            ddlQlty.SelectedIndex = 0;
            lblMsg.Text = "";
            txtRemarks.Value = "";
            chkStatus.Checked = false;
            SelectedDesignCodeId = -1;
            //lblPRTId.Text = "";


            if (CurrentMode == "Modify")
            {
                txtDesigncode.Enabled = true;
            }
        }
        #endregion

        #region New Button Click
        protected void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                ClearControls();
                CurrentMode = "Add";
                lblMode.Text = " - New Record";
                lblMode.ForeColor = System.Drawing.Color.Yellow;
                SelectedDesignCodeId = -1;
                //btnConfirm.Enabled = false;
                btnUpdate.Enabled = false;
                //btnDelete.Enabled = false;
                btnInquiry.Enabled = false;
                btnCancel.Enabled = true;
                EnableControls(true);
                txtDesigncode.Enabled = false;
                btnDesignCodehelp.Enabled = false;
                btnSave.Enabled = true;
                btnClear.Enabled = true;
                chkStatus.Checked = true;
                txtDesignCodename.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Update Button Click
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentMode = "Modify";
                lblMode.Text = " - Modify Record";
                lblMode.ForeColor = System.Drawing.Color.Yellow;
                //btnConfirm.Enabled = false;
                btnNew.Enabled = false;
                //btnDelete.Enabled = false;
                btnInquiry.Enabled = false;
                btnCancel.Enabled = true;
                txtDesigncode.Enabled = true;
                txtDesignCodename.Enabled = false;
                ddlQlty.Enabled = false;

                txtRemarks.Disabled = true;
                chkStatus.Enabled = false;
                btnDesignCodehelp.Enabled = true;
                btnSave.Enabled = false;
                btnClear.Enabled = true;
                txtDesigncode.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Inquiry Button Click
        protected void btnInquiry_Click(object sender, EventArgs e)
        {
            try
            {
                CurrentMode = "Inquiry";
                lblMode.Text = " - Inquiry Record";
                lblMode.ForeColor = System.Drawing.Color.Yellow;
                //btnConfirm.Enabled = false;
                btnNew.Enabled = false;
                btnUpdate.Enabled = false;
                //btnDelete.Enabled = false;
                txtDesigncode.Enabled = true;
                btnDesignCodehelp.Enabled = true;
                btnSave.Enabled = false;
                btnClear.Enabled = true;
                btnCancel.Enabled = true;
                txtDesigncode.Focus();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Clear Button Click
        protected void btnClear_Click(object sender, EventArgs e)
        {
            ClearControls();
        }
        #endregion

        #region Cancel Button Click
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                //lblMode.Text = "";
                CurrentMode = "Cancel";
                btnNew.Enabled = true;
                btnInquiry.Enabled = true;
                btnUpdate.Enabled = true;
                //btnDelete.Enabled = true;
                //btnConfirm.Enabled = false;
                btnCancel.Enabled = false;
                btnSave.Enabled = false;
                btnClear.Enabled = false;
                EnableControls(false);
                ClearControls();


                SetSavePermission(CreateRight);
                SetEditPermission(UpdateRight);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Save Button Click
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //if (FSaveConfirmation() == true)
                //{
                string strDesignCode;
                int statusId;

                if (CurrentMode == "Add")
                {
                    strDesignCode = "";
                }
                else
                {
                    strDesignCode = txtDesigncode.Text.ToString();
                }

                if (chkStatus.Checked == true)
                {
                    statusId = 8;
                }
                else
                {
                    statusId = 9;
                }

                int output = DesignCls.InsertDesignCode
                    (
                    SelectedDesignCodeId,
                    strDesignCode,
                    txtDesignCodename.Text,
                    int.Parse(ddlQlty.SelectedValue),
                    (string)Session["FileName"],
                    (string)Session["FilePath"],
                    txtRemarks.Value.ToString(),
                    statusId,
                    SelectedUserId,
                    CurrentMode.ToString());

                if (output > 0)
                {
                    if (CurrentMode == "Add")
                    {
                        txtDesigncode.Text = "FDR" +  output.ToString("00000");

                        lblMsg.Text = "Successfully Saved!";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                    }
                    else if (CurrentMode == "Modify")
                    {
                        lblMsg.Text = "Successfully Updated!";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                    }
                }
                else if (output == -3)
                {
                    lblMsg.Text = "Already Exists!";
                    lblMsg.ForeColor = System.Drawing.Color.Orange;
                }
                else if (output == -1)
                {
                    lblMsg.Text = "Save Unsuccessful! Code Error!";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
                else if (output == -2)
                {
                    lblMsg.Text = "Save Unsuccessful! SP Error!";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
                //}                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region DesignCode help Button Click
        protected void btnDesignCodehelp_Click(object sender, EventArgs e)
        {
            try
            {
                lblMsg.Text = "";
                txtDesignCodename.Text = "";
                txtRemarks.Value = "";
                chkStatus.Checked = false;

                DataTable dDSC = DesignCls.FGetDesignCode(txtDesigncode.Text.ToString());

                if (dDSC.Rows.Count > 0)
                {
                    dtDesignCodeHelp = dDSC;
                    Session["Help"] = "DSC";

                    gvHelp.DataSource = dDSC;
                    gvHelp.DataBind();

                    gvHelp.HeaderRow.Cells[3].Visible = false;  //Code
                    gvHelp.HeaderRow.Cells[4].Visible = false;  //StatusId 
                    gvHelp.HeaderRow.Cells[5].Visible = false;  //Remarks
                    gvHelp.HeaderRow.Cells[6].Visible = false;  //Id

                    foreach (GridViewRow gvr in gvHelp.Rows)
                    {
                        gvr.Cells[3].Visible = false;   //KnitUnitCode
                        gvr.Cells[4].Visible = false;   //StatusId 
                        gvr.Cells[5].Visible = false;   //Remarks 
                        gvr.Cells[6].Visible = false;   //GaugeId
                    }

                    mpConfirm.Show();
                }
                else
                {
                    lblMsg.Text = "No Record Found";
                    lblMsg.ForeColor = System.Drawing.Color.Red;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Help Grid Row Command
        protected void gvHelp_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {

                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow selectedRow = gvHelp.Rows[index];

                if (Session["Help"].ToString() == "DSC")
                {
                    SelectedDesignCodeId = Convert.ToInt32(selectedRow.Cells[3].Text);

                    DataRow[] dr = dtDesignCodeHelp.Select("[DesignCodeId] = " + SelectedDesignCodeId);

                    if (CurrentMode == "Modify")
                    {
                        txtDesigncode.Enabled = false;
                        txtDesignCodename.Enabled = true;
                        chkStatus.Enabled = true;
                        txtRemarks.Disabled = false;
                        btnSave.Enabled = true;
                    }
                    else if (CurrentMode == "Inquiry")
                    {
                        txtDesigncode.Enabled = true;
                        txtDesignCodename.Enabled = false;
                        ddlQlty.Enabled = false;

                        chkStatus.Enabled = false;
                        txtRemarks.Disabled = true;
                        btnSave.Enabled = false;
                    }

                    txtDesigncode.Text = dr[0]["DesignCodeCode"].ToString();
                    txtDesignCodename.Text = dr[0]["DesignCodeName"].ToString();
                    ddlQlty.SelectedValue = (dr[0]["QualityReference"]).ToString();


                    if (Convert.ToInt32(dr[0]["StatusId"]) == 8)
                    {
                        chkStatus.Checked = true;
                    }
                    else
                    {
                        chkStatus.Checked = false;
                    }
                    txtRemarks.Value = dr[0]["Remarks"].ToString();
                }
            }
        }
        #endregion



        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

                //Save files to disk
                FileUpload1.SaveAs(Server.MapPath("~/App_Data/" + FileName));

                //Add Entry to DataBase
                String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                string strQuery = "insert into DesignCodes (FileName, FilePath) values(@FileName, @FilePath)";
                SqlCommand cmd = new SqlCommand(strQuery);
                cmd.Parameters.AddWithValue("@FileName", FileName);
                cmd.Parameters.AddWithValue("@FilePath", "~/App_Data/" + FileName);
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
                finally
                {
                    con.Close();
                    con.Dispose();
                }


            }
        }
    }
}

Related posts

Recent Viewed