Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Search this Thread Display Modes
  #1 (permalink)  
Old October 26th, 2013, 11:39 PM
Authorized User
Points: 353, Level: 6
Points: 353, Level: 6 Points: 353, Level: 6 Points: 353, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
Default Your Book and VS Versions

Hi Imar,

You may recall that I built a bicycle routes website almost entirely from the advice in your 4.0 book - thanks again for the excellent book and advice in this forum. As part of my plan for my annual winter-time enhancements to my website, I bought a copy of your 4.5 book. Given my minimal programming skill level, I intend to refer to your 4.5 book as my primary go-to reference in a mostly cook-book fashion - I don't have time for a lot of experimentation on my own. I also want to move up to ASP 4.5, SQL 2012 and generally stay reasonably current with MS technologies. To start my winter programming project, I got your newest book, installed SQL Server and Visual Studio on a clean machine (no prior development tools), and began a few simple tests. Your book clearly states that one should install VS and SQL Server both in the 2012 versions. However, when I went to the MS download area, VS 2013 was available in its final release version. Given that I'm only in the planning stage, I decided to try it and see if it was reasonably compatible with your book.

I immediately went to Ch 19 and followed all of those deployment recommendations as I knew I would ultimately need to do that anyway - it went pretty smoothly. Then I jumped to Ch 14 as my website is very dependent upon EF and Linq - especially for use with Gridview and Listview. I roughly followed your steps for a little test to populate a gridview with a very simple DB table. I did this first on my other PC running VS 2010 and SQL 2008 - worked smooth as silk. However, on my PC running VS 2013, the very same exercise resulted in a very nasty error that has eluded remedy (see below for more detail on this error). I assume you have looked at VS 2013. So, my question is: in order to stay reasonably close to the procedures in your 4.5 book, should I scrap the 2013 version and install VS 2012?

It seems that VS 2013 has some benefits and should be reasonably backward compatible with VS 2012. If there are just a few bugs that one can expect to be fixed shortly, that would argue for hanging in there with the 2013 version. However, if I'm likely to find frequent mismatches between your book and VS 2013, then I'll stick with 2012. I realize that you don't always appreciate these kinds of broad questions. However, I'm assuming you've looked at the 2013 version and perhaps you have an opinion about using it with your book - I'm not asking for an evaluation of VS 2013 itself. Thanks again for your excellent books!

If you have an interest in my EF problem, I've posted it in these two places:

http://forums.asp.net/t/1946112.aspx...adata+resource

http://stackoverflow.com/questions/1...tity-framework
Reply With Quote
  #2 (permalink)  
Old October 27th, 2013, 04:24 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Dave,


Quote:
I assume you have looked at VS 2013.
I have, yes. I am about to finish the first draft of an updated version of the book for VS 2013 and ASP.NET 4.5.1.

Quote:
So, my question is: in order to stay reasonably close to the procedures in your 4.5 book, should I scrap the 2013 version and install VS 2012?
Some things have changed between VS2012/2013 and ASP.NET 4.5/4.5.1. Most of it will be the same so you won't have trouble following along. However, some things have changed considerably, such as the Entity Framework, leading to the issues you described in the two posts below.

For this particular EF problem, there are a few solutions / alternatives:

1. Drop back to EF 5. In order to do this, delete the .edmx and .tt files from the App_Code folder, delete the EF assemblies from the Bin folder, and remove the references to EF from web.config. Then rebuild the site, add a new ADO.NET Entity Model and choose version 5 in the Choose Your Version dialog. Then delete the .tt files from the App_Code folder, open the EDMX diagam, right-click an empty spot and on the Properties Grid set Code Generation Strategy to Legacy ObjectContext. Then rebuild again, and it should work.

2. Use Model Binding. This is really the recommended solution. The Microsoft / EF team recommends people to stay away from the EntityDataSource control and only use it for Dynamic Data web sites, Model Binding is the preferred solultion. It's pretty clean and straight forward to use, and gives you a lot of control over your queries and object updates. For more info about Model Binding, check out: http://www.asp.net/web-forms/tutoria...etrieving-data

My updated book uses the second option and shows how to use Model Binding instead of the EntityDataSource control.

Quote:
It seems that VS 2013 has some benefits and should be reasonably backward compatible with VS 2012.
Indeed. I would go for 2013 and deal with the differences. The EF issue is probably the largest issue you'll encounter.

I also answered the two separate posts about the problem. You probably shouldn't have posted this in three separate forums as I now had to answer it three times, and then the discussion will continue in three separate places.

Good luck, and let me know if you run into other differences between 2012 and 2013.

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
The Following 2 Users Say Thank You to Imar For This Useful Post:
daveharney (October 27th, 2013)
  #3 (permalink)  
Old October 27th, 2013, 11:47 AM
Authorized User
Points: 353, Level: 6
Points: 353, Level: 6 Points: 353, Level: 6 Points: 353, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
Default Works Perfectly

Hi Imar,

