internal static void Delete(ProductContact contact, ApplicationProduct product)
        {
            string query =
                "DELETE FROM ProductContacts WHERE ContactId = @ContactId AND ProductId = @ProductId; " +
                "DELETE FROM Contacts WHERE ContactId = @ContactId; ";

            bool ok = false;

            using (SqlConnection cnn = new SqlConnection(Properties.Settings.Default.PersianSoftwareConnectionString))
            {
                SqlTransaction transaction = null;
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("@ProductId", product.ProductId));
                    cmd.Parameters.Add(new SqlParameter("@ContactId", contact.Id));

                    try
                    {
                        foreach (SqlParameter Parameter in cmd.Parameters)
                        {
                            if (Parameter.Value == null)
                            {
                                Parameter.Value = DBNull.Value;
                            }
                        }
                        cnn.Open();
                        transaction     = cnn.BeginTransaction();
                        cmd.Transaction = transaction;

                        int affected = cmd.ExecuteNonQuery();
                        if (affected >= 2)
                        {
                            ok = true;
                        }

                        if (ok)
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        System.Diagnostics.Debug.WriteLine(string.Format("Exception:{0}", ex));
                    }

                    cnn.Close();
                }
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            ProductContact selected = this.dgvContacts.SelectedItem as ProductContact;

            if (selected == null)
            {
                throw new InvalidOperationException("Please selet a contact to remove");
            }

            if (MessageBox.Show("Are you sure you want to delete this record permanently?", "Confirm permanent remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                Repository.Sql.ProductContacts.Delete(selected, this.product);
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            ProductContact selected = this.dgvContacts.SelectedItem as ProductContact;

            if (selected == null)
            {
                throw new InvalidOperationException("Please selet a contact to edit");
            }

            using (FrmContactEditor frm = new FrmContactEditor(selected))
            {
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Repository.Sql.ProductContacts.Update(frm.Contact);
                }
            }
        }
        internal static void Reload(ApplicationProduct product)
        {
            string query = string.Format(
                "SELECT Contacts.ContactId, ContactValue, ContactPerson, UnitTitleName, Contacts.ContactMediaId, ContactUnitTitleId, ListContactMedias.ContactMediaName " +
                "FROM Contacts, ListContactUnitTitles, ListContactMedias " +
                "WHERE Contacts.ContactId IN (SELECT ContactId FROM ProductContacts WHERE ProductId = {0}) " +
                "AND Contacts.ContactMediaId = ListContactMedias.ContactMediaId AND " +
                "ListContactUnitTitles.UnitTitleId = Contacts.ContactUnitTitleId AND" +
                " ListContactUnitTitles.LanguageId = {1} AND ListContactMedias.LanguageId = {1}",
                product.ProductId, product.ArticleLanguage);

            using (SqlConnection cnn = new SqlConnection(Properties.Settings.Default.PersianSoftwareConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader != null && reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ProductContact contact = new ProductContact();
                                contact.Id             = Repository.Utils.Convert.ToInt64(reader[0]);
                                contact.ContactValue   = Repository.Utils.Convert.ToString(reader[1]);
                                contact.ContactPerson  = Repository.Utils.Convert.ToString(reader[2]);
                                contact.Unit.Name      = Repository.Utils.Convert.ToString(reader[3]);
                                contact.MediaType.Id   = Repository.Utils.Convert.ToInt32(reader[4]);
                                contact.Unit.Id        = Repository.Utils.Convert.ToInt32(reader[5]);
                                contact.MediaType.Name = Repository.Utils.Convert.ToString(reader[6]);

                                product.ProductContacts.Add(contact);
                            }

                            reader.Close();
                        }
                    }

                    cnn.Close();
                }
            }
        }
        internal static bool Update(ProductContact contact)
        {
            string query =
                "UPDATE Contacts " +
                "SET ContactUnitTitleId=@ContactUnitTitleId, ContactMediaId=@ContactMediaId, ContactValue=@ContactValue, ContactPerson=@ContactPerson " +
                "WHERE ContactId=@ContactId; ";

            bool ok = false;

            using (SqlConnection cnn = new SqlConnection(Properties.Settings.Default.PersianSoftwareConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add(new SqlParameter("@ContactId", contact.Id));
                    cmd.Parameters.Add(new SqlParameter("@ContactUnitTitleId", contact.Unit.Id));
                    cmd.Parameters.Add(new SqlParameter("@ContactMediaId", contact.MediaType.Id));
                    cmd.Parameters.Add(new SqlParameter("@ContactValue", contact.ContactValue));
                    cmd.Parameters.Add(new SqlParameter("@ContactPerson", contact.ContactPerson));
                    foreach (SqlParameter Parameter in cmd.Parameters)
                    {
                        if (Parameter.Value == null)
                        {
                            Parameter.Value = DBNull.Value;
                        }
                    }

                    cnn.Open();

                    int affected = cmd.ExecuteNonQuery();
                    if (affected > 0)
                    {
                        ok = true;
                    }

                    cnn.Close();
                }
            }

            return(ok);
        }
        public FrmContactEditor(ProductContact contact)
        {
            this.Contact = contact;

            InitializeComponent();

            this.cbxListContactMedia.DataSource    = Repository.Sql.ProductContacts.GetMediaList(false);
            this.cbxListContactMedia.DisplayMember = "Name";
            this.cbxListContactMedia.ValueMember   = "Id";

            this.cbxListContactUnits.DataSource    = Repository.Sql.ProductContacts.GetUnitList(false);
            this.cbxListContactUnits.DisplayMember = "Name";
            this.cbxListContactUnits.ValueMember   = "Id";

            this.tbxContactPerson.DataBindings.Add(new Binding("Text", this.Contact, "ContactPerson"));
            this.tbxContactValue.DataBindings.Add(new Binding("Text", this.Contact, "ContactValue"));

            this.cbxListContactMedia.DataBindings.Add(new Binding("SelectedValue", this.Contact.MediaType, "Id"));
            this.cbxListContactUnits.DataBindings.Add(new Binding("SelectedValue", this.Contact.Unit, "Id"));

            this.rtnNewContact.Checked = true;
            UpdateControls();
        }
        internal static bool Insert(ProductContact contact, ApplicationProduct product)
        {
            string query =
                "DECLARE @Id BIGINT " +
                "SET @Id = -1 " +
                "INSERT INTO Contacts " +
                "(ContactUnitTitleId, ContactMediaId, ContactValue, ContactPerson) " +
                "VALUES " +
                "(@ContactUnitTitleId, @ContactMediaId, @ContactValue, @ContactPerson);" +
                "SELECT @Id = SCOPE_IDENTITY(); " +
                "SELECT @Id; ";

            bool ok = false;

            using (SqlConnection cnn = new SqlConnection(Properties.Settings.Default.PersianSoftwareConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    SqlTransaction transaction = null;
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("@ContactUnitTitleId", contact.Unit.Id));
                    cmd.Parameters.Add(new SqlParameter("@ContactMediaId", contact.MediaType.Id));
                    cmd.Parameters.Add(new SqlParameter("@ContactValue", contact.ContactValue));
                    cmd.Parameters.Add(new SqlParameter("@ContactPerson", contact.ContactPerson));
                    foreach (SqlParameter Parameter in cmd.Parameters)
                    {
                        if (Parameter.Value == null)
                        {
                            Parameter.Value = DBNull.Value;
                        }
                    }

                    try
                    {
                        cnn.Open();
                        transaction     = cnn.BeginTransaction();
                        cmd.Transaction = transaction;

                        object res = cmd.ExecuteScalar();
                        if (res != null && res != DBNull.Value)
                        {
                            contact.Id = Convert.ToInt64(res);
                            if (InsertProductContact(contact.Id, product.ProductId, cnn, transaction))
                            {
                                ok = true;
                            }
                        }

                        if (ok)
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        System.Diagnostics.Debug.WriteLine(string.Format("Exception:{0}", ex));
                    }

                    cnn.Close();
                }
            }

            return(ok);
        }
