Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 9th, 2007, 12:38 PM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default A strange thing in Micro's NewsAndArticle Chapter

Recently,I was learning Micro Belinaso's ASP.NET 2.0 Website Programming - Problem-Design-Solution ,When I do with the News and Article Chapter ,I encounter a program . It was in the AddEditArticle.aspx Page, when I post a new article and sumit,it was just fine. But then I refresh the screen(the Browser),a strange thing happen:[the article I just posted before was posted again!!!
I don't know why this were happen.I just know in the ManageCategories.aspx, When I do the same things above,nothing will happen,because the InsertCategory Store Procedure check whether a category with the same title has already exist before it really insert a category,the codes are shown as below:
Code:
CREATE PROCEDURE dbo.tbh_Articles_InsertCategory
(
   @AddedDate     datetime,
   @AddedBy       nvarchar(256),
   @Title         nvarchar(256),
   @Importance    int,
   @Description   nvarchar(4000),
   @ImageUrl      nvarchar(256),
   @CategoryID    int OUTPUT
)
AS
SET NOCOUNT ON

-- check whether a category with the same name already exists
DECLARE @CurrID int
SELECT @CurrID = CategoryID FROM tbh_Categories WHERE
   LOWER(@Title) = LOWER(Title)

IF @CurrID IS NOT NULL
   BEGIN
   SET @CategoryID = -1
   RETURN
   END

INSERT INTO tbh_Categories 
   (AddedDate, AddedBy, Title, Importance, Description, ImageUrl)
   VALUES (@AddedDate, @AddedBy, @Title, @Importance, @Description, @ImageUrl)

SET @CategoryID = scope_identity()
so when I do the same thing I mentioned above ,I just doing fine.
But I wonder why this happen and how I can avoid it.I am a ASP.NET beginner.Eager for help.Thanks.

We learn from the history that we do not learn from the history
__________________
------------------------------------------------------------------------
We learn from the history that we do not learn from the history
 
Old August 9th, 2007, 12:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. maybe the SP that has to add/edit the article (not the article category) is not checking for something that already exists???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 9th, 2007, 12:50 PM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default

 
Quote:
quote:hi there.. maybe the SP that has to add/edit the article (not the article category) is not checking for something that already exists???
Quote:
Yes.But I think for the articles , it's not proper to force people to post a article with different title. So we can not do the same thing as the InsertCategory store procedure do. So how to manage this problem?

We learn from the history that we do not learn from the history
 
Old August 9th, 2007, 01:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

how are you updating the database?? (codebehind, control??) it will be hard to tell where the request is actually a refresh or new data...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 9th, 2007, 09:11 PM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default

