private void btnSave_Click(object sender, EventArgs e) { celbName = txtBoxCelebrityName.Text.ToString(); // selectedItem = this.cmbBoxItem.GetItemText(this.cmbBoxItem.SelectedItem); //selectedCat= this.cmbBoxCategory.GetItemText(this.cmbBoxCategory.SelectedItem); selectedCat = cmbBoxCategory.Text.ToString(); selectedItem = cmbBoxItem.Text.ToString(); //get the checked radio button var checkedButton = groupBoxObtainedBy.Controls.OfType <RadioButton>() .FirstOrDefault(rb => rb.Checked); autoObtnBy = checkedButton.Text.ToString(); //lblError.Text = checkedButton.Text.ToString(); //validation check vaildationCheck(); if (passedValidation == false) { return;//exit method } //obj creation AutographItem myItem = new AutographItem(); myItem.CelebName = celbName; myItem.SelectedCat = selectedCat; myItem.SelectedItem = selectedItem; myItem.AutoObtainedBy = autoObtnBy; myItem.PicLink = picLink; myItem.Description = descrip; myItem.DateAquired = dateAquired; myItem.AutoCost = autoCost.ToString(); myItem.ItemCost = itemCost.ToString(); //write to DB writeToDB(myItem); //open new form //ConfirmationForm cform = new ConfirmationForm(); //cform.Show(); //MessageBox.Show("**Form Saved** \n '" +myItem.CelebName +"' \nSucesfully Added To Database"); MessageBox.Show("**Form Saved** \n '" + myItem.CelebName + "' \nSucesfully Added To Database", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); //customize messagebox //https://msdn.microsoft.com/en-us/library/tewzaxwc(v=vs.110).aspx //https://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxicon(v=vs.110).aspx //close this form this.Hide(); //https://stackoverflow.com/questions/2751076/closing-a-form-and-then-call-another-one }
//Manage connection //public static void recordToDb(string celbName, string selectedItem, string selectedCat, string descrip, string dateAquired, string autoCost, string itemCost, string autoObtainedBy, string picLink) public static void recordToDb(AutographItem myItem) { string connect = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString; SqlConnection conn = new SqlConnection(connect); try { conn.Open(); //Get date and time String dateNow = DateTime.Now.ToString("M/d/yyyy h:mm:ss"); String query = "INSERT INTO dbo.NicksAutographs (CelebrityName, Item, Category, Description, DateAquired, AutographCost, ItemCost, ObtainedBy, PictureLink, DateRecoreded) VALUES (@cName,@item,@cat, @desc, @dateAq, @autoCost, @itemCost, @autoObtnBy,@picLink, @dateRec)"; using (SqlCommand comm = new SqlCommand()) { //LogFile.WriteToLog("******Query Inserted******"); // tempMonEventLog.WriteEntry("******Query Inserted******"); SqlCommand command = new SqlCommand(query, conn); command.Parameters.AddWithValue("@cName", myItem.CelebName); command.Parameters.AddWithValue("@item", myItem.SelectedItem); command.Parameters.AddWithValue("@cat", myItem.SelectedCat); command.Parameters.AddWithValue("@desc", myItem.Description); command.Parameters.AddWithValue("@dateAq", myItem.DateAquired); command.Parameters.AddWithValue("@autoCost", myItem.AutoCost); command.Parameters.AddWithValue("@itemCost", myItem.ItemCost); command.Parameters.AddWithValue("@autoObtnBy", myItem.AutoObtainedBy); command.Parameters.AddWithValue("@picLink", myItem.PicLink); command.Parameters.AddWithValue("@dateRec", dateNow); //run query command.ExecuteNonQuery(); } } finally { //close connection conn.Close(); } }
private void writeToDB(AutographItem theItem) { DBConnection.recordToDb(theItem); }