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();
}
7
  • 1
    Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it? Commented Mar 26, 2018 at 20:03
  • I assume you read the documentation ? 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... Commented Mar 26, 2018 at 20:03
  • Can you attach your pageload event? You should only be binding if (!IsPostback) Commented Mar 26, 2018 at 20:07
  • @maccettura yes I know but hows the first one is working? Commented Mar 26, 2018 at 20:07
  • I mean I can select rows, If I got records on page load Commented Mar 26, 2018 at 20:08

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.