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

Creating New Roles and Permissions Dynamically in Spring Security 3

spring spring-security

I am using Spring Security 3 in Struts 2 + Spring IOC project.

Dynamic binding collection of lists to ListView

wpf listview

I have a listview which has an extra property which I use to create dynamic columns to a Listview

Unit-testing method invocation (template method)?

java unit-testing mocking

Is it possible to unit-test method invocation on the same class(in order and how many times they were invoked) ?