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:

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure

Partial Public Class Entities
    Inherits DbContext

    Public Sub New()
    End Sub

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

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

End Class
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
<?xml version="1.0" encoding="utf-8"?>
  <package id="EntityFramework" version="6.0.0" targetFramework="net451" />
<?xml version="1.0" encoding="utf-8"?>
  For more information on how to configure your ASP.NET application, please visit
    <!-- For more information on Entity Framework configuration, visit -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.1">
        <add assembly="System.Security, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
        <add assembly="System.Data.Entity.Design, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider" />
    <httpRuntime targetFramework="4.5.1" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
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)

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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server"
And my vb code behind which obviously doesn't work because I obviously don't know what I'm doing

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.