Link to home
Start Free TrialLog in
Avatar of dekempeneer
dekempeneerFlag for Belgium

asked on

error in asp.net

In my aspx file I HAD :
<asp:AccessDataSource    id="AccessDataSource1"    DataFile="data\DBS.mdb"    runat="server" selectcommand="select * from table"    >
On my webpage I could change the language of the contents by clicking an icon, when the icon was clicked a javascript command like var sURL = unescape(window.location.pathname);  window.location.replace( sURL ); is performed.    This all work ok.
Now I changed my accessdatasource and added : <SelectParameters>                <asp:QueryStringParameter Name="Country" QueryStringField="C" />  
</SelectParameters>               </asp:AccessDataSource>
and in my codebehind I have :
 If Not Page.IsPostBack Then
            AccessDataSource1.SelectCommand = "SELECT * FROM table" 'where Country= " & Request.QueryString("C") & " order by id"
            EmployeesGridView.SelectedIndex = 0
            CustomersGridView_SelectedIndexChanged(sender, e)
        End If

But when I do this now I get an error on :
Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles EmployeesGridView.SelectedIndexChanged
        Dim row As GridViewRow = EmployeesGridView.SelectedRow
...
saying :
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

What can this be and how can this be solved ??
Avatar of ModarHijazi
ModarHijazi
Flag of Syrian Arab Republic image

It is due to manual calling of:
CustomersGridView_SelectedIndexChanged(sender, e)
Mostly you use the value of "index" in it, which is null due to  manual calling,
You can change it's code to not use the index, or make other function that doesn't use the index, and call it.

if you want more info you must provide us with it's code.

Avatar of dekempeneer

ASKER

This is the SelectedIndexChanged code :
   Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles EmployeesGridView.SelectedIndexChanged
 
 
        Dim row As GridViewRow = EmployeesGridView.SelectedRow
        Dim btnSelect As LinkButton = row.FindControl("btnSelect")
        If btnSelect IsNot Nothing Then
            Message.Text = "You selected " & btnSelect.Text & "."
            Image1.ImageUrl = btnSelect.Attributes("img")
 
            Dim Lang As String
            Dim getCookie As HttpCookie
            getCookie = Request.Cookies("Language")
            Dim keyTemp As String
            If getCookie.HasKeys Then
                Dim valueCookie As New NameValueCollection(getCookie.Values)
                For Each keyTemp In valueCookie.AllKeys
                    Response.Write(keyTemp & " : ")
                    Response.Write(getCookie.Values(keyTemp) & "<br>")
                Next
            Else
                Response.Write(getCookie.Value)
                lblLang.Text = getCookie.Value
                Lang = getCookie.Value
                If Lang = "L_Dutch" Then
                    PDF.NavigateUrl = row.Cells(1).Text
                    PDF.Text = row.Cells(1).Text 'btnSelect.Text
                ElseIf Lang = "L_English" Then
                    PDF.NavigateUrl = row.Cells(2).Text
                    PDF.Text = row.Cells(2).Text 'btnSelect.Text
                ElseIf Lang = "L_German" Then
                    PDF.NavigateUrl = row.Cells(3).Text
                    PDF.Text = row.Cells(3).Text 'btnSelect.Text
 
                End If
            End If
    End Sub

Open in new window

and this is the code of the gridview
<asp:GridView id="EmployeesGridView" runat="server" AutoGenerateColumns="False" 
                                DataSourceid="AccessDataSource1" selectedindex="0"       
                                onselectedindexchanged="CustomersGridView_SelectedIndexChanged"       
                                onselectedindexchanging="CustomersGridView_SelectedIndexChanging" 
                                ShowHeader="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%"   >

Open in new window

Avatar of Nasir Razzaq
When you have a select parameter defined then you should not use the select statement. Instead, set the value of the select parameter and call the Select() method of the datasource.
Do you get the error first time only or all the time?
yes but I do it now like (see code)
<asp:AccessDataSource    id="AccessDataSource1"    DataFile="data\nemotoDBS.mdb"    runat="server"    >
<SelectParameters>                <asp:QueryStringParameter Name="Country" QueryStringField="C" />             </SelectParameters>
               </asp:AccessDataSource>
 
aspx.VB :
            AccessDataSource1.SelectCommand = "SELECT * FROM table " 'where Country= " & Request.QueryString("C") & " order by id"
            EmployeesGridView.SelectedIndex = 0
            CustomersGridView_SelectedIndexChanged(sender, e)

Open in new window

If you are doing this
            AccessDataSource1.SelectCommand = "SELECT * FROM table " 'where Country= " & Request.QueryString("C") & " order by id"

You do not need this
<SelectParameters>                <asp:QueryStringParameter Name="Country" QueryStringField="C" />             </SelectParameters>
If I remove that line it does not work at all.
Add a line here

            AccessDataSource1.SelectCommand = "SELECT * FROM table " 'where Country= " & Request.QueryString("C") & " order by id"

            AccessDataSource1.Select()

            EmployeesGridView.SelectedIndex = 0
            CustomersGridView_SelectedIndexChanged(sender, e)

ASKER CERTIFIED SOLUTION
Avatar of dekempeneer
dekempeneer
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could save a refresh by using the code i mentioned. Give it a try.
that does not work with me, got the error : BC30455: Argument not specified for parameter 'arguments' of 'Public Function Select(arguments As System.Web.UI.DataSourceSelectArguments) As System.Collections.IEnumerable'.

And I need to refresh it as a cookie changes and then some text changes based on that cookie, also the shown fields change as the language changes (based on that cookie)