Hello Experts!
I use this same code for two other Datagrids and I don't have this problem so I figure it's something with the select statement I'm using. Every page load is taking around 15 seconds. When I first load the page, edit, cancel, update, change to page 2. All of it is taking a lot longer than it used to. I'm not sure what I changed that did this. Maybe the focus(), but I'm not sure. Hopefully someone can spot what's causing the slow down.
Here is the code:
<%@ Page Language="VB" Debug="true" Explicit="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Sql
Client" %>
<%@ import Namespace="System.DateTime
" %>
<script runat="server">
' TODO: update the ConnectionString and Command values for your application
Dim ConnectionString As String = "server=sqlserver;uid=uid;
PWD=pwd;da
tabase=gwp
f;"
Dim SelectCommand1 As String = "SELECT Distinct [First Name], [Last Name], [Document Date], Reference#, Control#, Acct#, Description, SumAmt, CommentBox, ControlNo, DateDiff(""d"",GetDate(),"
"Document Date"") as daysold FROM odsc_schedule4 INNER Join Comments1130 on Control# = ControlNo Inner Join odsc_names on [Name ID] = ControlNo Where (ODSC_Schedule4.AutoID IN (Select MAX(ODSC_SChedule4.AutoID)
From Odsc_Schedule4 Where ACCT# = '1130' Group By Control#)) ORDER BY daysold ASC"
Dim SelectCommand2 As String = "SELECT distinct * FROM Comments1130"
Dim isEditing As Boolean = False
Sub Page_Load(Sender As Object, E As EventArgs)
If Not Page.IsPostBack Then
' Databind the data grid on the first request only
' (on postback, bind only in editing, paging and sorting commands)
Yeah()
End If
End Sub
' --------------------------
----------
----------
----------
-------
'
' DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
'
Sub DataGrid_ItemCommand(Sende
r As Object, E As DataGridCommandEventArgs)
' this event fires prior to all of the other commands
' use it to provide a more graceful transition out of edit mode
'CheckIsEditing(e.CommandN
ame)
End Sub
Sub CheckIsEditing(commandName
As String)
If DataGrid1.EditItemIndex <> -1 Then
' we are currently editing a row
If commandName = " Cancel " Then
' user's edit changes (If any) will not be committed
Message.Text = "Your changes have not been saved yet. Please press update to save your changes, or cancel to discard your changes, before selecting another item."
isEditing = True
Else If commandName = " Update" Then
Message.Text = "Your changes have been made."
isEditing = True
End If
End If
End Sub
Sub DataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
' turn on editing for the selected row
Dim CommentBox As Textbox
DataGrid1.EditItemIndex = e.Item.ItemIndex
Dim scriptJs As String
BindGrid()
CommentBox = CType(DataGrid1.Items(Data
Grid1.Edit
ItemIndex)
.Cells(7).
Controls(0
), TextBox)
scriptJs = "<script language=javascript>" & vbCrLf
scriptJs &= "document.getElementById('
" & CommentBox.UniqueID & "').focus();" & vbCrLf
scriptJs &= "document.getElementById('
" & CommentBox.UniqueID & "').select();" & vbCrLf
scriptJs &= "<" & "/script>"
If (Not Me.IsStartupScriptRegister
ed("Startu
p")) Then
Me.RegisterStartupScript("
Startup", scriptJs)
End If
End Sub
Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
' update the database with the new values
' get the edit text boxes
Dim Control As String = CType(e.Item.Cells(0).Cont
rols(0), textbox).Text
Dim CommentBox As String = CType(e.Item.Cells(7).Cont
rols(0), textbox).Text
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim UpdateCommand As SqlCommand = new SqlCommand()
UpdateCommand.Connection = myConnection
If AddingNew = True Then
UpdateCommand.CommandText = "INSERT INTO comments1130(ControlNo, CommentBox) VALUES (@Contno, @CommentBox)"
Else
UpdateCommand.CommandText = "UPDATE comments1130 SET CommentBox = @CommentBox WHERE controlNo = @ContNo"
End If
UpdateCommand.Parameters.A
dd("@ContN
o", SqlDbType.VarChar, 8000).Value = Control
UpdateCommand.Parameters.A
dd("@Comme
ntBox", SqlDbType.VarChar, 99).Value = CommentBox
' execute the command
Try
myConnection.Open()
UpdateCommand.ExecuteNonQu
ery()
Catch ex as Exception
Message.Text = ex.ToString()
Finally
myConnection.Close()
End Try
' Resort the grid for new records
If AddingNew = True Then
DataGrid1.CurrentPageIndex
= 0
AddingNew = false
End If
' rebind the grid
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
Sub DataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)
' cancel editing
DataGrid1.EditItemIndex = -1
BindGrid()
AddingNew = False
End Sub
Sub DataGrid_Page(Sender As Object, E As DataGridPageChangedEventAr
gs)
' display a new page of data
If Not isEditing Then
DataGrid1.EditItemIndex = -1
DataGrid1.CurrentPageIndex
= e.NewPageIndex
BindGrid()
End If
End Sub
Property AddingNew() As Boolean
Get
Dim o As Object = ViewState("AddingNew")
If o Is Nothing Then
Return False
End If
Return CBool(o)
End Get
Set(ByVal Value As Boolean)
ViewState("AddingNew") = Value
End Set
End Property
Sub Yeah()
Const strSQL As String = "usp_SumByControlNo1130"
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim mycommand = New SqlCommand(strSQL, myConnection)
myConnection.Open()
DataGrid1.DataSource = mycommand.ExecuteReader(Co
mmandBehav
ior.CloseC
onnection)
BindGrid()
End Sub
Sub BindGrid()
Dim objDataSet As DataSet = New DataSet
Dim myConnection As New SqlConnection(ConnectionSt
ring)
Dim Adapter As SqlDataAdapter = New SqlDataAdapter
Adapter.SelectCommand = _
New SqlCommand(SelectCommand1,
myConnection)
myConnection.Open()
Adapter.Fill(objDataSet,"O
DSC_Schedu
le4")
Adapter.SelectCommand = _
New SqlCommand(SelectCommand2,
myConnection)
Adapter.Fill(objDataSet,"c
omments113
0")
DataGrid1.DataSource = objDataSet
DataGrid1.Databind()
End Sub
</script>
<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<h4>Bad Checks - 1130 <%=N
ow %><h4 align="right"><input type="button" value="Print Page" name="PrintBtn" onClick="window.print()">
</h4>
<hr size="2" />
<form id="form1" runat="server">
<asp:datagrid id="DataGrid1" runat="server" enableviewstate="true" ShowFooter="true" Font-Size="10pt" AutoGenerateColumns="False
" width="90%" CellSpacing="2" GridLines="None" HorizontalAlign="Center" CellPadding="3" BackColor="White" ForeColor="Black" OnPageIndexChanged="DataGr
id_Page" PageSize="12" AllowPaging="true" OnCancelCommand="DataGrid_
Cancel" OnUpdateCommand="DataGrid_
Update" OnEditCommand="DataGrid_Ed
it" OnItemCommand="DataGrid_It
emCommand"
DataKeyField="Control#">
<HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></Head
erStyle>
<PagerStyle horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages" font-size="smaller"></Page
rStyle>
<ItemStyle backcolor="#DEDFDE"></Item
Style>
<FooterStyle backcolor="#C6C3C6"></Foot
erStyle>
<Columns>
<asp:BoundColumn DataField="Control#" ReadOnly="False" HeaderText="Cust. Number" />
<asp:BoundColumn DataField="Reference#" ReadOnly="True" HeaderText="Reference#" />
<asp:BoundColumn DataField="Document Date" DataFormatString="{0:d}" ReadOnly="True" HeaderText="Document Date" />
<asp:BoundColumn DataField="Last Name" ReadOnly="True" HeaderText="Last Name" />
<asp:boundColumn DataField="First Name" ReadOnly="True" HeaderText="First Name" />
<asp:BoundColumn DataField="SumAmt" DataFormatString="{0:c}" ReadOnly="True" HeaderText="Amount" />
<asp:BoundColumn DataField="daysold" ReadOnly="True" HeaderText="Days Old" />
<asp:BoundColumn DataField="CommentBox" HeaderText="Comments" ReadOnly="False" />
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit" ItemStyle-Font-Size="small
er" ItemStyle-Width="7%"></asp
:EditComma
ndColumn>
</Columns>
</asp:datagrid>
<br />
<br />
<br />
<asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></
asp:Label>
</form>
</body>
</html>
Sorry if the code is messy.