hey all,
i have a little issue here with the below however the wired thing that i encoutred is that the below code run only for one time perfectly and when i invoked it again it displayes the error : " The ConnectionString property has not been initialized. "

 private void Label1_TextChanged(object sender, EventArgs e)
        {


            // the below Snippet is for Searching the database Access and Retirving it in the database
            if (Label2.Text == "EDIT")
            {
                using (AMSCONN)
                {
                    string UpdateString = "SELECT EmpCode,EfCode,Branch,SectorName,EmpType,SuperVisor,EmpName,Govenment,District,Area,Email,addrs FROM AddressBook WHERE EmpCode = '" + ForSearch.Text + "'";

                    }
                    AMSCONN.Open();  // here is where the error happen but it only happen's when the code is invoked for the second time
                    MessageBox.Show(ex.Message);
                    OleDbCommand UpdateCMD = new OleDbCommand(UpdateString, AMSCONN);
                    OleDbDataAdapter UpdateAD = new OleDbDataAdapter(UpdateCMD);
                    DataTable UpdateTable = new DataTable();
                    UpdateAD.Fill(UpdateTable);
                    AMSCONN.Close();
                    EmpTxt.DataBindings.Add("Text", UpdateTable, "EmpCode"); // retriving the enp code that it is used in the app
                    Sector_CB.DataBindings.Add("Text", UpdateTable, "SectorName");
                    FillDropDownList("SELECT SecCode,SecName FROM Sectors WHERE SecCode = " + Sector_CB.Text +"", Sector_CB, "SecCode", "SecName");
                    Name_Txt.DataBindings.Add("Text", UpdateTable, "EmpName");
                    Branch_CB.DataBindings.Add("Text", UpdateTable, "Branch");
                }              
            }

i really tried to close the connection and disposing it after running the code but it dosen't seem to work ,,,, so can any one here help me

Recommended Answers

All 2 Replies

Where is the definition for AMSCONN?

Using statement:

You can instantiate the resource object and then pass the variable to the using statement, but this is not a best practice. In this case, the object remains in scope after control leaves the using block even though it will probably no longer have access to its unmanaged resources. In other words, it will no longer be fully initialized. If you try to use the object outside the using block, you risk causing an exception to be thrown. For this reason, it is generally better to instantiate the object in the using statement and limit its scope to the using block.

commented: that really helped a lot thank you so much +0

@cgeier that really was helpfull how ever i encoutrrede a defrent

 private void Label1_TextChanged(object sender, EventArgs e)
        {


            // the below Snippet is for Searching the database Access and Retirving it in the database
            if (Label2.Text == "EDIT")
            {
                using (AMSCONN)
                {
                    string UpdateString = "SELECT EmpCode,EfCode,Branch,SectorName,EmpType,SuperVisor,EmpName,Govenment,District,Area,Email,addrs FROM AddressBook WHERE EmpCode = '" + ForSearch.Text + "'";

                    }
                    AMSCONN.Open(); 
                    MessageBox.Show(ex.Message);
                    OleDbCommand UpdateCMD = new OleDbCommand(UpdateString, AMSCONN);
                    OleDbDataAdapter UpdateAD = new OleDbDataAdapter(UpdateCMD);
                    DataTable UpdateTable = new DataTable();
                    UpdateAD.Fill(UpdateTable);
                    AMSCONN.Close();
                    EmpTxt.DataBindings.Add("Text", UpdateTable, "EmpCode"); // ERROR : This causes two bindings in the collection to bind to the same property.
                    Sector_CB.DataBindings.Add("Text", UpdateTable, "SectorName");
                    FillDropDownList("SELECT SecCode,SecName FROM Sectors WHERE SecCode = " + Sector_CB.Text +"", Sector_CB, "SecCode", "SecName");  
                    Name_Txt.DataBindings.Add("Text", UpdateTable, "EmpName");
                    Branch_CB.DataBindings.Add("Text", UpdateTable, "Branch");
                }              
            }

the error is "This causes two bindings in the collection to bind to the same property." and it also happened when the code is invoked for the first time
so can you help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.