System.Data.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理 [英] An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
问题描述
我有一个gridview,其值为一列
< asp:HyperLinkField DataNavigateUrlFields =runIdDataTextField =PercentAnalysed ControlStyle-CssClass =hlinkHeaderText =%ANALYZEDItemStyle-Width =6%DataNavigateUrlFormatString =runanalysis.aspx?runId = {0}ItemStyle-Font-Underline =true/>
由以下代码决定。
protected void GridView2_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
GridViewRow item = e.Row;
// int myvar;
//Int32.TryParse(item.Cells[0].Text,out myvar);
SqlConnection con = new SqlConnection(connectionstring.ToString());
string selectSQL =SELECT COUNT(*)AS'Count'FROM Analyzed WHERE runId = @ myvar group by runId;
SqlCommand cmd = new SqlCommand(selectSQL,con);
cmd.Parameters.AddWithValue(@ myvar,item.Cells [0] .Text);
SqlDataReader阅读器;
尝试
{con.Open();
reader = cmd.ExecuteReader();
reader.Read();
if(item.Cells [8] .Text.Equals(0))
item.Cells [13] .Text =0;
else
if(reader [Count]。ToString()。Equals(0))
item.Cells [13] .Text =0 ;
else
item.Cells [13] .Text = reader [Count]。ToString();
reader.Close();
}
}
finally
{
con.Close();
}
}
}
<
这个异常还说,我试图在没有值的时候读取一个值。
我分别执行了查询,结果很好,但是在某些情况下没有数据存在。所以我插入了这个检查:
if(reader [Count]。ToString()。Equals())
但仍然是异常。
有什么想法吗?
一些虚拟的值。列获得的价值,但它是非常奇怪,它不是一个超链接。
if(reader [Count] .ToString()。Equals())
也会尝试读取数据。所以你必须在阅读前检查reader.HasRows()
。
代码评论:
if(e.RowType == DataControlRowType.DataRow)
{
GridViewRow item = e.Row;
//在这里考虑这个条件。 (item.Cells [8] .Text.Equals(0))
{
item.Cells [13]),不需要执行连接和命令, .Text =0;
return;
}
//你应该总是试着用这个东西来使用?
//使用(var sqlConnection = new SqlConnection(connectionstring)
SqlConnection con = new SqlConnection(connectionstring.ToString());
$ b $ string selectSQL =SELECT COUNT(*)AS'Count'FROM Analyzed WHERE runId = @ myvar group by runId;
SqlCommand cmd = new SqlCommand(selectSQL,con);
cmd.Parameters.AddWithValue(@ myvar, item.Cells [0] .Text);
SqlDataReader reader;
try
{con.Open();
//在这里使用命令行为怎么样? .ExecuteReader(CommandBehavior.CloseConnection)
reader = cmd.ExecuteReader();
if(reader.HasRows())
{
reader.Read();
// if(item.Cells [8] .Text.Equals(0))//这个检查应该作为方法
中的第一件事//item.Cells[13].Text = 0;
// else
// {
if(reader [Count]。ToString()。Equals(0))
item.Cells [ 13] .Text =0;
else
item.Cells [13] .Text = reader [Count]。ToString();
reader.Close();
//
}
}
finally
{
con.Close();
}
I have a gridview whose value for a column
<asp:HyperLinkField DataNavigateUrlFields="runId" DataTextField="PercentAnalysed" ControlStyle-CssClass="hlink" HeaderText="% ANALYSED" ItemStyle-Width="6%" DataNavigateUrlFormatString="runanalysis.aspx?runId={0}" ItemStyle-Font-Underline="true"/>
is decided by the below code.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow item = e.Row;
//int myvar;
//Int32.TryParse(item.Cells[0].Text, out myvar);
SqlConnection con = new SqlConnection(connectionstring.ToString());
string selectSQL = " SELECT COUNT(*) AS 'Count' FROM Analysed WHERE runId =@myvar group by runId";
SqlCommand cmd = new SqlCommand(selectSQL, con);
cmd.Parameters.AddWithValue("@myvar", item.Cells[0].Text);
SqlDataReader reader;
try
{ con.Open();
reader = cmd.ExecuteReader();
reader.Read();
if (item.Cells[8].Text.Equals("0"))
item.Cells[13].Text = "0";
else
{
if (reader["Count"].ToString().Equals("0"))
item.Cells[13].Text = "0";
else
item.Cells[13].Text = reader["Count"].ToString();
reader.Close();
}
}
finally
{
con.Close();
}
}
}
I tried debugging it and it gives this exception.
The exception also says additionally that I am trying to read a value when no value is present.
I executed the query separately and the results are fine but for some cases no data is present. So I inserted this check :
if (reader["Count"].ToString().Equals(""))
but still the same exception.
Any idea ?
Also,I just experimented with some dummy value.The column gets that value but it is very weird that it is no more a hyperlink.
if (reader["Count"].ToString().Equals(""))
will also try to read the data. SO you have to check "reader.HasRows()"
before you try to read it.
Might not be mandatory, but I think you should consider below comments I am making.
Code Review :)
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow item = e.Row;
// Consider this condition here. No need to execute connection and command if u hard coding the value
if (item.Cells[8].Text.Equals("0"))
{
item.Cells[13].Text = "0";
return;
}
// You should always try to do this thing in using?
//using (var sqlConnection = new SqlConnection(connectionstring)
SqlConnection con = new SqlConnection(connectionstring.ToString());
string selectSQL = " SELECT COUNT(*) AS 'Count' FROM Analysed WHERE runId =@myvar group by runId";
SqlCommand cmd = new SqlCommand(selectSQL, con);
cmd.Parameters.AddWithValue("@myvar", item.Cells[0].Text);
SqlDataReader reader;
try
{ con.Open();
// What about using command behaviour here?? e.g., command.ExecuteReader(CommandBehavior.CloseConnection)
reader = cmd.ExecuteReader();
if(reader.HasRows())
{
reader.Read();
//if (item.Cells[8].Text.Equals("0")) // this check should go as first thing in method
//item.Cells[13].Text = "0";
//else
//{
if (reader["Count"].ToString().Equals("0"))
item.Cells[13].Text = "0";
else
item.Cells[13].Text = reader["Count"].ToString();
reader.Close();
//}
}
}
finally
{
con.Close();
}
这篇关于System.Data.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!