如何解决invalidoperationexception被捕获错误 [英] How to solve invalidoperationexception was caught error
问题描述
protected void btnUpdate_Click(对象发件人,EventArgs e)
{
尝试
{
string cs = ConfigurationManager.ConnectionStrings [ SSMS跨度>]的ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter(( string )ViewState [ SELECT_QUERY],con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataSet ds =(DataSet)ViewState [ DATASET];
if (ds.Tables [ 学生]。Rows.Count > 0 )
{
DataRow dr = ds.Tables [ 学生]。行[ 0 跨度>];
dr [ 名称] = txtStudentName.Text;
dr [ Gender] = ddlGender.SelectedItem;
dr [ TotalMarks] = txtTotalMarks.Text;
dr [ Id] = txtStudentId.Text;
}
da.Update(ds, 学生 );
}
catch (Ex Ex)
{
}
}
}
我尝试过:
i我试图运行此代码然后我得到错误invalidoperationexception被捕获在此代码
所以使用调试器,并逐步执行代码。
在该行上设置一个断点:
string cs = ConfigurationManager.ConnectionStrings [< span class =code-string> SSMS]。ConnectionString;并逐步查看代码在每个变量中。如果执行跳转到
catch
块,那么它与它尝试执行的最后一行有关 - 所以移动你的断点并再次运行它。这一次,仔细看看究竟在哪里。导致问题的原因应该是相当明显的,这应该告诉你从哪里开始寻找发生这种情况的原因。或者至少有更好的信息可以在你提出问题时给我们提供! :笑:
我们无法为您执行任何操作:我们无法运行您的代码,也无法访问您的数据!
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
string cs = ConfigurationManager.ConnectionStrings["SSMS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter((string)ViewState["SELECT_QUERY"],con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataSet ds=(DataSet)ViewState["DATASET"];
if (ds.Tables["Students"].Rows.Count > 0)
{
DataRow dr = ds.Tables["Students"].Rows[0];
dr["Name"] = txtStudentName.Text;
dr["Gender"] = ddlGender.SelectedItem;
dr["TotalMarks"] = txtTotalMarks.Text;
dr["Id"] = txtStudentId.Text;
}
da.Update(ds, "Students");
}
catch(Exception Ex)
{
}
}
}
What I have tried:
i am trying to run this code then i get error invalidoperationexception was caught in this code
So use the debugger, and step through the code.
Put a breakpoint on the line:
string cs = ConfigurationManager.ConnectionStrings["SSMS"].ConnectionString;And step through teh code looking at exactly what is in each variable. If the execution jumps to the
catch
block, then it's something to do with the last line it tried to execute - so move your breakpoint and run it again. This time, look very closely at exactly what is in where. It should be fairly obvious what is causing the problem, and that should tell you where to start looking for why that is happening. Or at least have much better information to give us when you ask a question about it! :laugh:
We can't do any of that for you: we can't run your code, and don't have access to your data!
这篇关于如何解决invalidoperationexception被捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!