Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I write a function for cheek if the records exists in BD the cheek function working right,but in the (Click(object sender, EventArgs e) button when execute the insert code I face to below error
Error:The connectionString property has not been initialized.
If I don’t use from cheek function my insert code in the Click event of button working right
When I use from cheek function this error happen,
My cheek function code
public bool EducationfieldExiste()
{
        SqlCommand cmdchek = new SqlCommand("ChekRecordExist", con);
        cmdchek.CommandType = CommandType.StoredProcedure;
        int ID = Convert.ToInt32(Session["PersonID"].ToString());
        cmdchek.Parameters.AddWithValue("@PersonID", ID.ToString());
        cmdchek.Parameters.AddWithValue("@FieldID", DropDownList_PersonField.SelectedValue.ToString());
        cmdchek.Parameters.AddWithValue("@SubFieldID", DropDownList_PersonSubField.SelectedValue.ToString());
        cmdchek.Parameters.AddWithValue("@Degree", DropDownList_PersonDegree.SelectedValue.ToString());
        cmdchek.Connection = con;
        try
        {
            con.Open();
            SqlDataReader read;
            read = cmdchek.ExecuteReader();
            if (read.HasRows)
            {

                return true;
            }
            else
            {
                return false;

            }
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            con.Dispose();
            con.Close();
        }
}

My insert code in event button
protected void ButtonEducationDgree_Click(object sender, EventArgs e)
{
    if (EducationfieldExiste() == true) //--------- this condition working right
    {
        Degreeexist.Visible = true;
    }
    else
    {
            SqlCommand cmd2 = new SqlCommand("Insert_Education_Degree_TB", con);
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.Parameters.AddWithValue("@Person_ID", Session["PersonID"].ToString());
            cmd2.Parameters.AddWithValue("@Field_ID", DropDownList_PersonField.SelectedValue.ToString());
            cmd2.Parameters.AddWithValue("@SubField_ID", DropDownList_PersonSubField.SelectedValue.ToString());
            cmd2.Parameters.AddWithValue("@Degree_Education", DropDownList_PersonDegree.SelectedValue.Trim());
            cmd2.Parameters.AddWithValue("@University_Name", TextBox_UniversityNmae.Text.Trim());
            cmd2.Parameters.AddWithValue("@Data_Graduated", TextBox_DateGraduated.Text.Trim());
            cmd2.Parameters.AddWithValue("@Experience_Job", DropDownList_Experience.SelectedValue);
            try
            {
                con.Open();
                cmd2.ExecuteNonQuery();
                cmd2.Parameters.Clear();
                Labelsucces.Visible = true;
                Labelsucces.Text = "Insert Education was Sucsessfuly";
                TextBox_UniversityNmae.Text = "";
                TextBox_DateGraduated.Text = "";
                LblErorr.Visible = false;
            }
            Catch //----------Here is the Error
            {

                LblErorr.Visible = true;
                LblErorr.Text = "Insert Education was failed try again";
                Labelsucces.Visible = false;

            }
            finally
            {
                con.Close();
                con.Dispose();
            }//--------------------11111111111111111111--------------

    }//------------------- end of else cheekrecord

}//--------end of Btton Education

Please help me thanks
Posted
Updated 28-Feb-15 1:10am
v2

If it's saying that "The connectionString property has not been initialized." then it's pretty obvious why:
When you run your EducationfieldExiste method it always does this:
C#
finally
{
    con.Dispose();
    con.Close();
}
So when you try to use the same connection object again immediately:
C#
if (EducationfieldExiste() == true) //--------- this condition working right
{
    Degreeexist.Visible = true;
}
else
{
    SqlCommand cmd2 = new SqlCommand("Insert_Education_Degree_TB", con);
    ...
the con SqlConnection object has been destroyed - you can't use it again.

Either change your code to close the connection only when you are finished with it, or create a new connection (and dispose it) each time.
 
Share this answer
 
Comments
Member 11240896 28-Feb-15 8:00am    
If i do not use from try catch in EducationfieldExiste method like this
public bool EducationfieldExiste()
{
SqlCommand cmdchek = new SqlCommand("ChekRecordExist", con);
cmdchek.CommandType = CommandType.StoredProcedure;
int ID = Convert.ToInt32(Session["PersonID"].ToString());
cmdchek.Parameters.AddWithValue("@PersonID", ID.ToString());
cmdchek.Parameters.AddWithValue("@FieldID", DropDownList_PersonField.SelectedValue.ToString());
cmdchek.Parameters.AddWithValue("@SubFieldID", DropDownList_PersonSubField.SelectedValue.ToString());
cmdchek.Parameters.AddWithValue("@Degree", DropDownList_PersonDegree.SelectedValue.ToString());
cmdchek.Connection = con;

con.Open();
SqlDataReader read;
read = cmdchek.ExecuteReader();
if (read.HasRows)
{

return true;
}
else
{
return false;

}
con.Close();//Warning: Unreachable code detected


}

I face to this Warning: Unreachable code detected I mean the line of con.Close(); do not Execute where i close the connection?
OriginalGriff 28-Feb-15 8:41am    
Well yes.

if (a) return true;
else return false;
// You can't reach any code down here!

Seriously? The problem isn't that you are using con - it's that your method closes and disposes it - then the code that called the method tried to use it again.
You should probably consider creating a new connection in each method, rather than trying to rely on a single global connection.
Member 11240896 28-Feb-15 11:53am    
Pleas write an example for this method or reform the method thanks
public bool EducationfieldExiste()
{
SqlCommand cmdchek = new SqlCommand("ChekRecordExist", con);
cmdchek.CommandType = CommandType.StoredProcedure;
int ID = Convert.ToInt32(Session["PersonID"].ToString());
cmdchek.Parameters.AddWithValue("@PersonID", ID.ToString());
cmdchek.Parameters.AddWithValue("@FieldID", DropDownList_PersonField.SelectedValue.ToString());
cmdchek.Parameters.AddWithValue("@SubFieldID", DropDownList_PersonSubField.SelectedValue.ToString());
cmdchek.Parameters.AddWithValue("@Degree", DropDownList_PersonDegree.SelectedValue.ToString());
cmdchek.Connection = con;

con.Open();
SqlDataReader read;
read = cmdchek.ExecuteReader();
if (read.HasRows)
{

return true;
}
else
{
return false;

}
con.Close();//Warning: Unreachable code detected
}
OriginalGriff 28-Feb-15 12:08pm    
Certainly.
Where should I send the pro-forma invoice, or do you just want to give me your credit card details and I'll take what I want?

We don't do your work (or your homework) - this isn't a complex problem, and if you can write that code to start with you should be able to work out what you have to do to fix it. I've told you what the problem is, and what you have to do: now think about your code, and why it gives problems.

Even if I did fix that bit, how would that help you to know where else in your code you are going to face the same problem? :laugh:
Member 11240896 28-Feb-15 13:24pm    
the problem is in this line con.Close();//Warning: Unreachable code detected
the close connection dose not execute at end of Else condition
Okay thanks for your help
I will find it
I've solved the error by this code

C#
if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
 
Share this answer
 

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