You must Sign In to post a response.
  • Category: .NET

    Index out of range exception.

    I have one form in which i run query which gives me data of 7 rows but while i running loop to print 7 rows in dgv . only one row printed and when trying to print next row above error shows at insertion statement of dgv.

    here is my code for reference,

    private void btnsearch_Click(object sender, EventArgs e)
    {
    try
    {
    DateTime frmdate = Convert.ToDateTime(dtpfrom.Text);
    DateTime todate = Convert.ToDateTime(dtpto.Text);
    string datetimefrm = frmdate.ToString("yyyy-MM-dd 00:00:00.000");
    string datetimeto = todate.ToString("yyyy-MM-dd 00:00:00.000");

    if (ckaprov.Checked == true)
    {
    cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Proc_GetIndentBetweenDate";
    cmd.Parameters.Add(new SqlParameter("fromDate", datetimefrm));
    cmd.Parameters.Add(new SqlParameter("@toDate", datetimeto));
    cmd.Connection = con;
    table = new DataTable();
    adapter = new SqlDataAdapter(cmd);
    adapter.Fill(table);
    int count = table.Columns.Count;

    if (table.Rows.Count > 0)
    {
    foreach (DataRow rw in table.Rows)
    {
    string no = rw["Indent No"].ToString();
    string date = rw["Indent Date"].ToString();
    string frm = rw["From Store"].ToString();
    string to = rw["To Store"].ToString();
    string status = rw["Indent Status"].ToString();
    string emergency = rw["Emergency Indent"].ToString();

    for (int i = 0; i <= table.Rows.Count - 1; i++)
    {
    dataGridView1.Rows[i].Cells[1].Value = no;
    dataGridView1.Rows[i].Cells[2].Value = date;
    dataGridView1.Rows[i].Cells[3].Value = frm;
    dataGridView1.Rows[i].Cells[4].Value = to;
    dataGridView1.Rows[i].Cells[5].Value = status;
    dataGridView1.Rows[i].Cells[6].Value = emergency;
    dataGridView1.Rows[i].Cells[7].Value = "Send";
    }
    // dataGridView1.Rows.Add();
    }
    }
    else { MessageBox.Show("No indents present between these dates."); }
    }
    else
    {
    MessageBox.Show("No open indents found , Please check Approved.");
    }
    }
    catch { }
    }
  • #763435
    Issue is in the following code.
    You are not creating instance of the new row.

    for (int i = 0; i <= table.Rows.Count - 1; i++)
    {
    dataGridView1.Rows[i].Cells[1].Value = no;
    dataGridView1.Rows[i].Cells[2].Value = date;
    dataGridView1.Rows[i].Cells[3].Value = frm;
    dataGridView1.Rows[i].Cells[4].Value = to;
    dataGridView1.Rows[i].Cells[5].Value = status;
    dataGridView1.Rows[i].Cells[6].Value = emergency;
    dataGridView1.Rows[i].Cells[7].Value = "Send";
    }

    Solution:

    Create Datatable and add the values in data that data table. Finally assign the data source and bind it to the dataGridView1.

    By Nathan
    Direction is important than speed

  • #763440
    Hello Priyanka Barwad,

    Update this line of code :

    for (int i = 0; i < table.Rows.Count; ++i)
    {
    dataGridView1.Rows[i].Cells[0].Value = no;
    dataGridView1.Rows[i].Cells[1].Value = date;
    dataGridView1.Rows[i].Cells[2].Value = frm;
    dataGridView1.Rows[i].Cells[3].Value = to;
    dataGridView1.Rows[i].Cells[4].Value = status;
    dataGridView1.Rows[i].Cells[5].Value = emergency;
    dataGridView1.Rows[i].Cells[6].Value = "Send";
    }

    -->> If your 0th cell does contain any control then start from Cells[1]. <<--

    i <= table.Rows.Count - 1

    This was causing an error because there was not 7 rows.

    Hope this will solve your issue. Let me know if you still facing the problem.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763451
    Index out of range error occurs when you are trying to fetch some array index which is not exist.
    it looks you have start your cell with 1 index not with 0, It should start with 0 and for loop should go till count-1
    so the code will look as below

    for (int i = 0; i < table.Rows.Count-1; i++)
    {
    dataGridView1.Rows[i].Cells[0].Value = no;
    dataGridView1.Rows[i].Cells[1].Value = date;
    dataGridView1.Rows[i].Cells[2].Value = frm;
    dataGridView1.Rows[i].Cells[3].Value = to;
    dataGridView1.Rows[i].Cells[4].Value = status;
    dataGridView1.Rows[i].Cells[5].Value = emergency;
    dataGridView1.Rows[i].Cells[6].Value = "Send";
    }

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763495
    Hi,

    First off all I'm confused with the code, using nested foreach what you want?


    foreach (DataRow rw in table.Rows)
    {
    string no = rw["Indent No"].ToString();
    string date = rw["Indent Date"].ToString();
    string frm = rw["From Store"].ToString();
    string to = rw["To Store"].ToString();
    string status = rw["Indent Status"].ToString();
    string emergency = rw["Emergency Indent"].ToString();

    for (int i = 0; i <= table.Rows.Count - 1; i++)
    {
    dataGridView1.Rows[i].Cells[1].Value = no;
    dataGridView1.Rows[i].Cells[2].Value = date;
    dataGridView1.Rows[i].Cells[3].Value = frm;
    dataGridView1.Rows[i].Cells[4].Value = to;
    dataGridView1.Rows[i].Cells[5].Value = status;
    dataGridView1.Rows[i].Cells[6].Value = emergency;
    dataGridView1.Rows[i].Cells[7].Value = "Send";
    }
    // dataGridView1.Rows.Add();
    }


    I didn't get what you want to achieve by using the above code, in first loop you are getting row1 information and store that into string and in second loop for each record of grid you are assigned that string value.

    Here, based on table count you are trying to access gridview cell value, this is the problem may be in your case your grid don't have more than one record to access at that time, to resolve this issue my suggestion is better to put a breakpoint and debug and check each line of code.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #763515
    Hello,

    I think there is a problem in the code section mentioned below due to which "IndexOutException" occured .

    Your Code Section
    ---------------------

    for (int i = 0; i <= table.Rows.Count - 1; i++)
    {
    dataGridView1.Rows[i].Cells[1].Value = no;
    dataGridView1.Rows[i].Cells[2].Value = date;
    dataGridView1.Rows[i].Cells[3].Value = frm;
    dataGridView1.Rows[i].Cells[4].Value = to;
    dataGridView1.Rows[i].Cells[5].Value = status;
    dataGridView1.Rows[i].Cells[6].Value = emergency;
    dataGridView1.Rows[i].Cells[7].Value = "Send";
    }

    Solution
    ----------

    for (int i = 0; i <= table.Rows.Count - 1; i++)
    {
    dataGridView1.Rows[i].Cells[0].Value = no;
    dataGridView1.Rows[i].Cells[1].Value = date;
    dataGridView1.Rows[i].Cells[2].Value = frm;
    dataGridView1.Rows[i].Cells[3].Value = to;
    dataGridView1.Rows[i].Cells[4].Value = status;
    dataGridView1.Rows[i].Cells[5].Value = emergency;
    dataGridView1.Rows[i].Cells[6].Value = "Send";
    }

    You can try with this otherwise you just bind the datatable to the datagridview datasource i.e.

    dataGridView1.DataSource = table;
    dataGridView1.Bind();

    Thanks


Sign In to post your comments