Example #8
0
        public static bool Load(string productUrlName, string cultureId, ApplicationProduct product, Int64?userId)
        {
            bool res = false;

            string query = "CatalogLoad";

            using (SqlConnection cnn = new SqlConnection(Configurations.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(query, cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UrlName", productUrlName));
                    cmd.Parameters.Add(new SqlParameter("@CultureId", cultureId));
                    cmd.Parameters.Add(new SqlParameter("@UserId", userId));

                    foreach (SqlParameter Parameter in cmd.Parameters)
                    {
                        if (Parameter.Value == null)
                        {
                            Parameter.Value = DBNull.Value;
                        }
                    }

                    cnn.Open();

                    SqlDataReader reader = cmd.ExecuteReader();
                    if (reader != null && reader.HasRows)
                    {
                        // Read first table: SoftwareProduct
                        if (reader.Read())
                        {
                            product.ProductId          = Repository.Utils.Convert.ToInt64(reader[0]);
                            product.ProductWebsite     = Repository.Utils.Convert.ToString(reader[1]);
                            product.ProductVersion     = Repository.Utils.Convert.ToString(reader[2]);
                            product.ProductReleaseDate = Repository.Utils.Convert.ToDateTime(reader[3]);
                            product.Price              = Repository.Utils.Convert.ToDecimal(reader[4]);
                            product.MinimumVolumeSize  = Repository.Utils.Convert.ToFloat(reader[5]);
                            product.MultiUser          = Repository.Utils.Convert.ToBool(reader[6]);
                            product.MultiLanguage      = Repository.Utils.Convert.ToBool(reader[7]);
                            product.LanguageExtendable = Repository.Utils.Convert.ToBool(reader[8]);
                        }

                        // Read next table: Details
                        reader.NextResult();
                        while (reader.Read())
                        {
                            product.ProductName      = Repository.Utils.Convert.ToString(reader[0]);
                            product.BriefDescription = Repository.Utils.Convert.ToString(reader[1]);
                            product.PriceDetails     = Repository.Utils.Convert.ToString(reader[2]);
                            product.GuarantyDetails  = Repository.Utils.Convert.ToString(reader[3]);
                        }

                        // Read next table: Brands
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductBrand brand = new ProductBrand();
                            //brand.BrandId = ;
                            brand.BrandName = Utils.Convert.ToString(reader[0]);

                            product.Brands.Add(brand);
                        }

                        // Read next table: Catalog
                        reader.NextResult();
                        if (reader.Read())
                        {
                            product.Catalog.SearchPriority = Repository.Utils.Convert.ToInt32(reader[0]);
                            product.Catalog.UserRating     = Repository.Utils.Convert.ToDecimal(reader[1]);
                            product.Catalog.EditorRating   = Repository.Utils.Convert.ToDecimal(reader[2]);
                            product.Catalog.UrlName        = Repository.Utils.Convert.ToString(reader[3]);
                            product.Catalog.ViewsCount     = Repository.Utils.Convert.ToInt32(reader[4]);
                            product.Catalog.UpdateDate     = Repository.Utils.Convert.ToDateTime(reader[5]);
                            product.Catalog.InsertDate     = Repository.Utils.Convert.ToDateTime(reader[6]);
                        }


                        // UNDONE:
                        // Read next table: Categories
                        reader.NextResult();

                        // Read next table: Contacts
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductContact   contact = new ProductContact();
                            ContactUnit      unit    = new ContactUnit();
                            ContactMediaType media   = new ContactMediaType();

                            contact.Unit      = unit;
                            contact.MediaType = media;

                            contact.Id            = Repository.Utils.Convert.ToInt64(reader[0]);
                            unit.Id               = Repository.Utils.Convert.ToInt32(reader[1]);
                            unit.Name             = Repository.Utils.Convert.ToString(reader[2]);
                            contact.ContactValue  = Repository.Utils.Convert.ToString(reader[3]);
                            contact.ContactPerson = Repository.Utils.Convert.ToString(reader[4]);
                            media.Id              = Repository.Utils.Convert.ToInt32(reader[5]);
                            media.Name            = Repository.Utils.Convert.ToString(reader[6]);

                            product.ProductContacts.Add(contact);
                        }


                        // Read next table: Customization
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductCustomizationOption customize = new ProductCustomizationOption();

                            customize.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            customize.Name = Repository.Utils.Convert.ToString(reader[1]);

                            product.CustomizationOptions.Add(customize);
                        }



                        // Read next table: Backup
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductDataBackupOption backup = new ProductDataBackupOption();
                            backup.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            backup.Name = Repository.Utils.Convert.ToString(reader[1]);;

                            product.BackupOptions.Add(backup);
                        }

                        // Read next table: Demo
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductDemoOption demo = new ProductDemoOption();

                            demo.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            demo.Name = Repository.Utils.Convert.ToString(reader[1]);;

                            product.DemoOptions.Add(demo);
                        }

                        // Read next table: Environment
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductEnvironmentOption env = new ProductEnvironmentOption();
                            env.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            env.Name = Repository.Utils.Convert.ToString(reader[1]);;

                            product.EnvironmentOptions.Add(env);
                        }

                        // Read next table: Extension
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductExtensionOptions ext = new ProductExtensionOptions();
                            ext.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            ext.Name = Repository.Utils.Convert.ToString(reader[1]);;

                            product.ExtensionOptions.Add(ext);
                        }

                        // Read next table: Guaranty
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductGuarantyOption guaranty = new ProductGuarantyOption();
                            guaranty.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            guaranty.Name = Repository.Utils.Convert.ToString(reader[1]);

                            product.GuarantyOptions.Add(guaranty);
                        }

                        // Read next table: Requirements
                        reader.NextResult();
                        while (reader.Read())
                        {
                            product.HardwareRequirements.Add(Repository.Utils.Convert.ToString(reader[1]));
                        }


                        // Read next table: Installation
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductInstallationOption inst = new ProductInstallationOption();

                            inst.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            inst.Name = Repository.Utils.Convert.ToString(reader[1]);

                            product.InstallationOptions.Add(inst);
                        }


                        // Read next table: Languages
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductLanguage lang = new ProductLanguage();
                            lang.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            lang.Name = Repository.Utils.Convert.ToString(reader[1]);

                            product.SupportedLanguages.Add(lang);
                        }

                        // Read next table: Payment
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductPaymentOption payment = new ProductPaymentOption();
                            payment.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            payment.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.PaymentOptions.Add(payment);
                        }

                        // Read next table: SoftwarePlatforms
                        reader.NextResult();
                        while (reader.Read())
                        {
                            SoftwarePlatform platform = new SoftwarePlatform();
                            platform.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            platform.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.SupportedPlatforms.Add(platform);
                        }

                        // Read next table: Publish
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductPublishOption publish = new ProductPublishOption();
                            publish.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            publish.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.PublishOptions.Add(publish);
                        }


                        // Read next table: Source
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductSourceOption source = new ProductSourceOption();
                            source.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            source.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.SourceOptions.Add(source);
                        }

                        // Read next table: Support
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductSupportOption support = new ProductSupportOption();
                            support.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            support.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.SupportOptions.Add(support);
                        }

                        // Read next table: SupportTypes
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductSupportType type = new ProductSupportType();
                            type.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            type.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.SupportTypes.Add(type);
                        }

                        // Read next table: Tags
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductTag tag = new ProductTag();
                            tag.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            tag.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.Tags.Add(tag);
                        }

                        // Read next table: Technologies
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductTechnology tech = new ProductTechnology();
                            tech.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            tech.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.ProductTechnologies.Add(tech);
                        }

                        // Read next table: Update
                        reader.NextResult();
                        while (reader.Read())
                        {
                            ProductUpdateOption update = new ProductUpdateOption();
                            update.Id   = Repository.Utils.Convert.ToInt32(reader[0]);
                            update.Name = Repository.Utils.Convert.ToString(reader[1]);
                            product.UpdateOptions.Add(update);
                        }

                        // Read next table: Update
                        reader.NextResult();
                        if (reader.Read())
                        {
                            product.Article.Content = Repository.Utils.Convert.ToString(reader[0]);
                        }

                        // Read next table: VoteCounts
                        reader.NextResult();
                        if (reader.Read())
                        {
                            product.Catalog.VoteCounts = Repository.Utils.Convert.ToInt32(reader[0]);
                        }

                        // Read next table: User vote
                        reader.NextResult();
                        if (reader.Read())
                        {
                            product.Catalog.CurrentUserRating = Repository.Utils.Convert.ToInt32(reader[0]);
                        }


                        res = true;
                    }

                    cnn.Close();
                }
            }

            return(res);
        }