Solve the error "The ConnectionString property has not been initialized."?

28,083

Solution 1

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".

Solution 2

From the examples i see online, in your connection string replace "DataSource" with "Data Source" (with a space between the two words). http://msdn.microsoft.com/en-us/library/ms156450.aspx

Solution 3

I was getting same error, even after made all changes you mentioned above. And after that, I change my code in that way:

My Web.Config:

<connectionStrings>
    <add name="conStr" connectionString="Data Source=SomeDS;Initial Catalog=SomeCatalog;User Id=myId;Password=myPass" />
</connectionStrings>

My old Code:

string ConnectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;


        using (var sqlConnection = new SqlConnection(ConnectionString ))
        {
            var result= sqlConnection.Query<My_Class>("MY_PROC_NAME", new { count }, commandType: CommandType.StoredProcedure);
            return result;
        }

My newCode:

var dt = new DataTable();

                using (var cnn = new SqlConnection(ConnectionString ))
                using (var cmd = new SqlCommand("MY_PROC_NAME", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cnn.Open();
                    var sqlReader = cmd.ExecuteReader();
                    if (sqlReader.HasRows)
                        dt.Load(sqlReader);

                    cnn.Close();
                }
                result = dt.DataTableToList<My_Class>();

DataTableToList is a converter method, if you need i can share it, too.

Share:
28,083
VJain
Author by

VJain

I am a computer science student and i just love programming.

Updated on February 06, 2020

Comments

  • VJain
    VJain about 4 years

    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:             }