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

November 26th, 2013, 11:53 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
Progress
[QUOTE=Imar;294822 many more who know this stuff.[/QUOTE]
No.. "you're da man"
the update sub seems to be working very well. I felt bold enough to post the working code on SO (and gave you the credit). You can see it on the same page as before:
http://stackoverflow.com/questions/2...w-selectmethod
This should keep me going for awhile  Thanks again and yes, I like this model.
|
|

November 27th, 2013, 12:19 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
Quote:
Originally Posted by Imar
>> Any chance at maybe seeing some mobile application development in your new book?
No. The Web Forms framework and its controls don't always play well with good responsive design principles. If you want to see how Microsoft handles this, just install VS 2013 and look at the default web site template. They use Bootstrap and don't use their own controls like Menu or TreeView as they don't work with Bootstrap.
>> Has Microsoft already come out with the final release version of VS 2013? Really?
>> Again I am wondering if you can install VS2013 on the same box that has VS2012 and VS2012 installed them
Do you ever use Google, Bing or what have you? You can answer questions like these in a split second yourself.
Imar
|
Disregard this post. My comments at this point are no longer needed.
Last edited by vbboyd; November 27th, 2013 at 12:38 AM..
|
|

November 27th, 2013, 03:53 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
This should keep me going for awhile Thanks again and yes, I like this model.
|
I looked at your code at SO and while it works, there are easier ways to accomplish this. You now have a parameter for each property while you could have the entire object type as a parameter instead. I'll post a comment in SO to show you how.
Cheers,
Imar
|
|

November 27th, 2013, 03:55 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Disregard this post. My comments at this point are no longer needed.
|
That's correct. I am glad you are coming to your senses.
Imar
|
|

November 27th, 2013, 10:06 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
Quote:
Originally Posted by Imar
For green field applications, I like this model a lot. I talk about it extensively in this eBook / article series: http://imar.spaanjaars.com/573/aspne...duction-part-1
For existing databases, database first is a suitable model as well. In my updated book for 4.5.1, I use database first as well, just as in earlier editions. I basically generate a new edmx file from a database (using EF 6) and then use model binding to the entities. For example, here's how a new PhotoAlbum gets inserted:
Code:
public void DetailsView1_InsertItem()
{
PhotoAlbum photoAlbum = new PhotoAlbum();
TryUpdateModel(photoAlbum);
if (ModelState.IsValid)
{
using (var myEntities = new PlanetWroxEntities())
{
myEntities.PhotoAlbums.Add(photoAlbum);
photoAlbum.UserName = User.Identity.Name;
myEntities.SaveChanges();
}
Response.Redirect(string.Format("ManagePhotoAlbum?PhotoAlbumId={0}", photoAlbum.Id.ToString()));
}
}
with this markup:
Code:
<asp:DetailsView AutoGenerateRows="false" ID="DetailsView1" DefaultMode="Insert" runat="server" InsertMethod="DetailsView1_InsertItem">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:CommandField ShowInsertButton="True" ShowCancelButton="false" />
</Fields>
</asp:DetailsView>
Does this help? I can supply you with other examples as well if you have specific requests, but I don't want to replicate my entire book here just yet ;-) Also, some examples may not make sense without the explanation from the book.
Imar
|
What is a greenfield application? I have heard that expression used a lot and when I google it I get all kinds of responses back mainly from those from agricultural websites. So what exactly is a greenfield application? The only thing I could find after googling that made any sense in regards to the IT world was:
"An application which the current programmers started. Aka good."
http://c2.com/cgi/wiki?GreenfieldApplication
And what does that mean, Aka good". Pretty much a cryptic answer as are most of the answers that I get back from Google are most of the time.
And this from Wikipedia:
"A greenfield is a project which lacks any constraints imposed by prior networks. An example of a greenfield network is the second generation of cell phone networks."
http://en.wikipedia.org/wiki/Greenfield_project
So are you referring to computer networks here?
|
|

November 27th, 2013, 03:09 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
Improved
Quote:
Originally Posted by Imar
I looked at your code at SO and while it works, there are easier ways to accomplish this. You now have a parameter for each property while you could have the entire object type as a parameter instead. I'll post a comment in SO to show you how.
|
Thanks again Imar, you can see the improved version at the same place on SO - it works fine and it's much cleaner:
http://stackoverflow.com/questions/2...w-selectmethod
I didn't clutter up that code with the msgbox fix but here is what I did - works fine but I'm sure there are lots of ways to do this:
Code:
<div id="messages" style="font-family: Verdana, Geneva, Tahoma, sans-serif; font-size: 14px; color: #FF0000">
Your update is <asp:Label ID="msglabel" runat="server" Text=""></asp:Label>
</div>
I should move the style to a class.
Code:
<script type="text/javascript">
$(function ()
{ if (isPostback) {
$("#messages").show();
}else
$("#messages").hide();
});
$(document).click(function () {
$('#messages').hide();
});
</script>
Code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
ClientScript.RegisterClientScriptBlock([GetType](), "isPostBack", [String].Format("var isPostback = {0};", IsPostBack.ToString().ToLower()), True)
End Sub
And of course in the sub (I can work on error message content later):
Code:
msglabel.Text = " Failed"
msglabel.Text = " Successful"
Hopefully I'm on my way - just waiting for the new book 
|
|

November 27th, 2013, 03:30 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
That looks fine. The only change I could recommend would be to either use TryUpdateModel, or to manually copy over the properties from the incoming parameter to the object you retrieved from the DbContext. That's what TryUpdateModel does so it seems like a waste of (just a few) CPU cycles.
Imar
|
|

November 27th, 2013, 05:28 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
A little confusion
Quote:
Originally Posted by Imar
either use TryUpdateModel, or to manually copy over the properties
|
I clearly see your point about unnecessary code, so I tried a few things:
This works fine when there is no editing/changing of user input at VB time:
Code:
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed"
Return
End If
TryUpdateModel(items)
If ModelState.IsValid Then
myentity.SaveChanges()
msglabel.Text = " Successful"
End If
End Sub
This does not do what I expected - no change to DB when trying to manipulate user input:
Code:
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed"
Return
End If
sector.SectorTitle += "sometext" ' manipulate user input
TryUpdateModel(items)
If ModelState.IsValid Then
myentity.SaveChanges()
msglabel.Text = " Successful"
End If
End Sub
This works fine:
Code:
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed"
Return
End If
items.SectorTitle = String.Concat(sector.SectorTitle, " sometext") ' manipulate user input
Try
myentity.SaveChanges()
Catch ex As Exception
msglabel.Text = " Failed"
Exit Try
Finally
msglabel.Text = " Successful"
End Try
End Sub
So, the TryUpdateModel works fine for no manipulation of user input but I was thinking this is one of the big advantages of this model - to intervene easily. The standard Try-Catch works fine but maybe (?) this is not consistent with this overall model in some why?
The TryUpdateModel seems to take the data just as it comes form listview and ignore any changes I make in VB. Is there a way to accomplish such manipulation and still use the TryUpdateModel? Or is it really just the same thing as the regular Try-Catch anyway? Thanks
|
|

November 27th, 2013, 06:05 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What does the sector look ike that gets passed in? Looks like it's missing data that is causing the validation to fail maybe?
Also, how does your control look? Has it been set up with fields that bind to the various Sector properties?
Imar
|
|

November 27th, 2013, 07:02 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
Clarification
Quote:
Originally Posted by Imar
What does the sector look ike that gets passed in? Looks like it's missing data that is causing the validation to fail maybe?
|
I don't think I was clear on what didn't happen.
Assuming this is the code we are talking about :
Code:
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed"
Return
End If
sector.SectorTitle += "sometext" ' manipulate user input
TryUpdateModel(items)
If ModelState.IsValid Then
myentity.SaveChanges()
msglabel.Text = " Successful"
End If
End Sub
The DB was updated with the data I put in the listview as a user. What did not get added to the DB was the additional text "sometext". It just ignored that and updated (properly) what the user put into the listview. But I wanted to see the concatenated data as well put into the DB (just a simple test).
However, in this code below, the update added what the user put in plus what I concatenated to the user input - which is what I wanted:
Code:
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed"
Return
End If
items.SectorTitle = String.Concat(sector.SectorTitle, " sometext") ' manipulate user input
Try
myentity.SaveChanges()
Catch ex As Exception
msglabel.Text = " Failed"
Exit Try
Finally
msglabel.Text = " Successful"
End Try
End Sub
In neither case did any validation fail - sorry for not being clear about that. I think everything is working correctly, except that if I use the TryUpdateModel I'm not finding the right place to modify the user input data. For example I might want to look for certain words or characters (like html code) that I want to eliminate or question.
The key to this problem is why
Code:
sector.SectorTitle += "sometext"
Did not get reflected in what the TryUpdateModel used?
When I did it without using the TryUpdateModel it worked as I expected and put in the user data concatenated with "sometext" as in this code:
Code:
items.SectorTitle = String.Concat(sector.SectorTitle, " sometext") ' manipulate user input
Try
myentity.SaveChanges()
Does this make sense?
|
|
 |
|