Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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:             }
like image 524
VJain Avatar asked Jul 07 '13 05:07

VJain


People also ask

How do I fix the ConnectionString property has not been initialized C#?

Solution 2string abc = ConfigurationSettings. AppSettings["databasepath"]; now pass it into sqlconnection. all the best.

What does it mean that the ConnectionString property has not been initialized?

Re: ConnectionString property has not been initialized- issue. Hi, This may happen if you are not populating the SQL connection string parameters (i.e. Server, Username, Password, etc) at install time into your config file.

Where do I put ConnectionString in web config?

config file in the Views folder.) Find the <connectionStrings> element: Add the following connection string to the <connectionStrings> element in the Web. config file.

Which is the ConnectionString property?

The value of the ConnectionString property is a connection string that includes the source database name and the parameters you need to establish the connection. The default value of the ConnectionString property is an empty string. The Server attribute is mandatory in all situations.


Video Answer


2 Answers

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".
like image 100
R.C Avatar answered Oct 31 '22 16:10

R.C


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

like image 24
asafrob Avatar answered Oct 31 '22 15:10

asafrob