Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

c# - An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...