Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 9th, 2011, 01:41 PM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Inserting item through Entity framework

I am using hand coding method to insert categories in database. For this I am using entity framework, but when I click button nothing happens.
Category table structure is
Id (bigint)- PK,
Name,
Description,
ParentId (bigint)


Code:
<asp:TextBox ID="txtName" runat="server" title="Enter Name of Category"></asp:TextBox>
Parent<br />
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" DataTextField="Name" DataValueField="Id" Width="129px">
  <asp:ListItem Value="0">None</asp:ListItem>
</asp:DropDownList>
        
Description<br />
<asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine" Width="250" Height="150"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Submit" />
and code behind is

Code:
using PlanetWroxModel;

public partial class Management_Categories : System.Web.UI.Page
{
    int _id = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
      ...
    }  
protected void Button1_Click(object sender, EventArgs e)
    {
        using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
        {
            Category cat;
            if (_id == -1) // Insert new item
            {
                cat = new Category();
                cat.Name = txtName.Text;
                cat.Description = TextBox2.Text;
                cat.ParentId = Convert.ToInt64(DropDownList1.SelectedValue);
                myEntities.AddToCategories(cat);
                myEntities.SaveChanges();
                Response.Redirect(Request.Url.ToString());
            }
        }
Reply With Quote
  #2 (permalink)  
Old December 10th, 2011, 03:19 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

Code:
 
<asp:Button ID="Button1" runat="server" Text="Submit" />
Looks like you haven't set ap a Click handler to call Button1_Click when you click the button.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old December 10th, 2011, 11:08 AM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

oops, silly mistake.
Corrected it and it is working now. Actually I copied code from book and I did not notice that I copied from VB code snippet for aspx page, and now I understand why application was not getting debugged.

But one more thing that I wanna ask is why you wrote myEntities.AddToReviews(myReview); at very early in if block and not just before savechanges() method in the code below?

Code:
protected void SaveButton_Click(object sender, EventArgs e)
{
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
Review myReview;
if (_id == -1) // Insert new item
{
myReview = new Review();
myReview.CreateDateTime = DateTime.Now;
myReview.UpdateDateTime = myReview.CreateDateTime;
myEntities.AddToReviews(myReview);
}
else // update existing item
{
myReview = (from r in myEntities.Reviewswhere r.Id == _id
select r).Single();
myReview.UpdateDateTime = DateTime.Now;
}
myReview.Title = TitleText.Text;
myReview.Summary = SummaryText.Text;
myReview.Body = BodyText.Text;
myReview.GenreId = Convert.ToInt32(GenreList.SelectedValue);
myReview.Authorized = Authorized.Checked;
myEntities.SaveChanges();
Response.Redirect(“Reviews.aspx”);
}
Thanks for pointing out error.
Reply With Quote
  #4 (permalink)  
Old December 10th, 2011, 11:28 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

You only want to add it when you create a new item. If you'd add it before you save, you also readd an existing item, which is not what you want.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old December 10th, 2011, 12:21 PM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

But myReview instance is added early then how other properties viz. Title, Summary etc are available to be added at later time?
Reply With Quote
  #6 (permalink)  
Old December 10th, 2011, 01:44 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

You're adding a reference to, and not a copy of, the Review instance, so EF can see the changes you're making as it's the exact same instance you're setting the properties on.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old December 10th, 2011, 02:17 PM
Friend of Wrox
Points: 1,469, Level: 15
Points: 1,469, Level: 15 Points: 1,469, Level: 15 Points: 1,469, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Thanks, it helped...
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Having problems with Entity Framework Rushino ASP.NET 4 General Discussion 7 June 12th, 2013 05:17 PM
Entity Framework vs datasets? hoss BOOK: Beginning ASP.NET 4 : in C# and VB 1 September 30th, 2010 02:42 AM
Switching To entity framework luckystar BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 July 31st, 2009 09:52 PM
The BeerHouse and Entity Framework docluv BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 October 3rd, 2008 01:32 PM



All times are GMT -4. The time now is 02:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.