locked
retrieveing record / count from SQLDataSource Control RRS feed

  • Question

  • User-2087919080 posted

    How can I get the record count from the SQLDataSource Control.  I have a SQLDataSource control on a page and not presentaion controls.  I know I can get the count from a presentaion control but how do I get a count from the SQLDataSource Control itself?

     

    Tuesday, December 8, 2009 12:38 PM

Answers

  • User77042963 posted

    You can get it through e.AffectedRow in the your datasource's Selected event. 

     protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label1.Text = e.AffectedRows.ToString();
        }


     Or

     protected void Page_Load(object sender, EventArgs e)
        {       
                System.Data.DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                Label1.Text = dv.Count.ToString();
        }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2009 12:46 PM
  • User1622592694 posted

    That is a winner!  This worked in Page_Load:

            System.Data.DataView dv = (DataView)ClaimantSqlDataSource.Select(DataSourceSelectArguments.Empty);
            Response.Write(dv.Table.Rows[0]["FirstName"].ToString());

    Now if this thread had some tags so others would not have to spend as much time trying everything but your GREAT solution!

    Todd


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2009 7:08 PM

All replies

  • User77042963 posted

    You can get it through e.AffectedRow in the your datasource's Selected event. 

     protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label1.Text = e.AffectedRows.ToString();
        }


     Or

     protected void Page_Load(object sender, EventArgs e)
        {       
                System.Data.DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                Label1.Text = dv.Count.ToString();
        }


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2009 12:46 PM
  • User1622592694 posted

    That is a winner!  This worked in Page_Load:

            System.Data.DataView dv = (DataView)ClaimantSqlDataSource.Select(DataSourceSelectArguments.Empty);
            Response.Write(dv.Table.Rows[0]["FirstName"].ToString());

    Now if this thread had some tags so others would not have to spend as much time trying everything but your GREAT solution!

    Todd


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2009 7:08 PM
  • User-2087919080 posted

    did you convert the c# code to vb?  if not, google code converter and convert it.

    Monday, May 16, 2011 9:59 AM
  • User1707590764 posted

    Also, i am not able to retrieve the info if i do not have a grid view, list view, repeater or any other object using the sqldatasource.

    here is the code on my page

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="SRreport.aspx.vb" Inherits="SR_2011_SRreport" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:Ranjit WebConnectionString %>" 
                SelectCommand="SELECT * FROM [SR2011]"></asp:SqlDataSource>
            <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            </asp:Repeater>
        
            <br />
            <br />
        
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:Ranjit WebConnectionString %>" 
                
                SelectCommand="SELECT DLTYPE, DLNO, DEPT FROM SR2011 WHERE (DLTYPE = N'DL-1') AND (DEPT = N'ELE')"></asp:SqlDataSource>
            <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
            </asp:Repeater>
        
            <br />
        
        </div>
        </form>
    </body>
    </html>
    

    and the vb code is as follows

     

    Partial Class SR_2011_SRreport
        Inherits System.Web.UI.Page
        Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected
            Label1.Text = String.Format("Total Records: {0}", e.AffectedRows)
        End Sub
    
        Protected Sub SqlDataSource2_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
            Label2.Text = String.Format("ELE dept DL-1 records: {0}", e.AffectedRows)
        End Sub
    End Class
    
    Monday, May 16, 2011 9:20 PM