Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

How to test a data connection to SQL Server in the web.config

$
0
0

Is there a way to test a data connection to SQL Server in the web.config to make sure that you have connected to the database correctly?, see below. I created a simple application to open a table and display each row. Every time I run this application I get an error: Object reference not set to an instance of an object in the"foreach (GEW.Models.Product prod in GetProducts())" in Listing.aspx. I don't think I have a correct connection. I need to be able to see that the connection was open and made correctly.

I have run this identical code with another database and another table and it worked fine. When I switched to another database and different table the error started.

======================================================================
Listing.aspx
======================================================================

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Listing.aspx.cs" Inherits="GEW.Pages.Listing" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Products</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%foreach (GEW.Models.Product prod in GetProducts()) {
              Response.Write("<div class='item'>");
              Response.Write(prod.Description);
              Response.Write("</div>");
          }
        %>
    
    </div>
    </form>
</body>
</html>


======================================================================
Listing.aspx.cs
======================================================================

using System;
using System.Collections.Generic;
using GEW.Models;
using GEW.Models.Repository;
using System.Linq;

namespace GEW.Pages {
    public partial class Listing : System.Web.UI.Page {
        private Repository repo = new Repository();

        protected void Page_Load(object sender, EventArgs e) {
        }
        protected IEnumerable<Product> GetProducts() {
            return repo.ProductsTbl;
        }
    }
}



======================================================================
Web.config
======================================================================

<?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>
  <connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=SAMSUNG-PC;Initial Catalog=OrdersSQL;Integrated
Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>


======================================================================
Error Message: Object reference not set to an instance of an object.
======================================================================


Object reference not set to an instance of an object.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack

trace for more information about the error and where it originated in the code.

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:



Line 10:     <form id="form1" runat="server">
Line 11:     <div>
Line 12:         <%foreach (GEW.Models.Product prod in GetProducts()) {
Line 13:               Response.Write("<div class='item'>");
Line 14:               Response.Write(prod.Description);
 

 Source File:  c:\Users\SAMSUNG\Documents\Visual Studio 2012\Projects\GEW\GEW\Pages\Listing.aspx    Line:  12


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>