捕获到无效的操作异常 [英] Invalid Operation Exception was caught
问题描述
你好
如果BD中的记录存在于脸颊功能正常的情况下,我会为脸颊编写一个函数,但是在执行插入代码时我会点击(Click(object sender,EventArgs e)按钮以下错误
错误:connectionString属性尚未初始化。
如果我不使用脸颊功能我在按钮的Click事件中插入代码
当我使用脸颊功能时会发生这个错误,
我的脸颊功能代码
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
推荐答案
如果它说connectionString属性还没有初始化。然后很明显为什么:
当你运行EducationfieldExiste
方法时,它总是这样做:
If it's saying that "The connectionString property has not been initialized." then it's pretty obvious why:
When you run yourEducationfieldExiste
method it always does this:
finally
{
con.Dispose();
con.Close();
}
因此,当您尝试立即再次使用相同的连接对象时:
So when you try to use the same connection object again immediately:
if (EducationfieldExiste() == true) //--------- this condition working right
{
Degreeexist.Visible = true;
}
else
{
SqlCommand cmd2 = new SqlCommand("Insert_Education_Degree_TB", con);
...
con
SqlConnection对象已被销毁 - 您无法再次使用它。
更改代码以仅在完成连接时关闭连接,或者每次都创建新连接(并处理它)。
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.
I已通过此代码解决了错误
I've solved the error by this code
if (con.State == ConnectionState.Open)
{
con.Close();
}
这篇关于捕获到无效的操作异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!