getting error The ConnectionString property has not been initialsed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agarwalsunitadhn
    New Member
    • Jan 2008
    • 82

    getting error The ConnectionString property has not been initialsed

    Hi
    I want to declare a coonectionstrin g in winapps as declared in web.config file in webapps.
    I declare that in app.config as follows
    Code:
    <connectionStrings>
            <add name="ABC_Enterprises_.Properties.Settings.ABCEnterpriseDataConnectionString"
                connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ABCEnterpriseData.mdf;Integrated Security=True;User Instance=True"
                providerName="System.Data.SqlClient" />
        </connectionStrings>
    but now i dnt know how to call this in program.

    Can you please help me
    Thanks
    Last edited by kenobewan; Mar 8 '08, 12:00 PM. Reason: MERGED POSTS
  • agarwalsunitadhn
    New Member
    • Jan 2008
    • 82

    #2
    getting error The ConnectionStrin g property has not been initialsed

    hello
    I am using sql for the first time with dotnet.
    I had created the database and directly using .net by choosing sqldatabase from new item
    and in the form i write the following code. My code is :
    Code:
     string constr = ConfigurationSettings.AppSettings["ConnectionString1"];
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    string str = "insert into OwnerDetails(OwnerId,FirstName,MiddleName,LastName,Age,Occupation,Address,City,State,PinCode,TelNo,Others) values(1,@FirstName,@MiddleName,@LastName,@Age,@Occupation,@Address,@City,@State,@PinCode,@TelNo,@Others)";
                    cmd.Connection = conn;
                    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
                    cmd.Parameters.Add("@MiddleName", SqlDbType.VarChar).Value = txtMName.Text;
                    cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text;
                    cmd.Parameters.Add("@Age", SqlDbType.Int).Value = txtAge.Text;
                    cmd.Parameters.Add("@Occupation", SqlDbType.VarChar).Value = txtOccupation.Text;
                    cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtAddress.Text;
                    cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = cmbCity.SelectedItem.ToString();
                    cmd.Parameters.Add("@State", SqlDbType.VarChar).Value = cmbState.SelectedItem.ToString();
                    cmd.Parameters.Add("@PinCode", SqlDbType.VarChar).Value = txtPincode.Text;
                    cmd.Parameters.Add("@TelNo", SqlDbType.VarChar).Value = txtTelNo.Text;
                    cmd.Parameters.Add("@Others", SqlDbType.VarChar).Value = txtOthers.Text;
    
                    cmd.CommandText = str;
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                    conn.Close();
    
    App.config files setting:
    <add name="ConnectionString1"
                connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NGEnterpriseData.mdf;Integrated Security=True;User Instance=True"
                providerName="System.Data.SqlClient" />
    But after when i ran the application i got the following error
    The ConnectionStrin g property has not been initialsed

    Please suggest me why this error occurs and can i modify it

    Comment

    Working...