View Single Post
  #7 (permalink)  
Old October 28th, 2013, 07:18 PM
daveharney daveharney is offline
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.