Avatar of Bonnie_K
Bonnie_K

asked on 

Total rowcount for gridview

Hi,

I have been trying to figure out how to get the total rowcount for a gridview (I am using paging).  I thought that the methods used here would work:

http://www.webswapp.com/codesamples/viewsource.aspx?file=~/codesamples/aspnet20/dropdownlist_gridview/default.aspx

but I get an error: System.InvalidCastException: Unable to cast object of type 'tbl_Email_Blast2DataTable' to type 'System.Collections.Generic.List`1[DataTableAdapters.tbl_Email_Blast2TableAdapter]'.

Below is my code.  Thanks for any help.

Bonnie


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
 
public partial class Email_List : System.Web.UI.Page
{
    protected void test(object sender, ObjectDataSourceStatusEventArgs e)
    {
        rowcountTextBox.Text = ((List<DataTableAdapters.tbl_Email_Blast2TableAdapter>)e.ReturnValue).Count.ToString();
    }
 
 }
********************************************************************************************************************
 
<%@ Page Language="C#" MasterPageFile="~/Email_List/MasterPage2.master" Debug="true" AutoEventWireup="true" CodeFile="CSV_List.aspx.cs" Inherits="Email_List" Title="Email Address List" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div align="center"> 
 <h3 style="font-family:Tahoma;">Datawarehouse Email Marketing List<br />
     <asp:TextBox ID="rowcountTextBox" runat="server"></asp:TextBox></h3>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid"
        BorderWidth="1px" CellPadding="3" DataKeyNames="db_key" DataSourceID="ObjectDataSource2"
        ForeColor="Black" GridLines="Vertical" PageSize="25">
        <FooterStyle BackColor="#CCCCCC" Font-Names="Tahoma" Font-Size="Small" />
        <Columns>
            <asp:TemplateField HeaderText="Edit" ShowHeader="False">
                <EditItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True" CommandName="Update"
                     ImageUrl="~/images/disk.png" ToolTip="Save Changes"  />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" CommandName="Edit"
                        ImageUrl="~/images/pencil.png" ToolTip="Edit Email Addresses" />
                </ItemTemplate>
            </asp:TemplateField>
            
            <asp:TemplateField HeaderText="Delete" ShowHeader="False">
                <EditItemTemplate>
                    <asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False"
                            CommandName="Cancel" ImageUrl="~/images/diskx.png" ToolTip="Discard Changes"  Text="Cancel" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton4" runat="server" CausesValidation="False" CommandName="Delete"
                        ImageUrl="~/images/cancel.png"  ToolTip="Delete Email Address" />
                </ItemTemplate>
            </asp:TemplateField>
          
            <asp:TemplateField HeaderText="Email Address" SortExpression="email_address">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("email_address") %>' Width="255px"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("email_address") %>' Width="255px"></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Left" />
            </asp:TemplateField>
            <asp:BoundField DataField="company_name" HeaderText="Company Name" SortExpression="company_name">
                <ItemStyle HorizontalAlign="Left" />
            </asp:BoundField>
        </Columns>
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" Font-Names="Tahoma" Font-Size="Small" />
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <RowStyle Font-Names="Tahoma" Font-Size="Small" />
        <EditRowStyle Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" BackColor="#FFFF80" />
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCSVAddresseSorted"
        TypeName="DataTableAdapters.tbl_Email_Blast2TableAdapter" InsertMethod="Insert" UpdateMethod="Update" OnSelected="test">
        <DeleteParameters>
            <asp:Parameter Name="Original_db_key" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="email_address" Type="String" />
            <asp:Parameter Name="company_name" Type="String" />
            <asp:Parameter Name="Original_db_key" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="email_address" Type="String" />
            <asp:Parameter Name="company_name" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>
</div>
 
</asp:Content>

Open in new window

ASP.NETC#

Avatar of undefined
Last Comment
abel
Avatar of Bonnie_K
Bonnie_K

ASKER

By the way,  when I do this:

    protected void test(object sender, ObjectDataSourceStatusEventArgs e)
    {
        rowcountTextBox.Text = e.AffectedRows.ToString();
    }


I just get the result -1 instead of 523, which is what I should get.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bonnie_K
Bonnie_K

ASKER

abel, thanks for the information and sending me in a new direction.  I ended up using a stored procedure with the use of a couple of other experts-exchange answers to get the value:

@row_count int = 0 OUTPUT
AS
BEGIN
SET NOCOUNT ON;

SELECT db_key FROM dbo.tbl_Email

SET @row_count = @@ROWCOUNT
END

****************************************
 protected void Page_Load(object sender, EventArgs e)
    {
        string strConnection =
           "Data Source=;Integrated Security=false;Initial Catalog=;User ID=;Password=";
       
        SqlConnection myConn = new SqlConnection(strConnection);

        SqlCommand myCommand = new SqlCommand("sp_CountRows", myConn);
        myCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter myParm = myCommand.Parameters.Add("@row_count", SqlDbType.Int);
        myParm.Direction = ParameterDirection.Output;

        myConn.Open();

        myCommand.ExecuteNonQuery();

        // Use this syntax to get the value from the OUT parameter
        int row_count = (int)myParm.Value;

        myConn.Close();

        rowcountTextBox.Text = row_count.ToString();

    }
Avatar of abel
abel
Flag of Netherlands image

Great you got it worked out & tx for the points. Lookout with that strConnection: don't place it inside your code but put it somewhere in your web.config.
Avatar of Bonnie_K
Bonnie_K

ASKER

If you don't mind a follow up, How do I properly refer to the connection string in the above example then?  I have a connection string called dataConnectionString in the web.config.

Thanks,
Bonnie
Avatar of abel
abel
Flag of Netherlands image

That would be

ConfigurationManager.AppSettings.Get("dataConnectionString")
Avatar of Bonnie_K
Bonnie_K

ASKER

Thanks abel
Avatar of abel
abel
Flag of Netherlands image

You're welcome, Bonnie ;)
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo