DropDownList in Gridview Binding error Index 1 is either negative or above rows count

I have been trying to bind a drop-down list which is present in itemtemplate of my GridView. But it is giving me the above said error of "Index 1 is either negative or above rows count."

My GridView contains ~100 of rows and I want to bind each drop-down list with specific data. The error is occurring at onRowDataBoundEvent and an exception(by the debug tool) is outputting at the GridBinding method to bind GridView with the main data.

Here is the code:

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            try
            {
                DropDownList _ddtpcs = (DropDownList)e.Row.Cells[4].FindControl("_ddtsttpc");
                if (_ddtstsubs.SelectedIndex != 0 && _ddtstsubs.SelectedIndex != -1)
                {
                    if (_ddtpcs != null)
                    {
                        _ddtpcs.DataSource = _adl.LoadTopics(long.Parse(_ddtstsubs.SelectedValue));
                           _ddtpcs.DataTextField = "top_nm";
                           _ddtpcs.DataValueField = "top_id";
                           _ddtpcs.DataBind();
                           _ddtpcs.Items.Insert(0, new ListItem("Select", "0"));
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", POPUPBuilder.ShowPopUpMsg(ex.ToString()), true);
            }
        }

Here is the code for gridbinding:

 _grdqans.DataSource = _adl.LoadQans(long.Parse(_hdntstId.Value));
                _grdqans.DataBind();

The drop-down list in GridView is:

 <ItemTemplate>
        <asp:DropDownList ID="_ddtsttpc" runat="server">
    </asp:DropDownList>
</ItemTemplate>

the LoadTopics method

 internal object LoadTopics(long _subId)
    {
        try
        {
            cmd = new SqlCommand("sp_LoadTopics", con.getconnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@subId", _subId);
            da = new SqlDataAdapter(cmd);
            dt.Clear();
            da.Fill(dt);
            cmd.Parameters.Clear();
            cmd.Connection.Close();
            cmd.Dispose();
            //da.Dispose();
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.closeconnection();
        }
    }

What am I doing wrong here?

Answers


update LoadTopics method with below code

DataTable topicDt = new DataTable();
using(SqlConnection con = new SqlConnection(connectionString))
using(SqlCommand cmd = new SqlCommand("sp_LoadTopics",con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@subId", _subId);
    con.Open();
    using(SqlDataAdapter da = new SqlDataAdapter(cmd))
    {
        da.Fill(topicDt);
    }
}

return topicDt;

In your code you are using many class level objects. Those objects can be null or connections etc can be closed. better to have method level objects specially when you working with database. you can reuse connection strings like constants.


Need Your Help

xsl:variable is not defined in predicate

xslt xls predicate

It looks like the xsl:variables are not defined in the predicates in libxml2.

How to generate dynamic multi-dimensional list or array and insert data accordingly based on a dynamic SQL Server table in c#?

c# algorithm list insert dynamic-arrays

I want to create a dynamic 2-dimensional array (or any other structure) based on a dynamic database table in C# or T-SQL, which means the data source (which is a database table) is dynamic too.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.