1

I have a gridview on my page. And I have 2 snippets of SQL code to binds that gridview.

First one is run on page load. If there are records returned from the first SQL, I can select a row on gridview.

But the problem is when there is no record returned from the first SQL, I have button that runs another SQL and binds its result to the gridview too. But when I try to select a row, I get this error:

Index was out of range. when I trying to select a row Must be non-negative and less than the size of the collection. Parameter name: index

My code is like that

First SQL (its run on page load)

void listele()
{
    baglanti.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT * From kayitlar where durum='Çözülmedi' or durum='İşlem Yapılıyor'", baglanti);
    DataTable dataTable = new DataTable();
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    da.Fill(dataTable);


    GridView1.DataSource = dataTable;
    GridView1.DataBind();
    baglanti.Close();
}

and thats the second SQL that when runs when I click button

void listelehepsi()
{
    baglanti.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT * From kayitlar", baglanti);
    DataTable dataTable = new DataTable();
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    da.Fill(dataTable);


    GridView1.DataSource = dataTable;
    GridView1.DataBind();
    baglanti.Close();
}

and this is the GridView1_SelectedIndexChanged event

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    int secili;
    secili = GridView1.SelectedIndex;
    GridViewRow row = GridView1.Rows[secili]; // I GOT ERROR HERE
    TextBox1.Text = row.Cells[1].Text;
}

why Am I getting this error ?

EDIT-- I got solve changing the page load sql like this;

void listele()
{
    baglanti.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT * From kayitlar where durum='Çözülmedi' or durum='İşlem Yapılıyor'", baglanti);
    DataTable dataTable = new DataTable();
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    da.Fill(dataTable);


    GridView1.DataSource = dataTable;
    if (!IsPostBack)
    {
        GridView1.DataBind();
    }
    else
    {
        //
    }
    baglanti.Close();
}
Shelby115
  • 2,816
  • 3
  • 36
  • 52
  • 1
    Possible duplicate of [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – penleychan Mar 26 '18 at 20:03
  • I assume you [read the documentation](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindex(v=vs.110).aspx) ? Did you see the part where it says the default value for `.SelectedIndex` is `-1` (which indicates that no row is currently selected)? You should attach the debugger and see what the value of `secili` is, I bet it would surprise you... – maccettura Mar 26 '18 at 20:03
  • Can you attach your pageload event? You should only be binding if (!IsPostback) – FJT Mar 26 '18 at 20:07
  • @maccettura yes I know but hows the first one is working? – Kerem Aygül Mar 26 '18 at 20:07
  • I mean I can select rows, If I got records on page load – Kerem Aygül Mar 26 '18 at 20:08
  • okay I got solve. @FJT I didnt add !IsPostBack on page load but I added on the function – Kerem Aygül Mar 26 '18 at 20:16
  • Cool - I'll add as an answer so that anyone else with the same issue can find it. – FJT Mar 26 '18 at 20:20

2 Answers2

1

Make sure that you are not rebinding your datagrid on postback - you can do this in your PageLoad event - something like

if (!IsPostback)
{
   ... bind your datagrid
}
FJT
  • 1,923
  • 1
  • 15
  • 14
0

In the GridView1_SelectedIndexChanged event, could you simply do a RowCount to see if the value is != 0 before the other code runs?

if (GridView1.RowCount <= 0)
{
    return;
} 
Shelby115
  • 2,816
  • 3
  • 36
  • 52