I am using gridview to load the data from the database. I have done correctly with databind. But this time i am trying to display data by filling the values of cells in rows...

My code is like this:-

Public Sub grid_init()

    dim index_flag as integer = 0
    curr_user = Welcome_id.Text()
    string_split = curr_user.Split("".ToCharArray)(0)
    img_green = ("~\Icons\" & "circle_green.ico")
    img_orange = ("~\Icons\" & "circle_orange.ico")
    img_red = ("~\Icons\" & "circle_red.ico")

    GridView1.AllowPaging = True
    GridView1.PageSize = 10
    cmd = New SqlCommand("SELECT  a.curr_datetime, a.site_id, b.site_name, a.dc_volt, a.curr_temp, a.eb_val, a.dc_low, a.hrt_temp, a.curr_dfs, a.curr_dft, a.curr_llop, a.curr_dgonl, a.fa_alarm, a.door_open, a.curr_spare FROM final_test a INNER JOIN site_details b ON a.site_id = b.site_id;", conn)

    If conn.State = ConnectionState.Closed Then
    End If

    da.SelectCommand = cmd
    da.Fill(ds, "final_test")
    dt = ds.Tables("final_test")

    dr = cmd.ExecuteReader

    If dr.Read() Then

        GridView1.Rows(index_flag).Cells(0).Text = ds.Tables(0).Rows(0).Item("curr_datetime").ToString
        GridView1.Rows(index_flag).Cells(1).Text = ds.Tables(0).Rows(0).Item("site_id").ToString
        GridView1.Rows(index_flag).Cells(2).Text = ds.Tables(0).Rows(0).Item("site_name").ToString
        GridView1.Rows(index_flag).Cells(3).Text = ds.Tables(0).Rows(0).Item("dc_volt").ToString
        GridView1.Rows(index_flag).Cells(4).Text = ds.Tables(0).Rows(0).Item("curr_temp").ToString

        lcl_ebval = ds.Tables(0).Rows(0).Item("eb_val").ToString
        If lcl_ebval = 0 Then
            eb_img = img_green
        ElseIf lcl_ebval = 1 Then
            eb_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(5).Text = ds.Tables(0).Rows(0).Item("eb_img").ToString

        lcl_dclow = ds.Tables(0).Rows(0).Item("dc_low").ToString
        If lcl_dclow = 0 Then
            dclow_img = img_green
        ElseIf lcl_dclow = 1 Then
            dclow_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(6).Text = ds.Tables(0).Rows(0).Item("dclow_img").ToString

        lcl_hrt = ds.Tables(0).Rows(0).Item("hrt_temp").ToString
        If lcl_hrt = 0 Then
            hrt_img = img_green
        ElseIf lcl_hrt = 1 Then
            hrt_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(7).Text = ds.Tables(0).Rows(0).Item("hrt_img").ToString

        lcl_dfs = ds.Tables(0).Rows(0).Item("curr_dfs").ToString
        If lcl_dfs = 0 Then
            dfs_img = img_green
        ElseIf lcl_dfs = 1 Then
            dfs_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(8).Text = ds.Tables(0).Rows(0).Item("dfs_img").ToString

        lcl_dft = ds.Tables(0).Rows(0).Item("curr_dft").ToString
        If lcl_dft = 0 Then
            dft_img = img_green
            dft_img = img_orange
        End If
        GridView1.Rows(index_flag).Cells(9).Text = ds.Tables(0).Rows(0).Item("dft_img").ToString

        lcl_llop = ds.Tables(0).Rows(0).Item("curr_llop").ToString
        If lcl_llop = 0 Then
            llop_img = img_green
            llop_img = img_orange
        End If
        GridView1.Rows(index_flag).Cells(10).Text = ds.Tables(0).Rows(0).Item("llop_img").ToString

        lcl_dgol = ds.Tables(0).Rows(0).Item("curr_dgonl").ToString
        If lcl_dgol = 0 Then
            dgol_img = img_green
            dgol_img = img_orange
        End If
        GridView1.Rows(index_flag).Cells(11).Text = ds.Tables(0).Rows(0).Item("dgol_img").ToString

        lcl_faalarm = ds.Tables(0).Rows(0).Item("fa_alarm").ToString
        If lcl_faalarm = 0 Then
            falarm_img = img_green
            falarm_img = img_red
        End If
        GridView1.Rows(index_flag).Cells(12).Text = ds.Tables(0).Rows(0).Item("falarm_img").ToString

        lcl_door = ds.Tables(0).Rows(0).Item("door_open").ToString
        If lcl_door = 0 Then
            dopen_img = img_green
            dopen_img = img_orange
        End If
        GridView1.Rows(index_flag).Cells(13).Text = ds.Tables(0).Rows(0).Item("dopen_img").ToString

        lcl_spare = ds.Tables(0).Rows(0).Item("curr_spare").ToString

        'GridView1.DataSource = dt

        ' dt.Clear()
        index_flag = index_flag + 1
    End If

    If conn.State = ConnectionState.Open Then
    End If

It displays the error as
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

End Sub
3 Years
Discussion Span
Last Post by salathia
Featured Replies

On which line of code is this error thrown.

You seem to have a lot of hard coded indexes, it could be numerous things.

I would start with the obvious, do you have 14 cells in your datagridview?

Does your dataset have rows?


There are total 14 cells in an row. And in some cells i have to display the images on the bases of the certain cells value and for this reason i m fetching the values from the database row by row and check the value of every cell so that on the bases of that value i can display either red, green or orange color image in particular cell..... please provide me appropriate solution for this...... I have already checked this databind method but in that method the images are same for every row... Thats why i quit it....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.