SqlCommand CreateUpdateCommand() { var updateCommand = new SqlCommand( @"IF NOT EXISTS(SELECT ShippingMethodGUID FROM ShippingMethod WHERE ShippingMethodID = @ID) BEGIN INSERT INTO ShippingMethod(ShippingMethodGUID, [Name], DisplayName, ImageFileName) VALUES (@GUID, @Name, @DisplayName, @ImageFileName) SELECT @ID = ShippingMethodID FROM ShippingMethod WHERE ShippingMethodGUID = @GUID END ELSE BEGIN DECLARE @ISRtShipping tinyint SELECT @IsRtShipping = IsRTShipping FROM ShippingMethod WHERE ShippingMethodID = @ID UPDATE ShippingMethod SET [Name] = CASE WHEN @IsRtShipping = 1 Then [Name] ELSE @Name END, DisplayName = @DisplayName, ImageFileName = @ImageFileName WHERE ShippingMethodID = @ID END" ); updateCommand.Parameters.AddRange(new SqlParameter[] { new SqlParameter("@GUID", ShippingMethodGuid), new SqlParameter("@Name", NameLocaleField.GetTextFromFields()), new SqlParameter("@DisplayName", DisplayNameLocaleField.GetTextFromFields()), new SqlParameter("@ImageFileName", SqlDbType.Text) { Value = !string.IsNullOrEmpty(hdnImageFileName.Value) ? (object)hdnImageFileName.Value : DBNull.Value }, new SqlParameter("@ID", SqlDbType.Int) { Value = DBNull.Value, Direction = ParameterDirection.InputOutput } }); return(updateCommand); }
protected bool SaveShippingMethod() { bool saved = true; try { if (!Editing) { // ok to add: ShippingMethodGuid = new Guid(DB.GetNewGUID()); using (var updateCommand = CreateUpdateCommand()) { DB.ExecuteSQL(updateCommand); ShippingMethodID = (int)updateCommand.Parameters["@ID"].Value; } ViewState["ShippingMethodID"] = ShippingMethodID; } else { // ok to update: using (var updateCommand = CreateUpdateCommand()) { updateCommand.Parameters["@ID"].Value = ShippingMethodID; DB.ExecuteSQL(updateCommand); } } // for the store mapping if (ShippingMethodStoreFilteringEnabled) { DB.ExecuteSQL("DELETE ShippingMethodStore WHERE ShippingMethodId = @shippingMethodId", new[] { new SqlParameter("@shippingMethodId", ShippingMethodID) }); foreach (ListItem item in MappedStores.Items) { if (item.Selected) { DB.ExecuteSQL("INSERT INTO ShippingMethodStore(StoreId, ShippingMethodId) Values(@storeId, @shippingMethodId)", new[] { new SqlParameter("@storeId", item.Value), new SqlParameter("@shippingMethodId", ShippingMethodID), }); } } } BindPage(); NameLocaleField.BindData(); DisplayNameLocaleField.BindData(); AlertMessage.PushAlertMessage("Updated", AspDotNetStorefrontControls.AlertMessage.AlertType.Success); } catch (Exception exception) { AlertMessage.PushAlertMessage(exception.Message, AspDotNetStorefrontControls.AlertMessage.AlertType.Error); saved = false; } return(saved); }