Thank you for your quick reply.I update the database with ObjectDataSources.To make things clearer ,I have to paste all the code here.It's so long , and I hope it will not upset you.And in order to make it simple,I just show the codes do with the article categories. Here is the code for the ManageCategories.aspx
Code:
<%@ Page Language="C#" MasterPageFile="~/Template.master" ValidateRequest="false" AutoEventWireup="true" CodeFile="ManageCategories.aspx.cs" Inherits="MB.TheBeerHouse.UI.Admin.ManageCategories" Title="The Beer House - Manage Categories" %>
<%@ Register Src="../Controls/FileUploader.ascx" TagName="FileUploader" TagPrefix="mb" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" Runat="Server">   
   <div class="sectiontitle">Manage Article Categories</div>
   <p></p>
   <asp:GridView ID="gvwCategories" runat="server" AutoGenerateColumns="False" DataSourceID="objAllCategories" Width="100%" DataKeyNames="ID" OnRowDeleted="gvwCategories_RowDeleted" OnRowCreated="gvwCategories_RowCreated" OnSelectedIndexChanged="gvwCategories_SelectedIndexChanged" ShowHeader="false">
      <Columns>
         <asp:ImageField DataImageUrlField="ImageUrl">
            <ItemStyle Width="100px" />
         </asp:ImageField>
         <asp:TemplateField>
            <ItemTemplate>
               <div class="sectionsubtitle">
               <asp:Literal runat="server" ID="lblCatTitle" Text='<%# Eval("Title") %>' />
               </div>
               <br />
               <asp:Literal runat="server" ID="lblDescription" Text='<%# Eval("Description") %>' />
            </ItemTemplate>
         </asp:TemplateField>
         <asp:HyperLinkField Text="<img border='0' src='../Images/ArrowR.gif' alt='View articles' />"
            DataNavigateUrlFormatString="ManageArticles.aspx?CatID={0}" DataNavigateUrlFields="ID">
            <ItemStyle HorizontalAlign="Center" Width="20px" />
         </asp:HyperLinkField>
         <asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/Edit.gif" SelectText="Edit category" ShowSelectButton="True">
            <ItemStyle HorizontalAlign="Center" Width="20px" />
         </asp:CommandField>
         <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/Delete.gif" DeleteText="Delete category" ShowDeleteButton="True">
            <ItemStyle HorizontalAlign="Center" Width="20px" />
         </asp:CommandField>
      </Columns>
      <EmptyDataTemplate><b>No categories to show</b></EmptyDataTemplate>   
   </asp:GridView>
   <asp:ObjectDataSource ID="objAllCategories" runat="server" SelectMethod="GetCategories"
      TypeName="MB.TheBeerHouse.BLL.Articles.Category" DeleteMethod="DeleteCategory">
   </asp:ObjectDataSource>
   <p></p>
   <asp:DetailsView ID="dvwCategory" runat="server" AutoGenerateRows="False" DataSourceID="objCurrCategory" Height="50px" Width="50%" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" HeaderText="Category Details" OnItemInserted="dvwCategory_ItemInserted" OnItemUpdated="dvwCategory_ItemUpdated" DataKeyNames="ID" OnItemCreated="dvwCategory_ItemCreated" DefaultMode="Insert" OnItemCommand="dvwCategory_ItemCommand">
      <FieldHeaderStyle Width="100px" />
      <Fields>
         <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" InsertVisible="False" />
         <asp:BoundField DataField="AddedDate" HeaderText="AddedDate" InsertVisible="False"
            ReadOnly="True" SortExpression="AddedDate" />
         <asp:BoundField DataField="AddedBy" HeaderText="AddedBy" InsertVisible="False" ReadOnly="True"
            SortExpression="AddedBy" />
         <asp:TemplateField HeaderText="Title" SortExpression="Title">
            <ItemTemplate>
               <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("Title") %>' MaxLength="256" Width="100%"></asp:TextBox>
               <asp:RequiredFieldValidator ID="valRequireTitle" runat="server" ControlToValidate="txtTitle" SetFocusOnError="true"
                  Text="The Title field is required." ToolTip="The Title field is required." Display="Dynamic"></asp:RequiredFieldValidator>
            </EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Importance" SortExpression="Importance">
            <ItemTemplate>
               <asp:Label ID="lblImportance" runat="server" Text='<%# Eval("Importance") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="txtImportance" runat="server" Text='<%# Bind("Importance") %>' MaxLength="256" Width="100%"></asp:TextBox>
               <asp:RequiredFieldValidator ID="valRequireImportance" runat="server" ControlToValidate="txtImportance" SetFocusOnError="true"
                  Text="The Importance field is required." ToolTip="The Importance field is required." Display="Dynamic"></asp:RequiredFieldValidator>
               <asp:CompareValidator ID="valImportanceType" runat="server" Operator="DataTypeCheck" Type="Integer"
                  ControlToValidate="txtImportance" Text="The Importance must be an integer."
                  ToolTip="The Importance must be an integer." Display="dynamic" />
            </EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Image" ConvertEmptyStringToNull="False">
            <ItemTemplate>
               <asp:Image ID="imgImage" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' 
                  AlternateText='<%# Eval("Title") %>'
                  Visible='<%# !string.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "ImageUrl").ToString()) %>' />
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="txtImageUrl" runat="server" Text='<%# Bind("ImageUrl") %>' MaxLength="256" Width="100%"></asp:TextBox>
            </EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Description" SortExpression="Description" ConvertEmptyStringToNull="False">
            <ItemTemplate>
               <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>' Width="100%"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' Rows="5" TextMode="MultiLine" MaxLength="4000" Width="100%"></asp:TextBox>
            </EditItemTemplate>
         </asp:TemplateField>
      </Fields>
   </asp:DetailsView>
   <asp:ObjectDataSource ID="objCurrCategory" runat="server" InsertMethod="InsertCategory" SelectMethod="GetCategoryByID" TypeName="MB.TheBeerHouse.BLL.Articles.Category" UpdateMethod="UpdateCategory">
      <SelectParameters>
         <asp:ControlParameter ControlID="gvwCategories" Name="categoryID" PropertyName="SelectedValue"
            Type="Int32" />
      </SelectParameters>
   </asp:ObjectDataSource>
   <p></p>
   <mb:FileUploader ID="FileUploader1" runat="server" />
</asp:Content>
And the code behind file ManageCategories.cs
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MB.TheBeerHouse;

namespace MB.TheBeerHouse.UI.Admin
{
   public partial class ManageCategories : BasePage
   {
      protected void Page_Load(object sender, EventArgs e)
      {

      }