Your first suggestion works perfectly and I'll pursue the Model Binding Tutorial in the second suggestion - thank you very much! I should note that your steps need to be followed in the exact order given (delete .tt before changing EDMX property). Also, the auto EF update of the config file requires a cut-paste to move the ConfigSections back to the beginning of the file - at least in my little test.

Yes, I felt a guilt pain as I triple posted. I assumed that MS folks would be monitoring the roll-out of their new VS release and would jump at clarifying an issue like this where backward compatibility was broken. It was frustrating when MSDN produced zero response so I posted on ASP.NET which produced no response from a person familiar with VS 2013. Stack Overflow was my last hope. I'm guessing that I'm not the only person who will encounter this problem. So, perhaps the multiple postings will save other developers the time I wasted on this issue. But, you're right, multiple posting is not a good idea.

Actually, you were the first person I thought of to address the problem - but your book clearly stated VS 2012 so I felt it might not be appropriate to ask the detailed question in your forum as you never mentioned VS 2013. I did feel my more general question related to your book and I'm very happy that you took it to the next step and solved the initial problem. Also I'm very glad you've encouraged me to stick with 2013 as I was about ready to scrap it! Per your invitation, I'll report any other VS 2013 problems that are specific to things in your current book.

Once again, you are a great resource in the MS development world and much appreciated. Microsoft should automatically give you a healthy percent of their stock shares :-)

Quote:
Originally Posted by Imar View Post
I am about to finish the first draft of an updated version of the book for VS 2013 and ASP.NET 4.5.1.
Any idea when the new book will be released?
Reply With Quote
  #4 (permalink)  
Old October 27th, 2013, 11:57 AM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
I felt it might not be appropriate to ask the detailed question in your forum as you never mentioned VS 2013
That's because VS 2013 was only released a few weeks ago, long after the book came out ;-)

Quote:
Per your invitation, I'll report any other VS 2013 problems that are specific to things in your current book.
Great. I think I have tackled all issue (working on the last chapter - 19 - at the moment), so I should be able to help you out in case you run into issues.

Quote:
Any idea when the new book will be released?
Amazon says March, but I am working hard to get it done sooner. Not sure if that's possible as much of it depends on processes that place after I sent in my final files.

Quote:
Stack Overflow was my last hope. ... So, perhaps the multiple postings will save other developers the time I wasted on this issue. But, you're right, multiple posting is not a good idea.
If the issue is fully resolved, can you mark my reply at SO as the answer?

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
The Following User Says Thank You to Imar For This Useful Post:
daveharney (October 29th, 2013)
  #5 (permalink)  
Old October 28th, 2013, 01:01 PM
Authorized User
Points: 353, Level: 6
Points: 353, Level: 6 Points: 353, Level: 6 Points: 353, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
Default Your Book Update

Hi Imar,

"2. Use Model Binding. .....check out: http://www.asp.net/web-forms/tutoria...etrieving-data My updated book uses the second option...."

I successfully completed the first section of the above link: "Retrieving and displaying data with model binding and web forms" which generated a couple of thoughts about your book update.

