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

September 20th, 2013, 01:57 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
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;)
|
|

September 20th, 2013, 02:18 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

September 20th, 2013, 02:32 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
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:)
|
|

September 20th, 2013, 03:06 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
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!!!
|
|

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

September 20th, 2013, 03:09 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Oh, looks like you already found it out.... ;-)
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

September 20th, 2013, 03:14 PM
|
|
Authorized User
|
|
Join Date: Sep 2013
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
|
|
Thank you so much Mr.Spaanjaars...
You and your book help me a lot... ;)
|
|
 |
|