Ejemplo n.º 1
0
        ///<summary>Add a hardware into the database
        ///with the filled information</summary>
        ///<remarks>NULL in the database (table) will be overwritten with a empty string
        ///</remarks>
        protected void Submit_Click(object sender, EventArgs e)
        {
            String strSerialNr             = Serialnr.Text.ToString();
            String strWarrantyInfo         = warrantyInfo.Text.ToString();
            String strInternalNr           = internalNr.Text.ToString();
            String strExtraInfo            = extraInfo.Text.ToString();
            String mImagePath              = Testlocation.Text.ToString();
            String mAttPath                = TestlocationAtt.Text;
            String strModel                = modelNr.Text;
            int    intSelectedTypeIndex    = typeList.SelectedIndex + 1;
            String strSelectedManufacturer = manufacturerList.SelectedItem.ToString();
            String dtePurchaseYear         = txtDatepicker.Text.Substring(6);
            String dtePurchaseDay          = txtDatepicker.Text.Substring(0, 2);
            String dtePurchaseMonth        = txtDatepicker.Text.Substring(3, 2);
            //dtePurchaseDate converts the datepicker to database usable date
            String   dtePurchaseDate = dtePurchaseYear + '-' + dtePurchaseMonth + '-' + dtePurchaseDay;
            String   dteAddedDate    = DateTime.Now.ToString("yyyy-MM-dd");
            DateTime addedDate       = DateTime.Today;
            DateTime purchaseDate    = new DateTime(int.Parse(dtePurchaseYear), int.Parse(dtePurchaseMonth), int.Parse(dtePurchaseDay), 0, 0, 0);
            int      result          = DateTime.Compare(purchaseDate, addedDate);

            if (result > 0)
            {
                dtePurchaseDate = dteAddedDate;
            }
            try
            {
                if (mImagePath == "" || mImagePath == null)
                {
                    ViewState["pictureLocation"] = "";
                }
                else
                {
                    ViewState["pictureLocation"] = ViewState["timeStampAddedHardware"] + mImagePath;
                }
                if (TestlocationAtt.Text == "" || TestlocationAtt.Text == null)
                {
                    ViewState["attachmentLocation"] = "";
                }
                else
                {
                    ViewState["attachmentLocation"] = ViewState["timeStampAddedHardware"] + TestlocationAtt.Text;
                }
                txtResultUpload.Text = "Congratulations! The device with a internal nr: " + "<span style=\"color:red\">" + strInternalNr + "</span>" +
                                       " and a serial nr: " + "<span style=\"color:red\">" + strSerialNr + "</span>" + " successfully added to the database.";
                var hardware = new Hardware(dteAddedDate, ViewState["attachmentLocation"].ToString(), strExtraInfo, strInternalNr, strSelectedManufacturer, strModel, ViewState["pictureLocation"].ToString(),
                                            dtePurchaseDate, strSerialNr, strWarrantyInfo, typeList.SelectedValue);
                hardware.AddHardwareIntoDatabase();
                viewJustAddedHardware();
            }
            catch (MySqlException ex)
            {
                if (ex.Number.ToString() == "1062")
                {
                    txtResultUpload.Text = "The device with a internal nr: " + "<span style=\"color:red\">" + strInternalNr + "</span>" +
                                           " and a serial nr: " + "<span style=\"color:red\">" + strSerialNr + "</span>" + " already exist in de database.";
                }
                else
                {
                    ShowMessage(ex.Message);
                }
            }
            addHardwarePanel.Visible = false;
            addResultPanel.Visible   = true;
        }