error while selecting record from gridview

Last Reply 4 days ago By dharmendr

Posted 6 days ago

 

An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

error comes from this code below

 

Private Sub StudentGrid_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles StudentGrid.RowCommand
        If e.CommandName = "Payment" Then

            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = StudentGrid.Rows(index)
            Dim named As String = StudentGrid.Rows(index).Cells(1).Text
            Dim namesd As String = StudentGrid.Rows(index).Cells(2).Text
            Dim docment As String = StudentGrid.Rows(index).Cells(7).Text
            admno = DirectCast(StudentGrid.Rows(index).Cells(1).FindControl("admno"), Label).Text
            Dim names As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("Label1"), Label).Text
            Dim rclass As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("Label3"), Label).Text
            Dim rstream As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("Label4"), Label).Text
            Dim rfather As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("lblCategory2"), Label).Text
            Dim rmother As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("lblCategory3"), Label).Text
            Dim raddress As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("Label2"), Label).Text
            Dim rstatus As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("Label5"), Label).Text



            'Dim ddl As Object = TryCast(StudentGrid.Rows(index).FindControl("ddlClass"), DropDownList)
            ' Dim valueclass As String

            'Dim ddlstr As Object = TryCast(StudentGrid.Rows(index).FindControl("ddlStream"), DropDownList)
            'Dim valuestream As String
            ' Response.Redirect(String.Format("~/Forms/Paymentdetail.aspx?admno={0}&Names={1}&class={2}&stream={3}", admno, names, valueclass, valuestream))
            Session("account") = admno
            Session("Names") = names
            Session("Category") = "RECEPTION"
            SName.Text = names
            accounts.Text = admno
            DropDownList1.SelectedItem.Text = rclass
            Streamd.Text = rstream
            Fcontact.Text = rfather
            MotherContact.Text = rmother
            Address.Text = raddress
            Status.Text = rstatus
            ' Response.Redirect(String.Format("~/Forms/PatientBillPay.aspx"))
        Else

            If e.CommandName = "Staff" Then
                Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                Dim row As GridViewRow = StudentGrid.Rows(index)
                Dim named As String = StudentGrid.Rows(index).Cells(1).Text
                Dim namesd As String = StudentGrid.Rows(index).Cells(2).Text
                Dim docment As String = StudentGrid.Rows(index).Cells(7).Text
                admno = DirectCast(StudentGrid.Rows(index).Cells(1).FindControl("admno"), Label).Text
                Dim names As String = DirectCast(StudentGrid.Rows(index).Cells(2).FindControl("Label1"), Label).Text
                Dim age As String = DirectCast(StudentGrid.Rows(index).Cells(3).FindControl("lblCategory2"), Label).Text
                Dim Ptype As String = DirectCast(StudentGrid.Rows(index).Cells(4).FindControl("Label3"), Label).Text
                Dim Phonet As String = DirectCast(StudentGrid.Rows(index).Cells(5).FindControl("lblCategory3"), Label).Text




                ' Dim ddl As Object = TryCast(StudentGrid.Rows(index).FindControl("ddlClass"), DropDownList)
                Dim valueclass As String = age

                ' Dim ddlstr As Object = TryCast(StudentGrid.Rows(index).FindControl("ddlStream"), DropDownList)
                Dim valuestream As String = Ptype


                Response.Redirect(String.Format("~/Forms/staff.aspx?admno={0}&Names={1}&Age={2}&Ptype={3}&Phone={4}", admno, names, valueclass, valuestream, Phonet))
            Else

                If e.CommandName = "StreamPro" Then


                    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
                    Dim row As GridViewRow = StudentGrid.Rows(index)

                    Dim ddl As Object = TryCast(StudentGrid.Rows(index).FindControl("ddlCLASS"), DropDownList)
                    Dim ddl2 As Object = TryCast(StudentGrid.Rows(index).FindControl("ddlStream"), DropDownList)
                    ' Rclassr = DirectCast(StudentGrid.Rows(index).Cells(1).FindControl("Rclass"), Label).Text
                    Dim text As String = ddl.SelectedItem.Text
                    Dim value As String = ddl.SelectedItem.Value


                    Dim text2 As String = ddl2.SelectedItem.Text
                    Dim value2 As String = ddl2.SelectedItem.Value

                    If String.IsNullOrEmpty(value) Or value = "Please select" Then
                        ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", "alert('You Must Indicate Stream');", True)

                        Exit Sub
                    Else

                        Using Cont As New SqlConnection(conString)
                            Cont.Open()
                            Using cmd As New SqlCommand
                                cmd.Connection = Cont
                                cmd.CommandType = CommandType.StoredProcedure
                                cmd.CommandText = "Pushmissingstream"
                                cmd.Parameters.Add("@c", SqlDbType.NVarChar).Value = value
                                cmd.ExecuteReader()
                                Cont.Close()
                            End Using
                        End Using

                        Using Cont As New SqlConnection(conString)
                            Cont.Open()
                            Using cmd As New SqlCommand
                                cmd.Connection = Cont
                                cmd.CommandType = CommandType.StoredProcedure
                                cmd.CommandText = "Progressdataclass"
                                cmd.Parameters.Add("@c", SqlDbType.NVarChar).Value = value
                                cmd.ExecuteReader()
                                Cont.Close()
                            End Using
                        End Using



                    End If


                    Response.Redirect("~/Forms/StreamProgress.aspx")
                    ' Response.Redirect("~/Forms/StreamProgress.aspx?class=" & "P1")
                    ' ClientScript.RegisterStartupScript(Me.[GetType](), "Pop", "openModal2();", True)
                End If
            End If
        End If

    End Sub

 html view

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="SearchStudentdata.aspx.vb" Inherits="STMIS.SearchStudentdata" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Student Registration</title>
  
         <script type = "text/javascript">
              function askConfirm(msg) {
                  alert(msg)
              }
          </script>
    <script type="text/javascript">
