Beispiel #1
0
        protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataSet cardsDS       = (DataSet)Cache["Cards"];
            int     selectedIndex = e.RowIndex;
            string  temp          = magicCardsGridView.Rows[selectedIndex].Cells[0].Text;
            int     cardIds       = int.Parse(temp);
            DataRow row           = cardsDS.Tables["AllMagicCards"].Rows.Find(cardIds);

            for (int i = 1; i < magicCardsGridView.Rows[e.RowIndex].Cells.Count - 4; i++)
            {
                row[i] = ((TextBox)magicCardsGridView.Rows[e.RowIndex].Cells[i].Controls[0]).Text;
            }
            string     filename  = "";
            FileUpload cardImage = (FileUpload)magicCardsGridView.Rows[e.RowIndex].FindControl("cardImageFileUpload");

            if (cardImage.PostedFile.FileName != "")
            {
                filename = Path.GetFileName(cardImage.FileName);
                cardImage.PostedFile.SaveAs(Server.MapPath(filename));
            }
            CardType card = new CardType(row[1].ToString(), row[2].ToString(), int.Parse(row[3].ToString()), 1, filename);

            cardsService = new CardsService(card);
            int    rowUpdated;
            string cardName = row["cardName"].ToString();

            if (cardName.Length != 0)
            {
                if (!cardsService.CardAlreadyExists(cardIds, cardName))
                {
                    rowUpdated = cardsService.UpdateTableRow(int.Parse(row[0].ToString()));
                }
                else
                {
                    Response.Write("<script>alert('name already taken');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('name can't be empty');</script>");
            }
            magicCardsGridView.EditIndex  = -1;
            magicCardsGridView.DataSource = cardsService.GetAllCards();
            magicCardsGridView.DataBind();
        }
        protected void insertClick(object sender, EventArgs e)
        {
            //try
            //{
            if (IsValid)
            {
                if (cardImage.PostedFile.FileName == "")
                {
                    Response.Write("<script>alert('File has no name');</script>");
                }
                else
                {
                    string filename = Path.GetFileName(cardImage.FileName);
                    cardImage.PostedFile.SaveAs(Server.MapPath(filename));
                    CardType     card          = new CardType(cardNameTextBox.Text, abilityTextBox.Text, int.Parse(manaCostTextBox.Text), int.Parse(raritiesDropDownList.SelectedValue), filename);
                    CardsService insertService = new CardsService(card);
                    if (!insertService.CardAlreadyExists())
                    {
                        if (insertService.InsertCard() > 0)
                        {
                            Response.Write("<script>alert('Successfully inserted');</script>");
                            int cardId = insertService.LastCardId();
                            foreach (ListItem item in colorsList.Items)
                            {
                                if (item.Selected)
                                {
                                    if (insertService.InsertColor(cardId, int.Parse(item.Value)) != 0)
                                    {
                                        Response.Write("<script>alert('color successfully inserted');</script>");
                                    }
                                    else
                                    {
                                        Response.Write("<script>alert('color insertion failed');</script>");
                                    }
                                }
                            }
                            foreach (ListItem item in kindsList.Items)
                            {
                                if (item.Selected)
                                {
                                    if (insertService.InsertKind(cardId, int.Parse(item.Value)) != 0)
                                    {
                                        Response.Write("<script>alert('color successfully inserted');</script>");
                                    }
                                    else
                                    {
                                        Response.Write("<script>alert('color insertion failed');</script>");
                                    }
                                }
                            }
                            if (kindsList.Items.FindByText("creature").Selected)
                            {
                                if (insertService.InsertAttributes(cardId, int.Parse(powerTextBox.Text), int.Parse(toughnesBox.Text)) > 0)
                                {
                                    Response.Write("<script>alert('color successfully inserted');</script>");
                                }
                                else
                                {
                                    Response.Write("<script>alert('color insertion failed');</script>");
                                }
                            }
                        }


                        else
                        {
                            Response.Write("<script>alert('Insertion failed');</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Card already exists');</script>");
                    }
                }
            }
            //}
            //catch(Exception ex)
            //{

            //}
        }