ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;提示未将设置引用到对象实例
后台代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usi...
后台代码:
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 WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter mysqlada = new SqlDataAdapter("select * from Stu",str);
DataSet myset = new DataSet();
mysqlada.Fill(myset,"News");
GridView1.DataSource = myset.Tables["News"].DefaultView;
GridView1.DataBind();
}
}
}
web.config里配置代码;
<?xml version="1.0" encoding="utf-8"?>
<!-- 有关使用 web.config 转换的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionString>
<add name="Connection" connectionString="Server=.;uid=sa;pwd=123;database=Student"/>
</connectionString>
<system.web>
</system.web>
</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;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter mysqlada = new SqlDataAdapter("select * from Stu",str);
DataSet myset = new DataSet();
mysqlada.Fill(myset,"News");
GridView1.DataSource = myset.Tables["News"].DefaultView;
GridView1.DataBind();
}
}
}
web.config里配置代码;
<?xml version="1.0" encoding="utf-8"?>
<!-- 有关使用 web.config 转换的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionString>
<add name="Connection" connectionString="Server=.;uid=sa;pwd=123;database=Student"/>
</connectionString>
<system.web>
</system.web>
</configuration> 展开
1个回答
展开全部
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; SqlDataAdapter mysqlada = new SqlDataAdapter("select * from Stu",str);
DataSet myset = new DataSet();
mysqlada.Fill(myset,"News");
GridView1.DataSource = myset.Tables["News"].DefaultView;
GridView1.DataBind();
}
}
换成这样 试试呢
{
protected void Page_Load(object sender, EventArgs e)
{
string str = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; SqlDataAdapter mysqlada = new SqlDataAdapter("select * from Stu",str);
DataSet myset = new DataSet();
mysqlada.Fill(myset,"News");
GridView1.DataSource = myset.Tables["News"].DefaultView;
GridView1.DataBind();
}
}
换成这样 试试呢
更多追问追答
追问
不行 还是同样的错误
追答
打断点调试一下 确保是否是 ConfigurationManager.ConnectionStrings["Connection"]为空
System.Configuration 该引用版本是多少啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询