Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : 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
 
Old September 20th, 2013, 01:57 PM
Authorized User
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default SaveChanges() not working with updating data

Hi, this is my first post in this forum, So before anything I have to thank Mr.Spaanjaars for writing such a nice asp.net book...
.....
Here is my problem, I can insert records , delete them, but I can't update them

this is my code for updating a table named Assemble :

using (CostPriceEntities myEntities = new CostPriceEntities())
{
Assemble edit = (from a in myEntities.Assembles
where a.Id == _id
select a).SingleOrDefault();
if (edit != null)
{
edit.CodeA = tbCode.Text;
edit.MESCA = tbMESC.Text;
edit.NameA = tbName.Text;
edit.SpecificationA = tbSpecification.Text;
edit.ZaribA = Convert.ToDouble(tbZarib.Text);
edit.UpdateTimeA = DateTime.Now;
myEntities.SaveChanges();
}

it doesn't give me any error... but I can't figure out why it's not working!!!
I have to mention that just UpdateTimeA will update and others not...
sorry if my English is bad... Please tell me if I have to post more information...
Thanks again;)
 
Old September 20th, 2013, 02:18 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Quote:
So before anything I have to thank Mr.Spaanjaars for writing such a nice asp.net book...
Thank you. Glad you like it.

Are you sure that the query returns an object? Maybe _id doesn't have a valid value so edit is always null and the code in the if block never fires?

Quote:
I have to mention that just UpdateTimeA will update and others not...
Not sure what you mean with this. Can you give an example?

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!
The Following User Says Thank You to Imar For This Useful Post:
Kourosh94 (September 20th, 2013)
 
Old September 20th, 2013, 02:32 PM
Authorized User
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

Hi again, thanks for answering...

_id has a valid value, it's getting value here in page_load and after that elements in Page take value :

protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString.Get("Id ")))
{
_id = Convert.ToInt32(Request.QueryString.Get("Id"));
}
using (CostPriceEntities myEntities = new CostPriceEntities())
{
// Assemble Info
var Ainfo = (from a in myEntities.Assembles
where a.Id == _id
select a).Single();
tbCode.Text = Ainfo.CodeA;
tbMESC.Text = Ainfo.MESCA;
tbName.Text = Ainfo.NameA;
tbSpecification.Text = Ainfo.SpecificationA;
tbZarib.Text = Ainfo.ZaribA.ToString();

// lCreate and lUpdate are Labels for showing datetime and lKind is a label just for getting the kind of Assemble from Assemble Table in database... not important...

lCreate.Text = Ainfo.CreateTimeA.ToString();
lUpdate.Text = Ainfo.UpdateTimeA.ToString();
lKind.Text = Ainfo.KindA;
}
}

Then I change some value in TextBox es and click on :
protected void bDoEdit_Click(object sender, EventArgs e)
{
using (CostPriceEntities myEntities = new CostPriceEntities())
{
Assemble edit = (from a in myEntities.Assembles
where a.Id == _id
select a).SingleOrDefault();
if (edit != null)
{
edit.CodeA = tbCode.Text;
edit.MESCA = tbMESC.Text;
edit.NameA = tbName.Text;
edit.SpecificationA = tbSpecification.Text;
edit.ZaribA = Convert.ToDouble(tbZarib.Text);
edit.UpdateTimeA = DateTime.Now;
myEntities.SaveChanges();
}
}
}


and for update example, I can tell you from the code above, just edit.UpdateTimeA = DateTime.Now is working and will update the time...
if I have to bring more information, Please tell me...

thanks again:)
 
Old September 20th, 2013, 03:06 PM
Authorized User
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

I found how to solve the problem, but I can't figure it out why the cause of problem...

I just add in Page_Load event :

if (!Page.IsPostBack && _id > -1)
{
using (CostPriceEntities myEntities = new CostPriceEntities())
{
// Assemble Info
var Ainfo = (from a in myEntities.Assembles
where a.Id == _id
select a).Single();
tbCode.Text = Ainfo.CodeA;
tbMESC.Text = Ainfo.MESCA;
tbName.Text = Ainfo.NameA;
tbSpecification.Text = Ainfo.SpecificationA;
tbZarib.Text = Ainfo.ZaribA.ToString();
lCreate.Text = Ainfo.CreateTimeA.ToString();
lUpdate.Text = Ainfo.UpdateTimeA.ToString();
lKind.Text = Ainfo.KindA;
}
}

It means I just add the : if (!Page.IsPostBack && _id > -1)

can anyone tell me the what's the problem?? it's because of view state??? I don't know!!!
 
Old September 20th, 2013, 03:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Ah, I see. The code in Page_Load overwrites the changes the user made with the data from the database, resulting in no change. Wrap the code that fills the controls in an if (!Page.IsPostBack) block and it should work:

Code:
if (!Page.IsPostBack)
{
  using (CostPriceEntities myEntities = new CostPriceEntities())
  {
  // Assemble Info
  var Ainfo = (from a in myEntities.Assembles
                                    where a.Id == _id
       select a).Single();
                   tbCode.Text = Ainfo.CodeA;
  tbMESC.Text = Ainfo.MESCA;
  tbName.Text = Ainfo.NameA;
                
  // Rest of the code here
  }
}
Hope this helps,

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!
The Following User Says Thank You to Imar For This Useful Post:
Kourosh94 (September 20th, 2013)
 
Old September 20th, 2013, 03:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, looks like you already found it out.... ;-)

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!
The Following User Says Thank You to Imar For This Useful Post:
Kourosh94 (September 20th, 2013)
 
Old September 20th, 2013, 03:14 PM
Authorized User
 
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Default

Thank you so much Mr.Spaanjaars...
You and your book help me a lot... ;)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating data from a report ? palli BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 May 28th, 2007 12:34 PM
Updating data from a report ? palli Reporting Services 0 May 28th, 2007 12:32 PM
Updating data table vaidyapragati ASP.NET 2.0 Basics 4 May 11th, 2007 11:52 PM
Updating Data Using DataAdapter ysebastian Classic ASP Databases 0 June 3rd, 2005 10:27 PM
Updating data with a cursor jscuderi SQL Server 2000 2 August 10th, 2003 08:14 AM





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