We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

Code runs forever, then times out

simflex
simflex asked
on
303 Views
Last Modified: 2007-10-03
Dear all,

I am trying to add viewing page to my datagrid control so users can view details of a contact.

When I run this code, it just runs forever and eventually times out.

Can you please tell me what I am doing wrong with the "Mydatagrid_View event?

Here is my code:

    Sub MyDataGrid_View(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
       Dim ID As Integer = CInt(MyDataGrid.DataKeys(CInt(e.Item.ItemIndex)))

        Dim SQLStatement As String = "SELECT * FROM Contact WHERE ID =" & ID
        'Response.Write(SQLStatement)
        'Response.End()
        'instantiate sql connection and command object
        Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("Contacts.mdb") & ";")
        Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter(SQLStatement, myConnection)
        BindGrid()
        'Handle cell 0 - Contact name click event
        e.Item.Cells(0).Attributes.Add("Onclick", "javascript:window.open('viewing.aspx?id=" & ID & "'," & "'','height=690,width=700')")
      End Sub

form source code is:

    <form id="Form1" method="post" runat="server">
      <asp:DataGrid id="MyDataGrid" runat="server" BorderStyle="Ridge" GridLines="None" BorderWidth="2px"
          BorderColor="White" BackColor="White" CellPadding="3" CellSpacing="1" AllowPaging="True" AllowSorting="True"
        PageSize="25" PagerStyle-Mode="NextPrev" PagerStyle-NextPageText="Next" PagerStyle-PrevPageText="Previous"
        PagerStyle-HorizontalAlign="Center" PagerStyle-Position="TopAndBottom" DataKeyField="ID" OnPageIndexChanged="MyDataGrid_PageIndexChanged"
        OnSortCommand="Sort_Grid" OnDeleteCommand="MyDataGrid_Delete" OnUpdateCommand="MyDataGrid_Update"
        OnItemDataBound="MyDataGrid_View" OnCancelCommand="MyDataGrid_Cancel" OnEditCommand="MyDataGrid_Edit"
        AutoGenerateColumns="False" HorizontalAlign="Left">
       <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
       <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
       <PagerStyle NextPageText="Next" PrevPageText="Previous" HorizontalAlign="Right" ForeColor="Black"
        Position="TopAndBottom" BackColor="#C6C3C6"></PagerStyle>
       <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
       <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
      
      <Columns>
      <asp:ButtonColumn Text="<img border=0 src=images/user.gif>" CommandName="View"></asp:ButtonColumn>
      <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="<img border=0 src=images/ok.gif>" CancelText="<img border=0 src=images/cancel.gif>"
       EditText="<img border=0 src=images/icon_edit.gif>"></asp:EditCommandColumn>
      <asp:ButtonColumn Text="<img border=0 src=images/icon_delete.gif>" CommandName="Delete"></asp:ButtonColumn>
      <asp:BoundColumn DataField="ID" SortExpression="ID" ReadOnly="True" HeaderText="Contact ID"></asp:BoundColumn>
      <asp:BoundColumn DataField="lastName" SortExpression="lastName" HeaderText="Last Name"></asp:BoundColumn>
      <asp:BoundColumn DataField="firstName" SortExpression="firstName" HeaderText="First Name"></asp:BoundColumn>
      <asp:BoundColumn DataField="street_Address" SortExpression="street_Address" HeaderText="Street Address"></asp:BoundColumn>
      <asp:BoundColumn DataField="City" SortExpression="City" HeaderText="City"></asp:BoundColumn>
      <asp:BoundColumn DataField="Zip" SortExpression="Zip" HeaderText="Zip"></asp:BoundColumn>
      <asp:BoundColumn DataField="Phone" SortExpression="Phone" HeaderText="Phone"></asp:BoundColumn>
      <asp:BoundColumn DataField="Email" SortExpression="Email" HeaderText="Email"></asp:BoundColumn>
      <asp:BoundColumn DataField="dateofBirth" DataFormatString="{0:d}" SortExpression="dateofBirth" HeaderText="Birth Date"></asp:BoundColumn>
      </Columns>
       </asp:DataGrid>
      </form>

Thanks in advance
Comment
Watch Question