function Showalert() {
alert('Call JavaScript function from codebehind');
}
</script>
   
    <script type="text/javascript" src="../Scripts/Jquery-3.5.1.js"></script>
     <script type="text/javascript"  src="../Scripts/Jquery.datatables.min.js"></script>
     <script type="text/javascript"  src="../Scripts/datatable.fixedColumns.min.js"></script>
     <link href="../Scripts/Jquery.datatables.min.css" rel="stylesheet" type="text/css" />
     <link href="../Scripts/fixedColumns.dataTables.min.css" rel="stylesheet" type="text/css" />

  
    <style type="text/css">
        .dataTables_scrollHead {
            width: 100% !important;
        }
 
        .dataTables_scrollHeadInner {
            width: 100% !important;
        }
 
        .dataTables_scrollBody {
            width: 100% !important;
        }
        .auto-style1 {
            height: 20px;
            width: 1065px;
        }
        .auto-style2 {
            color: #0066CC;
        }
        .auto-style3 {
            height: 30px;
        }
        .auto-style4 {
            height: 257px;
        }
        .auto-style5 {
            width: 99px;
        }
        .auto-style6 {
            width: 169px;
        }
        .auto-style7 {
            width: 13px;
        }
        .auto-style9 {
            width: 45px;
        }
        .auto-style10 {
            width: 121px;
        }
        .auto-style11 {
            width: 92px;
        }
        .auto-style13 {
            width: 57px;
        }
        .auto-style14 {
            width: 4px;
        }
    </style>
    <script type="text/javascript">
    $(function () {
        $('#StudentGrid tfoot tr').appendTo('#StudentGrid thead');
        $('#StudentGrid').removeAttr('width').DataTable({
            bFilter: true,
            bSort: true,
            scrollCollapse: true,
            paging: true,
            fixedColumns: false,
            orderCellsTop: true
        });
    });
</script>

       <script type="text/javascript">
        function Capital(txt) {
            txt.value = txt.value.replace(/^\s+/, '').toUpperCase();
        }
    </script>
