2016-04-06 11 views
0

C# Code:Warum bekomme ich Null Referenz Ausnahme?

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-Datei (Datei 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 Quelle:

<%@ 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 

Warum bin ich in C# -Code über NULL-Verweis Ausnahme zu bekommen? Der obige Inhalt enthält folgende: - 1. C# -Code 2. XML-Datei (Web.config-Datei) 3. HTML-Quelle Kann mir bitte jemand helfen, dieses Problem zu lösen?

+0

sein, was Linie die Ausnahme wirft? – ChrisS

+0

Wo NULL Ausnahme erscheinen? –

Antwort

0

Ich denke, SPACE in Ihre Verbindung string name ist, sollte diese ähnliche

<add name="DBCS" connectionString="Data Source=USE-PC\ROLEN_SIKHRAKAR;Initial Catalog=FunDS1;Integrated Security=True" 
      providerName="System.Data.SqlClient" /> 
Verwandte Themen