 |
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 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
|
|
|
|
|

December 9th, 2011, 02:41 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
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());
}
}
|
|

December 10th, 2011, 04:19 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

December 10th, 2011, 12:08 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
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.
|
|

December 10th, 2011, 12:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

December 10th, 2011, 01:21 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
But myReview instance is added early then how other properties viz. Title, Summary etc are available to be added at later time?
|
|

December 10th, 2011, 02:44 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

December 10th, 2011, 03:17 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
|
|
Thanks, it helped...
|
|
 |
|