解决错误" ConnectionString属性尚未初始化&QUOT ;?。 [英] Solve the error "The ConnectionString property has not been initialized."?

查看:329
本文介绍了解决错误" ConnectionString属性尚未初始化&QUOT ;?。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是查看在GridView中的所有数据

的Web.config code是

 <结构>
  <&是connectionStrings GT;
    <添加名称=构造的connectionString =数据源=;集成安全性= SSPI;初始目录= sshopping/>
  < /&是connectionStrings GT;
  <&的System.Web GT;
    <编译调试=真targetFramework =4.5/>
    <的httpRuntime targetFramework =4.5/>
  < /system.web>
< /结构>

有codeD在外部类<​​/ P>

 命名空间DBAction
{
    公共类ViewAction这一
    {
        公共数据集GetAllData()
        {
                CMD的SqlCommand = DataConnection.GetConnection()CreateCommand()。
                cmd.CommandText =选择用户名,密码,角色名,EMAILID,SecurityQuestion,SecurityAnswer,LastLogin从LoginInfo类;
                SqlDataAdapter的大=新SqlDataAdapter的(CMD);
                DataSet的DS =新的DataSet();
                da.Fill(DS);
                cmd.Dispose();
                DataConnection.CloseConnection();
                返回DS;
        }
    }
}

这是给在网上误差 da.Fill(DS)
在code绑定数据源与GridView的是这样的页面加载codeD。

 的DataSet DS =新ViewAction这一()GetAllData()。
        gvLoginInfo.DataSource = DS;
        gvLoginInfo.DataBind();

和conectionstring $ C $在数据连接类C

 公共静态的SqlConnection的getConnection()
        {            如果(CON == NULL)
            {
                CON =新的SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings [构造]的ConnectionString。
                con.Open();
            }             返回CON;
        }

和一一误差

 异常详细信息:System.ArgumentException:关键字不支持:数据源。源错误:
第19行:{
第20行:CON =新的SqlConnection();
第21行:con.ConnectionString = ConfigurationManager.ConnectionStrings [构造]的ConnectionString。
第22行:con.Open();
第23行:}


解决方案

该错误是只在Web.Config中。请把数据源之间的一个空间的connectionString为:数据源。因此,您的连接字符串将成为:

 数据源=;集成安全性= SSPI;初始目录= sshopping。

My code is view all the data in the gridview

Web.config code is

<configuration>
  <connectionStrings>
    <add name="ConStr" connectionString="DataSource=.;Integrated Security=SSPI;Initial catalog=sshopping"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
</configuration>

It is coded in external class

namespace DBAction
{
    public class ViewAction
    {
        public DataSet GetAllData()
        {
                SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
                cmd.CommandText = "Select UserName,Password,RoleName,EmailID,SecurityQuestion,SecurityAnswer,LastLogin from LoginInfo";
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();  
                da.Fill(ds);
                cmd.Dispose();
                DataConnection.CloseConnection();
                return ds;
        }
    }
}

it is giving error in line da.Fill(ds) The code to bind data source with gridview is coded on page load like this.

 DataSet ds = new ViewAction().GetAllData();
        gvLoginInfo.DataSource = ds;
        gvLoginInfo.DataBind();

And conectionstring code in data connection class is

 public static SqlConnection GetConnection()
        {

            if (con == null)
            {
                con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
                con.Open();
            }

             return con;
        }

And one one error is

Exception Details: System.ArgumentException: Keyword not supported: 'datasource'.

Source Error:


Line 19:             {
Line 20:                 con = new SqlConnection();
Line 21:                 con.ConnectionString =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
Line 22:                 con.Open();
Line 23:             }

解决方案

The error is in the Web.Config only. Please put one space between DataSource in connectionString as: Data Source. Thus your connection String will become:

 "Data Source=.;Integrated Security=SSPI;Initial catalog=sshopping".

这篇关于解决错误&QUOT; ConnectionString属性尚未初始化&QUOT ;?。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