0

I am in the process of learning Entity Framework 6. I am looking for a tutorial explaining how to setup an ASP.NET Web Forms site (not ASP.NET MVC) and create basic CRUD pages. The only tutorials I can find are based on MVC and/or Console Apps rather than web application.

I am familiar with MVC but I do not want to use MVC for this project. Entity Framework 6 webforms only. All tutorials seem to be based on MVC or they step you through creating a Console App rather than a Web App.

An example of what I am running into is trying to add a simple gridview to a webform page.

It seems this should be a simple procedure but I have not found the code to make it work yet. I think I must be missing a simple step. The page has a gridview named Gridview1. In the code behind I am using:

namespace EFTestSchool.Models
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Student db = new Student();
            GridView1.DataSource
        }
    }
}

When I add the .Models to the namespace, it says that the Gridview1 cannot be found. When I use the namespace as simply EFTestSchool, the Gridview1 is recognized but my line Student db = New Student() says Student cannot be found. The name of my project is EFTestSchool. The name of my model is SchoolModel. This is based on the standard School database used in some tutorials. I must be missing something very simple that is preventing me from adding a simple grid to a webform page.

Any suggestions would be appreciated.

1 Answer 1

1

I can't think of why you would want to choose ASP.NET Web Forms over the more modern .Net alternatives that there are now. But assuming you have a good reason here is my answer.

Firstly here is a tutorial I found on Entity framework, crud and webforms. The entity framework code is a little out of date now. But if you understand the console app demos that you have mentioned you should be able to see from this how to apply it to webforms.

Going by your description I think what is happening is you are getting confused with how namespaces work. This code will hopefully help with that.

using AnotherNamespace;  //this means the code on the page can access the public 
                         //parts of AnotherNamespace

namespace MyNamespace
{
    //code in here belongs to MyNamespace
}

Below is a simple example of what I think your code should look like.

First your dbcontext

using EFTestSchool.Models;
using System.Data.Entity;

namespace EFTestSchool.Data
{
    public class MyDbContext: DbContext
    {

        public DbSet<Student> Students { get; set; }

        public MyDbContext()
            :base("{your connection string}")
        {
            Database.SetInitializer<MyDbContext>(null);
        }
    }
}

The student model

namespace EFTestSchool.Models
{
    public class Student
    {

        public int Id { get; set; }
        public string Name { get; set; }
    }
}

The Default.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EFTestSchool.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">     
        <asp:GridView ID="Gridview1" runat="server">

        </asp:GridView>
    </form>
</body>
</html>

The code behind, i.e. default.aspx.cs

using EFTestSchool.Data;
using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using EFTestSchool.Models;  //use this in your code to remove the student cannot be found compiler error

namespace EFTestSchool  //needs to stay as this or it won't be able to recognise the controls in the aspx file
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if(!IsPostBack)
            {
                using (var ctx = new MyDbContext())
                {
                    var students = ctx.Students.ToList();
                    Gridview1.DataSource = students;
                    Gridview1.DataBind();
                }
            }    
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Dave, thank you for the help. As I said, I'm just in a learning mode and discovered what you explain since I posted. I will try your logic but it makes sense now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.