By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,684 Members | 1,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,684 IT Pros & Developers. It's quick & easy.

Gridview doubling up rows

P: n/a
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")
cmdFAAvailability.Parameters.Add(prmfaavailability )

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

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

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

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

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

Try
Dim daGetAvailability As New
SqlDataAdapter(cmdFAAvailability)
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 =
dsAvailability.Tables("zproduct")
Me.GridView1.DataBind()
Else
Select Case Session("btnSelect")
Case "SG" 'Sporting
Label4.Text = "No Sporting widgets on
Availability List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Day(Now())
Case "LE" 'Entertainment
Label4.Text = "No Entertainment widgets on
Availability List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Day(Now())
Case "ISP" 'ISP
Label4.Text = "No ISP widgets on
Availability List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Day(Now())
Case "SG_STS" 'Sporting Subject-To-Stock
Label4.Text = "No Sporting widgets on
Subject-to-Stock List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Day(Now())
Case "LE_STS" 'LE Subject-to-Stock
Label4.Text = "No Entertainment widgets on
Subject-to-Stock List as of " & Year(Now()) & "-" & Month(Now()) & "-" &
Day(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 = "me@mydomain.com"

Dim ErrorMessage = "The error description is as
follows: " & SQLexc.ToString()
mail.From = New MailAddress(stemp)
mail.To.Add("ad***********@mydomain.com")
mail.Subject = "Error in widget.com application"
mail.Priority = MailPriority.High
String.Format("HTML")
mail.Body = ErrorMessage
Dim SmtpMail As New SmtpClient(Session("MyExchangeServer"))
SmtpMail.Send(mail)

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. "
Finally
Me.SqlConnection1.Close()
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
GridViewRowEventArgs)
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" />
<Columns>
<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>
<asp:BoundField DataField="freestock"
HeaderText="Free Stock" >
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="Note"
HeaderText="Note" />
</Columns>
<RowStyle BackColor="Moccasin" />
<HeaderStyle BackColor="Gainsboro" />
<AlternatingRowStyle BackColor="PapayaWhip" />
</asp:GridView>
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?

Thanks,

BC
Jun 5 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 5, 10:27 pm, Blasting Cap <goo...@christian.netwrote:
>
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
Jun 5 '07 #2

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

Alexey Smirnov wrote:
On Jun 5, 10:27 pm, Blasting Cap <goo...@christian.netwrote:
>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

Jun 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.