为什么我得到null引用异常?

时间:2016-04-06 10:29:54

标签: c# asp.net ado.net

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FunDepartmentStore1ASPX
{
    public partial class CurrentStoreInventory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnShowCurrentInventory_Click(object sender,
EventArgs e)
        {
            DataTable taStoreItems = new DataTable("StoreItems");
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;//Here is exception

            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM StoreItems", con);
                SqlDataAdapter adStoreItems = new SqlDataAdapter(cmd);
                adStoreItems.Fill(taStoreItems);

                for (int i = 0; i < taStoreItems.Rows.Count; i++)
                {
                    DataRow rcdStoreItem = taStoreItems.Rows[i];

                    ListItem liStoreItem = new ListItem((i + 1).ToString(),rcdStoreItem["ItemNumber"].ToString());
                    ListBox2.Items.Add(liStoreItem);
                }
            }
        }
    }
} 

XML文件(Web.config文件)

 <?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="csFunDS " connectionString="Data Source=USE-PC\ROLEN_SIKHRAKAR;Initial Catalog=FunDS1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="DBCS " connectionString="Data Source=USE-PC\ROLEN_SIKHRAKAR;Initial Catalog=FunDS1;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>  

HTML来源:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Current Store Inventory</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Index"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Item #"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Manufacturer"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Category"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label5" runat="server" Text="Sub-Caregory"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label6" runat="server" Text="Item Name/Description"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label7" runat="server" Text="Size"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label8" runat="server" Text="Unit Price"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="Label9" runat="server" Text="Status"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox4" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox5" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox6" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox7" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox8" runat="server"></asp:ListBox>
                </td>
                <td>
                    <asp:ListBox ID="ListBox9" runat="server"></asp:ListBox>
                </td>
            </tr>
            <tr>
                <td colspan="7">
                    <asp:Button ID="btnShowCurrentInventory" runat="server" 
                        Text="Show Store Items Inventory" onclick="btnShowCurrentInventory_Click" />
                </td>
                <td colspan="2">
                    <asp:Button ID="btnClose" runat="server" Text="Close" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


  [1]: http://i.stack.imgur.com/gapIY.jpg

为什么我在上面的C#代码中得到空引用异常? 以上内容包含以下内容: - 1. C#代码 2. XML文件(Web.config文件) 3. HTML源代码 有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为您的连接字符串名称中有SPACE, 这应该是这样的

io.open()