</head>
    
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style1">
                <tr>
                    <td class="auto-style3"><strong>
                        <asp:Button ID="Button3" runat="server" CssClass="auto-style2" Text="Back To Selection Menu" Width="156px" />
                        </strong></td>
                </tr>
                <tr>
                    <td class="auto-style4">
                      <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <table class="dataTables_scrollHead">
            <tr>
                <td class="auto-style5">Student Name</td>
                <td class="auto-style6">
                    <asp:TextBox ID="SName" runat="server" Width="117px"></asp:TextBox>
                </td>
                <td class="auto-style7">Class</td>
                <td class="auto-style7">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>NONE</asp:ListItem>
                        <asp:ListItem>BABY</asp:ListItem>
                        <asp:ListItem>DAYCARE</asp:ListItem>
                        <asp:ListItem>NUR</asp:ListItem>
                        <asp:ListItem>TOP</asp:ListItem>
                        <asp:ListItem>P1</asp:ListItem>
                        <asp:ListItem>P2</asp:ListItem>
                        <asp:ListItem>P3</asp:ListItem>
                        <asp:ListItem>P4</asp:ListItem>
                        <asp:ListItem>P5</asp:ListItem>
                        <asp:ListItem>P6</asp:ListItem>
                        <asp:ListItem>P7</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>Status</td>
                <td class="auto-style14">
                    <asp:TextBox ID="Status" runat="server" Width="73px"></asp:TextBox>
                </td>
                <td class="auto-style14">Stream</td>
                <td class="auto-style9">
                    <asp:TextBox ID="Streamd" runat="server" Width="31px"></asp:TextBox>
                </td>
                <td class="auto-style5">Father Contact</td>
                <td class="auto-style10">
                    <asp:TextBox ID="Fcontact" runat="server" Width="95px"></asp:TextBox>
                </td>
                <td class="auto-style11">MotherConact</td>
                <td class="auto-style13">
                    <asp:TextBox ID="MotherContact" runat="server" Width="83px"></asp:TextBox>
                </td>
                <td>Gurdian</td>
                <td>
                    <asp:TextBox ID="Guard" runat="server" Width="97px"></asp:TextBox>
                </td>
                <td>Destination</td>
                <td>
                    <asp:TextBox ID="Address" runat="server" Width="77px"></asp:TextBox>
                </td>
                <td>Driver</td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="drived" DataTextField="DriverName" DataValueField="DriverName">
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="drived" runat="server" ConnectionString="<%$ ConnectionStrings:STMISWEBConnectionString %>" SelectCommand="SELECT [DriverName] FROM [DriverNames]"></asp:SqlDataSource>
                </td>
                <td>&nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Label ID="accounts" runat="server"></asp:Label>
                </td>
                <td>
                    <asp:Button ID="Button4" runat="server" Text="Update Details" />
                </td>
            </tr>
        </table>
        <table class="dataTables_scrollHead">
            <tr>
                <td colspan="2">
                    <asp:GridView ID="StudentGrid" runat="server" AutoGenerateColumns="False" CellSpacing="1" ClientIDMode="Static" EnableSortingAndPagingCallbacks="True" Height="190px" PageSize="30" ShowFooter="True" Width="1055px" EnableViewState="False">
                        <Columns>
                            <asp:TemplateField HeaderText="AcNo." SortExpression="account">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("account") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:Button ID="Button2" runat="server" OnClick="Button2_Click1" Text="AddNew" />
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Admno" runat="server" Text='<%# Bind("account") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Student Name" SortExpression="Name">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server" Height="16px"  Text='<%# Bind("Name") %>' Width="181px"></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="Namesf" runat="server" Width="263px"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Class">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Class") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:DropDownList ID="ddlClass" runat="server">
                                        <asp:ListItem>NONE</asp:ListItem>
                                        <asp:ListItem>BABY</asp:ListItem>
                                        <asp:ListItem>TOP</asp:ListItem>
                                        <asp:ListItem>DAYCARE</asp:ListItem>
                                        <asp:ListItem>P1</asp:ListItem>
                                        <asp:ListItem>P2</asp:ListItem>
                                        <asp:ListItem>P3</asp:ListItem>
                                        <asp:ListItem>P4</asp:ListItem>
                                        <asp:ListItem>P5</asp:ListItem>
                                        <asp:ListItem>P6</asp:ListItem>
                                        <asp:ListItem>P7</asp:ListItem>
                                    </asp:DropDownList>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Class") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Stream">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Stream") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="Streamf" runat="server" Width="53px"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Stream") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Father Contact">
                                <FooterTemplate>
                                    <asp:TextBox ID="FAge" runat="server" Width="132px"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCategory2" runat="server" Text='<%# Bind("Phone2") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Mother Contact">
                                <FooterTemplate>
                                    <asp:TextBox ID="FPhone" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCategory3" runat="server" Text='<%# Eval("Phone") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Guardian">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox7" runat="server" Height="18px" Text='<%# Bind("Guardian") %>' Width="88px"></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="Gurdiandata" runat="server" Text='<%# Bind("Guardian") %>' Width="83px"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("Guardian") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Destination" SortExpression="address">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" onkeyup="Capital(this)" Text='<%# Bind("address") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="FAddress" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("address") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Driver Name">
                                <FooterTemplate>
                                    <asp:DropDownList ID="ddlSexF" runat="server" DataTextField="DriverName" DataValueField="DriverName" DataSourceID="driverdat">
                                        <asp:ListItem>NONE</asp:ListItem>
                                        <asp:ListItem>MALE</asp:ListItem>
                                        <asp:ListItem>FEMALE</asp:ListItem>
                                    </asp:DropDownList>
                                    <asp:SqlDataSource ID="driverdat" runat="server" ConnectionString="<%$ ConnectionStrings:STMISWEBConnectionString %>" SelectCommand="SELECT [DriverName] FROM [DriverNames]"></asp:SqlDataSource>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCategory4" runat="server" Text='<%# Eval("DriverName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Dates" HeaderText="DateTime" DataFormatString="{0:dd/MM/yyyy}" />
                            <asp:ButtonField CommandName="Payment" HeaderText="Edit" Text="Edit" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style4">
                        &nbsp;</td>
                </tr>
                            </table>
        </div>
    </form>
</body>
</html>

<script type="text/javascript">
    //On UpdatePanel Refresh
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_endRequest(function (sender, e) {
            if (sender._postBackSettings.panelsToUpdate != null) {
                $('[id$=StudentGrid]').prepend($("<thead></thead>").append($('[id$=StudentGrid]').find("tr:first"))).DataTable({
                    "responsive": true,
                    "sPaginationType": "full_numbers"
                });
            }
        });
    };
</script>

 

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim cmd As New SqlCommand("SELECT NAME,SEX, ACCOUNT,STDTYPE,STATUS,Phone,Phone2,address,Dates,Class,Stream,Guardian,DriverName FROM Student")
            StudentGrid.DataSource = Me.ExecuteQuery(cmd, "SELECT")
            StudentGrid.DataBind()
            'Required for jQuery DataTables to work.
            StudentGrid.UseAccessibleHeader = True
            StudentGrid.FooterRow.TableSection = TableRowSection.TableFooter
            StudentGrid.HeaderRow.TableSection = TableRowSection.TableHeader


        End If
    End Sub

 

You are viewing reply posted by: makumbi 5 days ago.
Posted 5 days ago

the index is zero ( 0)