1. As this tutorial focused on a Web Application and Code First approach, I'm hopeful that your book will continue with the simple Web Site approach and include examples for "Generating EF Code First model classes from an existing database" - as explained by this article in Jon Galloway's blog (http://weblogs.asp.net/jgalloway/arc...-database.aspx). Although I got mostly through the tutorial, I gave up due to 2012-2013 and C#-VB mismatches and related problems - more than I want to deal with at this time.

2. More generally - I can really understand your support for the Model Binding approach and I'd like to pursue that also. However, the Code First approach for generating the DB is not something I can relate to very well. Aside from the fact that lots of code will be written for existing databases, I personally don't see the practicality of generating a DB from code. I spent decades designing fairly large and complicated business systems and found the incremental development of a DB Entity Relationship diagram to be critical tool - even as an early adopter of OO and Agile technologies. Although that gets us really off-topic - my point is that I'd love to see some of your great detailed examples for using the Model Binding approach by generating and modifying code from an existing DB.

If this is not the right place to continue this discussion, I can use your Blog to give you my email address. Prior to publication of your book update, I'd be very happy to try some of your examples that might relate to this discussion and provide feedback. Thanks again for your help and looking forward to your book update being published.
Reply With Quote
  #6 (permalink)  
Old October 28th, 2013, 04:28 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
However, the Code First approach for generating the DB is not something I can relate to very well.
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

Quote:
Although that gets us really off-topic - my point is that I'd love to see some of your great detailed examples for using the Model Binding approach by generating and modifying code from an existing DB.
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
__________________
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
The Following User Says Thank You to Imar For This Useful Post:
daveharney (October 29th, 2013)
  #7 (permalink)  
Old October 28th, 2013, 07:18 PM
Authorized User
Points: 353, Level: 6
Points: 353, Level: 6 Points: 353, Level: 6 Points: 353, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
Default First Attempt at Model Binding

Hi Imar,

I'll have to do some reading of your e-book.

I don't want to wear out my welcome on this topic, but perhaps you can help with my first little test with my tinyDB containing one table with 3 fields - the goal is to populate a gridview using model binding (I only use VB).

I created an empty website, added an app_code folder with an ADO.NET Entity Data Model using EF 6, chose generate from DB but cleared the checkbox for saving the connection string in the config file, and selected the testTable1which is the only table in the TinyDB database. Which resulted in:

testModel.Context.vb
Code:
Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

Partial Public Class Entities
    Inherits DbContext

    Public Sub New()
        MyBase.New("name=Entities")
    End Sub

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
        Throw New UnintentionalCodeFirstException()
    End Sub

    Public Overridable Property TestTable1() As DbSet(Of TestTable1)

End Class
testModel.vb
Code:
Imports System
Imports System.Collections.Generic

Partial Public Class TestTable1
    Public Property theKey As Integer
    Public Property FirstField As String
    Public Property SecondField As String

End Class
packages.config - which has an error that says that the packages element is not declared
Code:
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.0.0" targetFramework="net451" />
</packages>
Web.config
Code:
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
      <buildProviders>
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5.1" />
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
There was also the .edmx and the 2 dll files in Bin

This is my attempt at default.aspx displaying a gridview (trying to keep it as simple as possible)

Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"
            SelectMethod="table1_GetData">
        </asp:GridView>
    </div>
    </form>
</body>
</html>
And my vb code behind which obviously doesn't work because I obviously don't know what I'm doing

Code:
Imports TestTable1

Partial Class _Default
    Inherits System.Web.UI.Page
    Public Function table1_GetData() As IQueryable(Of TestTable1)
        Dim db As TestTable1 = New TestTable1()
        Dim query = db.---
        Return query
    End Function
End Class
first, the imports statement doesn't make sense to me because I'd image a DB name here, not just one table name (even if the db has only one table).

second I'm not at all sure of the Function line "As" element of IQueryable but it seemed reasonable (I guess).

third, I'm really lost with the formation of the query itself - I want to return all fields in all rows to the girdview.

I think if I can get this procedure down, it'll help a lot knowing what to do next. Right now I'm not sure if I missed (or screwed up) some important step in the overall process. As always, thanks for your help.
Reply With Quote
  #8 (permalink)  
Old October 29th, 2013, 03:35 PM
Authorized User
Points: 353, Level: 6
Points: 353, Level: 6 Points: 353, Level: 6 Points: 353, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
Default Another Attempt

Hi Imar,

I added a connection string in the config file for "entities" and tried this (no luck)

Code:
Imports TestTable1
Imports Entities
Imports System.Data
Imports System.Linq
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page

    Public Function GridView1_GetData() As IQueryable 
        Using myentities As New Entities()
            Dim myquery = From mytable In myentities.TestTable1 Select mytable
            Return myquery
        End Using
    End Function
End Class
Error for myquery line is:

An exception of type 'System.Data.Entity.Core.MetadataException' occurred in EntityFramework.dll but was not handled in user code

Will appreciate help whenever you have time - thanks.
Reply With Quote
  #9 (permalink)  
Old October 29th, 2013, 04:05 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
I chose generate from DB but cleared the checkbox for saving Ithe connection string in the config file
Looks like you fixed this, but yes, you want to store it in the web.config file so EF can use it.

Your DbContext and test table classes look fine; very similar to my Planet Wrox model classes.

Quote:
which has an error that says that the packages element is not declared
You can ignore that; it's just a warning when you have the package file open as it doesn't have a schema defined.
Quote:
An exception of type 'System.Data.Entity.Core.MetadataException' occurred in EntityFramework.dll but was not handled in user code
Does it provide more information? Do you see an actual, more useful error message?

Quote:
Return myquery
Try replacing that with this:

Code:
Return myquery.ToList()
EF 6 no longer works with deferred queries in a using block. Calling ToList executes the query immediately, removing the dependency from the Entities object.

Hope this helps; sounds like you're almost there.

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
The Following 2 Users Say Thank You to Imar For This Useful Post:
daveharney (October 29th, 2013)
  #10 (permalink)  
Old October 29th, 2013, 04:27 PM
Imar's Avatar
Wrox Author
Points: 72,045, Level: 100
Points: 72,045, Level: 100 Points: 72,045, Level: 100 Points: 72,045, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,083
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

>> I'll have to do some reading of your e-book.

Or maybe not ;-) Concepts from that book are very different from the stuff in the Beginning book. I would recommend staying close to my Beginning ASP.NET book and make things work with EF 6 and VS 2013. You'll need to learn a million new concepts if you want to switch to the n-layered approach described in my e-book....

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
The Following User Says Thank You to Imar For This Useful Post:
daveharney (October 29th, 2013)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
PHP, Original PHP, Mysql and Apacher versions used in the book Sam321 BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 1 August 12th, 2012 07:07 AM
Different Versions of IE South Side Rob BOOK: CSS Instant Results 1 February 26th, 2009 06:05 PM
VS versions??? SKhna Javascript 1 February 4th, 2008 03:27 PM
Book Versions MER78 BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 September 24th, 2004 02:56 PM
Java Versions? maayub J2EE 4 August 22nd, 2003 05:29 AM



All times are GMT -4. The time now is 03:09 PM.


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