Top Expert 2007

Commented:
I can think of three possibilities:
1. The click event timer is not firing at all, though this is unlikely if you see a timeout.
2. Something inside the the event handler is not returning in a timely manner.
3. Something is causing the event handler to fire repeatedly.

Knowing which of the three is occurring is a start. To determine that, set one breakpoint at the start of the handler and another at the end. From that, you can deduce which of the three cases needs investigation.

Jim

Author

Commented:
Sorry but I think the issue has more to do with the sql string.

Dim ID As Integer = CInt(MyDataGrid.DataKeys(CInt(e.Item.ItemIndex)))

        Dim SQLStatement As String = "SELECT * FROM Contact WHERE ID =" & ID
If I comment out these 2 lines, then the code runs real fast. Only problem is that it won't return a value for ID.

So, really how to make this line:

'Handle cell 0 - Contact name click event
        e.Item.Cells(0).Attributes.Add("Onclick", "javascript:window.open('viewing.aspx?id=" & ID & "'," & "'','height=690,width=700')")

communicate with this

OnItemDataBound="MyDataGrid_View"

on the webform is the problem.
Top Expert 2007

Commented:
If you just run this select
"SELECT * FROM Contact WHERE ID = ?
(substitute some real value for the question mark)
from your favorite query tool, what does it return?

Jim
what is this "BindGrid()" writtien within the event for?


Itemdatabound is fired for each row of the grid after a grid is bound .. within this event if you again bind the grid, the item databound would again get fired .. and so in your case, this woud mean a "endless loop" and so the timeout ..

Rejo
so comment the "bindgid()" statement and see if it works ..
Rejo

Author

Commented:
thank you gurus. Please forgive me as I am still very, very new to this.

I removed the bindrid() statement and now this is the error I get:

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

and this is the latest code:

    Sub MyDataGrid_View(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        Dim ID As Integer = CInt(MyDataGrid.DataKeys(CInt(e.Item.ItemIndex)))
        Dim SQLStatement As String = "SELECT * FROM Contact WHERE ID =" & ID
        Response.Write(SQLStatement)
        Response.End()
        'instantiate sql connection and command object
        Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("Contacts.mdb") & ";")
        Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter(SQLStatement, myConnection)
        e.Item.Cells(0).Attributes.Add("Onclick", "javascript:window.open('viewing.aspx?id=" & ID & "'," & "'','height=690,width=700')")
    End Sub

To answer JimBrandley's question, If I enter a value of 5208, and put a debugger, I get this:

SELECT * FROM Contact WHERE ID = 5208

but that value is not transferred to the ID on this line:

        e.Item.Cells(0).Attributes.Add("Onclick", "javascript:window.open('viewing.aspx?id=" & ID & "'," & "'','height=690,width=700')")

Any more ideas
as mentioned above, the databound code runs for every row in the datagrid .. so it runs for the header, footer etc too .. your header will not have that link and that is the reason why it is giving the error for the header row .. so check for the type of row .. like this ..


Sub MyDataGrid_View(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
if  e.Row.RowType = DataControlRowType.DataRow then
Dim ID As Integer = CInt(MyDataGrid.DataKeys(CInt(e.Item.ItemIndex)))
        Dim SQLStatement As String = "SELECT * FROM Contact WHERE ID =" & ID
        Response.Write(SQLStatement)
        Response.End()
        'instantiate sql connection and command object
        Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("Contacts.mdb") & ";")
        Dim myCommand As OleDbDataAdapter = New OleDbDataAdapter(SQLStatement, myConnection)
        e.Item.Cells(0).Attributes.Add("Onclick", "javascript:window.open('viewing.aspx?id=" & ID & "'," & "'','height=690,width=700')")
end if
end sub

Rejo

Author

Commented:
Thank you so much again for your prompt response Rejo.

I get the following error:

Row is not a member of System.Web.UI.Webcontrols.DataGridItemEventArgs

then another that says:

Name DataControlRowType is not declared

Could it be because of I am use datagrid and not gridView control?

If yes, can you please tell me the equivalent for datagrid?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
sorry for my late response Rejo.

It is working perfectly.

You are a guru, that's for sure.

Thank you so so so much!!!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.