Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Code runs forever, then times out

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
0
simflex
Asked:
simflex
  • 4
  • 4
  • 2
1 Solution
 
JimBrandleyCommented:
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
0
 
simflexAuthor 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.
0
 
JimBrandleyCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RejojohnyCommented:
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
0
 
RejojohnyCommented:
so comment the "bindgid()" statement and see if it works ..
Rejo
0
 
simflexAuthor 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
0
 
RejojohnyCommented:
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
0
 
simflexAuthor 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?
0
 
RejojohnyCommented:
Sub MyDataGrid_View(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
if e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem then
  Dim SQLStatement As String  
.. ur code
end if

Rejo
0
 
simflexAuthor 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!!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now