Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
why this error happened?
Here is tow DropDownList control when I Select a field from first dropdownllist I face to the below error.

The ConnectionString property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Quote:
Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.
Line 203: sqlCmd.Parameters.AddWithValue("@FieldID", DropDownList_PersonField.SelectedValue);
Line 204: sqlCmd.Connection = con;
Line 205: con.Open();
Line 206: SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
Line 207: DataTable dt = new DataTable();
Source File: e:\Final Project\FindEeasyJobs\ManagePersons.aspx.cs Line: 205

---------------
my C# cod
C#
protected void DropDownList_PersonField_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            using (SqlCommand sqlCmd = new SqlCommand())
            {
                sqlCmd.CommandText = "select SubField_ID,SubField_Name_english FROM SubField_TB WHERE Field_ID=@FieldID";
                sqlCmd.Parameters.AddWithValue("@FieldID", DropDownList_PersonField.SelectedValue);
                sqlCmd.Connection = con;
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                DropDownList_PersonSubField.DataSource = dt;
                DropDownList_PersonSubField.DataValueField = "SubField_ID";
                DropDownList_PersonSubField.DataTextField = "SubField_Name_english";
                DropDownList_PersonSubField.DataBind();
                con.Close();
                //Adding "Please select SubField" option in dropdownlist
                DropDownList_PersonSubField.Items.Insert(0, new ListItem("Please select your field", "0"));
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
    }//-------------------end of selectindexchanged
Posted
Updated 12-Feb-15 1:43am
v2
Comments
CHill60 12-Feb-15 7:44am    
Where are you setting the connection string for con?
Member 11240896 12-Feb-15 7:49am    
the setting connection string for con is in the webconfig
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["FindeasyjobsConnectionString"].ConnectionString);
CHill60 12-Feb-15 7:57am    
*where* are you setting the connection string for con - i.e. are you sure it is happening before the code above is run? And are you sure the connection string is being returned in the assignment in your comment?
Member 11240896 12-Feb-15 8:05am    
Before the code was completely fine and worked properly, but when I deleted some repeated records from DB table this problem happened.
CHill60 12-Feb-15 10:34am    
It is highly unlikely that deleting some records from the DB table could possibly cause this error. You must have changed something else as well ... the config possibly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900