      protected void gvwCategories_SelectedIndexChanged(object sender, EventArgs e)
      {
         dvwCategory.ChangeMode(DetailsViewMode.Edit);
      }

      protected void gvwCategories_RowDeleted(object sender, GridViewDeletedEventArgs e)
      {
         gvwCategories.SelectedIndex = -1;
         gvwCategories.DataBind();
         dvwCategory.ChangeMode(DetailsViewMode.Insert);
      }

      protected void gvwCategories_RowCreated(object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            ImageButton btn = e.Row.Cells[4].Controls[0] as ImageButton;
            btn.OnClientClick = "if (confirm('Are you sure you want to delete this category?') == false) return false;";
         }
      }

      protected void dvwCategory_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
      {
         gvwCategories.SelectedIndex = -1;
         gvwCategories.DataBind();
      }

      protected void dvwCategory_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
      {
         gvwCategories.SelectedIndex = -1;
         gvwCategories.DataBind();
      }

      protected void dvwCategory_ItemCreated(object sender, EventArgs e)
      {
         if (dvwCategory.CurrentMode == DetailsViewMode.Insert)
         {
            TextBox txtImportance = dvwCategory.FindControl("txtImportance") as TextBox;
            txtImportance.Text = "0";
         }
      }

      protected void dvwCategory_ItemCommand(object sender, DetailsViewCommandEventArgs e)
      {
         if (e.CommandName == "Cancel")
         {
            gvwCategories.SelectedIndex = -1;
            gvwCategories.DataBind();
         }
      }
   }
}
Then if I cross out the "check whether a category with the same name already exists" code in the InsertCategory store procedure,the strange thing I mention above happen.
Hope this can make myself understood.


------------------------------------------------
We learn from the history that we do not learn from the history
 
Old August 10th, 2007, 07:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

It's not a strange thing.. is totally logical.. when you refresh the page, since it's an asp form, it will send the request again, and the code will be fired again, so the SP will run again, and if you don't have a way to check for duplicates it will duplicate the article... In your code (that is very hard to read) I don't even see where you are calling the SP..


HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 10th, 2007, 11:25 AM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default

I think now I understand why this would happen.But how can we do something in the code to manage it. I am not quite catch you ...do you mean we can not avoid this problem .Just not to refresh the browser when doing some operations like insert or update? again,thanks for your reply.

------------------------------------------------------------------------
We learn from the history that we do not learn from the history
 
Old August 10th, 2007, 11:29 AM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default

Quote:
quote:In your code (that is very hard to read) I don't even see where you are calling the SP..
Actually,I call the SP in the Data access layer,and I just pasted the Presentation layer code.So you wouldn't see the SP in my code.

------------------------------------------------------------------------
We learn from the history that we do not learn from the history
 
Old August 10th, 2007, 12:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

let thinks a little.. you are trying to avoid duplicates that where made when you refresh the page or duplicates at distinct times?
maybe you coulde give the user a ticket number when he entered the page, and use it to store the data. If the user refresh the page, the ticket will be the same and you know that it was a refresh...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old August 10th, 2007, 09:49 PM
Authorized User
 
Join Date: Aug 2007
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Magi
Default

Thanks gbianchi.It's a good way.

Now I have another problem.I don't quite understand the event of the the DataControls,
such as GridView.Recently I saw some codes which hide the Delete button according to
the datakeys in the records it binds.Here is the the code:

Code:
protected void gvwOrderStatuses_RowCreated(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      ImageButton btn = e.Row.Cells[2].Controls[0] as ImageButton;

      int orderStatusID = Convert.ToInt32(
         gvwOrderStatuses.DataKeys[e.Row.RowIndex][0]);

      if (orderStatusID > 3)
         btn.OnClientClick = "if (confirm('Are you sure you want to delete this
order status?') == false) return false;";
      else
         btn.Visible = false;
   }
}
You see,it handle it in the RowCreated event of the GridView displaying the records,
to ensure that the Delete LinkButton is hidden for the first three records.
I understant the logic of the code,but I don't quite understant why it is handle
in the GridView's RowCreated event.Doesn't in that event the GridView hasn't bind
anything until the RowDataBound event.So how can it get the DataKeys in that
row being created?



------------------------------------------------------------------------
We learn from the history that we do not learn from the history





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error 50290: VBA in Excel 2003 doing strange thing Andraw Pro VB 6 0 October 29th, 2008 08:44 AM
About CSS Chapter 5 the <thing> + <thing> {} part thenetduck BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 0 February 7th, 2007 03:45 AM
Eight chapter - very strange things happening marjan BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 May 1st, 2004 06:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.