运行时遇到报错

liangjinzhe 2010-03-02 11:45:54
Default.aspx.cs文件
using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

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

}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString =
WebConfigurationManager.ConnectionStrings["MyConnectionString "].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
try
{
con.Open();
Label2.Text = "<b>Server Version:</b>" + con.ServerVersion;
Label2.Text = "<br /><b>Connection Is:</b> " + con.State.ToString();

}
catch (Exception err)
{
Label2.Text = "Error reading the database. " + Error.Message;
}
finally
{
con.Close();
Label2.Text +="<br /><b>Now Connection Is:</b> " +
con.State.ToString();
}
}
}

Web.config文件
<?xml version="1.0"?>

<configuration>
<appSettings/>
<system.web>

<compilation debug="true"/>

<authentication mode="Windows"/>

</system.web>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=localhost;Initial Catalog=dbname;user id=admin;password=admin"/>
</connectionStrings>
</configuration>

在Default.aspx.cs文件中点击F5的话提示
1.当前上下文中不存在名称"WebConfigurationManager"
2.事件"System.Web.UI.TemplateControl.Error"只能出现在 += 或 -=的左边
...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangjinzhe 2010-03-02
  • 打赏
  • 举报
回复
using System.Configuration; 这不是已经引用了命名空间了么? 为什么还提示1.当前上下文中不存在名称"WebConfigurationManager"

如果没有命名空间,那应该写什么啊?

为什么会显示 Error reading the database. ConnectionString 属性尚未初始化。

然而填写string connectionString; 的话 显示 已在此范围定义了名为"connectionString"的局部变量

liangjinzhe 2010-03-02
  • 打赏
  • 举报
回复
还有 using System.Configuration; 这不是已经引用了命名空间了么? 为什么还提示1.当前上下文中不存在名称"WebConfigurationManager"
liangjinzhe 2010-03-02
  • 打赏
  • 举报
回复
点击按钮提示 Error reading the database. ConnectionString 属性尚未初始化。
然后重新填写string connectionString; 的话 显示 已在此范围定义了名为"connectionString"的局部变量
丰云 2010-03-02
  • 打赏
  • 举报
回复
string connectionString =
WebConfigurationManager.ConnectionStrings["MyConnectionString "].ConnectionString;
改下:

string connectionString = System.Configuration.ConfigurationSettings.AppSettings["MyConnectionString"];
symbol_bc 2010-03-02
  • 打赏
  • 举报
回复
1.很明显你没有引用命名空间
2.是一个事件的注册错误,但你的代码里没有写
zhang_yugang 2010-03-02
  • 打赏
  • 举报
回复
引用 1 楼 foren_whb 的回复:
错误提示说得很清楚了。。。。

  catch (Exception err)
        {
            Label2.Text = "Error reading the database. " + Error.Message;
        }
改下:
  catch (Exception err)
        {
            Label2.Text = "Error reading the database. " + err.Message;
        }

正解
丰云 2010-03-02
  • 打赏
  • 举报
回复
错误提示说得很清楚了。。。。

catch (Exception err)
{
Label2.Text = "Error reading the database. " + Error.Message;
}
改下:
catch (Exception err)
{
Label2.Text = "Error reading the database. " + err.Message;
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