且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解决错误" ConnectionString属性尚未初始化&QUOT ;?。

更新时间:2022-10-15 15:50:36

该错误是只在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".