Gridview doubling up rows


Blasting Cap

I've got a gridview (that I converted over from a datagrid, which had
been working properly), that is doubling up the number of rows returned.
When it was running as a datagrid, the same code sent back the
proper number of rows. The only thing different I am doing is to
display the number of rows returned in the footer of the gridview.

The Bindgrid is as follows:

Sub BindGrid()
Session("reportlevel") = Session("availabilityrptlevel")
Session("sportinggoods") = Session("sg_sku")
Session("le_widgets") = Session("le_sku")
Session("ISP_widgets") = Session("ISP_sku")

Dim ConnectionString As String = Session("ConnectString")
Dim myConnection As New SqlConnection(ConnectionString)
Dim cmdFAAvailability As New
SqlCommand("sp_ASP_fa_availability_new3", myConnection)
cmdFAAvailability.CommandType = CommandType.StoredProcedure

Dim prmfaavailability As New SqlParameter
prmfaavailability.ParameterName = "@availabilityrptlevel"
prmfaavailability.SqlDbType = SqlDbType.VarChar
prmfaavailability.Value = Session("availabilityrptlevel")

Dim prmsg_sku As New SqlParameter
prmsg_sku.ParameterName = "@sg_sku"
prmsg_sku.SqlDbType = SqlDbType.Int
prmsg_sku.Value = Session("sg_sku")

Dim prmle_sku As New SqlParameter
prmle_sku.ParameterName = "@le_sku"
prmle_sku.SqlDbType = SqlDbType.Int
prmle_sku.Value = Session("le_sku")

Dim prmISP_AFW As New SqlParameter
prmISP_AFW.ParameterName = "@isp"
prmISP_AFW.SqlDbType = SqlDbType.VarChar
prmISP_AFW.Value = Session("ISP_widgets")

Dim prmSG_sts As New SqlParameter
prmSG_sts.ParameterName = "@sg_sts"
prmSG_sts.SqlDbType = SqlDbType.VarChar
prmSG_sts.Value = Session("SG_sts")

Dim prmLE_sts As New SqlParameter
prmLE_sts.ParameterName = "@le_sts"
prmLE_sts.SqlDbType = SqlDbType.VarChar
prmLE_sts.Value = Session("LE_sts")

Dim daGetAvailability As New
Dim dsAvailability As New DataSet
daGetAvailability.Fill(dsAvailability, "zproduct")

If daGetAvailability.Fill(dsAvailability, "zproduct") >
0 Then
Label4.ForeColor = Color.Black
Label4.Text = ""
Me.GridView1.Visible = True
Me.GridView1.DataSource =
Select Case Session("btnSelect")
Case "SG" 'Sporting
Label4.Text = "No Sporting widgets on
Availability List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Case "LE" 'Entertainment
Label4.Text = "No Entertainment widgets on
Availability List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Case "ISP" 'ISP
Label4.Text = "No ISP widgets on
Availability List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Case "SG_STS" 'Sporting Subject-To-Stock
Label4.Text = "No Sporting widgets on
Subject-to-Stock List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Case "LE_STS" 'LE Subject-to-Stock
Label4.Text = "No Entertainment widgets on
Subject-to-Stock List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
End Select
Me.GridView1.Visible = False
Me.Label4.ForeColor = Color.Red
End If
Catch SQLexc As SqlException

Dim mail As New MailMessage()
Dim stemp As String = "(e-mail address removed)"

Dim ErrorMessage = "The error description is as
follows: " & SQLexc.ToString()
mail.From = New MailAddress(stemp)
mail.To.Add("(e-mail address removed)")
mail.Subject = "Error in application"
mail.Priority = MailPriority.High
mail.Body = ErrorMessage
Dim SmtpMail As New SmtpClient(Session("MyExchangeServer"))

Label3.Text = ""
Label4.Text = ""
Label4.ForeColor = Color.Red
Label4.Text = Label4.Text + SQLexc.Message.ToString()
Catch ex As Exception
Label3.Text = ""
Label4.ForeColor = Color.Red
Label4.Text = "There was a problem retrieving data for
this report. "
End Try
End Sub

I also included the code that does different things if I have a SQL
exception, etc.

The Rowdatabound code is:

Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As
If e.Row.RowType = DataControlRowType.Footer Then
Dim rows As Integer
rows = Me.GridView1.Rows.Count
e.Row.Cells(1).Text = "Total Records: (" & rows & ")"
End If
End Sub

The HTML is:

<asp:GridView ID="GridView1" runat="server"
Font-Size="X-Small" ShowFooter="True"
OnRowDataBound="CustomersGridView_RowDataBound" AutoGenerateColumns="False">
<FooterStyle BackColor="Gainsboro" />
<asp:BoundField DataField="Prod Cd"
HeaderText="Prod Cd" />
<asp:BoundField DataField="Description"
HeaderText="Description" />
<asp:BoundField DataField="Price"
DataFormatString="{0:C}" HeaderText="Price" HtmlEncode="False" >
<ItemStyle HorizontalAlign="Right" />
<asp:BoundField DataField="freestock"
HeaderText="Free Stock" >
<ItemStyle HorizontalAlign="Right" />
<asp:BoundField DataField="Note"
HeaderText="Note" />
<RowStyle BackColor="Moccasin" />
<HeaderStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="PapayaWhip" />

In debugging, I have a break point on:

If daGetAvailability.Fill(dsAvailability, "zproduct") > 0 Then

and this, and the query run in Query analyzer returns 202 rows. However
a stop point on:

e.Row.Cells(1).Text = "Total Records: (" & rows & ")"

shows the rows variable to be 404, 606, 808, or 1010, and the gridview
has the same number of records in it.

I think the onrowdatabound is the problem, but not sure how to fix it,
since this same code worked just fine in dealing with datagrids. When I
brought in the gridview, I ran them side by side and know that up until
I took out the datagrid (and added the on row databound) the datagrid &
gridview returned the same number of records. When I re-did the code for
the gridview (I had it commented out for the datagrid), I more-or-less
just changed the datagrid to gridview, correcting any errors as I went.

Any ideas why this is getting doubled up? I have a stop point on the
fill, and I believe it's only getting called 1 time, and when I run the
sql code with the parameters in Query Analyzer:

sp_ASP_fa_availability_new3 '10','1','0','0','0','0'

it returns only 202 rows. However, the app is doubling (and more) these
same records.

Any ideas as to what is going on here?






I think the onrowdatabound is the problem, but not sure how to fix it,

No. The problem is very simple:

daGetAvailability.Fill(dsAvailability, "zproduct")
If daGetAvailability.Fill(dsAvailability, "zproduct") > 0 Then

You added rows in to the dsAvailability data set two times.

Use Rows.Count instead

If (dsAvailability.Tables(0).Rows.Count > 0) Then



Blasting Cap

Thank you!! That fixed it. I had been so focused on what "new" was
there, I didn't even look at that.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question