【问题标题】:The name [ControlName] missing in current context w/ Database connection当前上下文中缺少名称 [控件名称] 与/数据库连接
【发布时间】:2020-03-13 19:43:16
【问题描述】:

我是 Visual Studio 的新手,正在尝试学习一些 ASP.NET/C# Web 表单教程。在我的代码中,我试图建立与我们公司 SQL Server 的数据库连接。

Visual Studio 2013 SQL Server 2012

我正在关注的教程建议在 Web.config 中配置数据库连接字符串:

<!-- Johns Connection String to the DB -->
  <connectionStrings>
      <add name="DBconnection" providerName="System.Data.SqlClient" connectionString="data source=TheServerName;initial catalog=TheDatabaseName;user id=TheUserName;password=ThePassword;" />
  </connectionStrings>
</configuration>

面向网络的页面是基本的。我只想说我是否建立了联系。这是代码隐藏:

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

namespace CPITraining
{
    public partial class TestForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var connectionFromConfiguration =     WebConfigurationManager.ConnectionStrings["DBConnection"];
            using (SqlConnection dbConnection = new     SqlConnection(connectionFromConfiguration.ConnectionString))
        {
                try
                { 
                    dbConnection.Open();
                    ltConnectionMessage.Text = "Connection Successful.";
                }
                catch (SqlException ex)
                {
                    ltConnectionMessage.Text = "Connection failed: " + ex.Message;
                }
                finally
                {
                    dbConnection.Close();
                    dbConnection.Dispose();
                }
            }
        }
    }
}

最后,这是网页:

<%@ Page Title="Colors Example" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="CPITraining.TestForm" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <h3>Your application description page.</h3>
    <p>Use this area to provide additional information.</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <asp:Literal ID="ltConnectionMessage" runat="server" />
    <div>
        <asp:Literal ID="ltOutput" runat="server" />
    </div>
</asp:Content>

我不断收到名称“ltConnectionMessage”在当前上下文中不存在。

不确定如何解决此错误。谢谢你的帮助。 约翰

【问题讨论】:

  • 您能从代码隐藏文件中粘贴 ltConnectionMessage 定义吗?您可以从网页访问 ltOutput 吗?
  • 那会是 ltConnectionMessage.Text = "连接成功。";输入我发布的声明?
  • 不,TestForm 是部分类。因此,将会有另一个文件(文件名会有所不同,但会在其中定义 TestForm 类。展开您的解决方案视图,您将在此表单文件的同一级别看到)。该文件将具有控制定义。但是您可以访问 ltOutput 吗?
  • 谢谢,我回家再看看
  • 感谢@sam 的帮助,但我需要倒带。我读了一些部分课程。似乎分部类允许您在多个文件上设置单个类函数,然后在构建项目时将文件编译为单个类。虽然我使用的教程讲授了 SQL 服务器连接代码,但作者和示例代码并没有解释部分类(或定义的其余部分在哪里)。所以对我来说,没有全貌的教程是没有用的。我会试着找到别的东西。对于您关于 ltOutput 的问题 - 它什么也不做。谢谢。

标签: c# asp.net connection-string


【解决方案1】:

我找到了三个更好的教程,它们共同提供了一种在 ASP.NET 中使用 C# 建立网格视图的合理构造方法。我不确定在此处发布教程是否合适。我不希望它看起来像我在隐瞒信息,但我也不希望它看起来像我在为教程做广告。本教程的重点也与此线程的主题无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2014-12-09
    • 1970-01-01
    • 2019-11-09
    • 2013-10-02
    • 2014-04-22
    相关资源
    最近更新 更多