how to count the fetched rows by sqldatasource
Asked Answered
V

7

8
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim LoginChecker As New SqlDataSource()
    LoginChecker.ConnectionString = ConfigurationManager.ConnectionStrings("A1ConnectionString1").ToString()
    LoginChecker.SelectCommandType = SqlDataSourceCommandType.Text
    LoginChecker.SelectCommand = "SELECT username FROM A1login WHERE username=@username AND password=@password"
    LoginChecker.SelectParameters.Add("username", username.Text)
    LoginChecker.SelectParameters.Add("password", password.Text)
    Dim rowsAffected As Integer = 0
    Try
        rowsAffected = LoginChecker.<what i have to write here>
    Catch ex As Exception
        'Server.Transfer("LoginSucessful.aspx")
    Finally
        LoginChecker = Nothing
    End Try
    username.Text = rowsAffected
    ' If rowsAffected = 1 Then
    'Server.Transfer("A1success.aspx")
    '   Else
    'Server.Transfer("A1failure.aspx")
    '  End If

End Sub

this is the code for login.aspx.vb
it checks the username and password in the database and redirecrs to respective page on the basis of rows returned. i'm having problem in finding a right function in the sqldatareader namespace so that it counts the number of rows affected. Can anybody tell me the function I should use there? Thanks in advance.

Viridescent answered 7/11, 2010 at 17:15 Comment(0)
C
3

Use the Selected event of the SqlDataSource Control. The event is raised after the Select operation is complete.

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    lblRecordCount.Text = "Record Count: " + e.AffectedRows.ToString();
}
Cheesewood answered 8/7, 2018 at 2:54 Comment(0)
B
1
protected void Button1_Click(object sender, EventArgs e)
{
    DataView d =(DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    Response.Write(d.Count);
}
Bullpen answered 1/8, 2013 at 2:46 Comment(0)
K
0

Select will return an IEnumerable. You you can ToList().Count on this.

Kremer answered 7/11, 2010 at 18:10 Comment(1)
thanks . but, can u plz elaborate what you are saying . correct me if i'm wrong -------- objective is to count the number of IEnumerables so to do that how to code .. is it ToList(LoginChecker).CountViridescent
C
0

The SQLDataReader doesn't have a row count property the easiest way to get the count would be to do this...

int rowCount = 0;
if (dr.HasRows)  
{  
    while (dr.Read())  
        {  
            rowCount++;
            dgResults.DataSource = dr;  
            dgResults.DataBind();  
            dgResults.Visible = true;  
        }  

lblMsg.Text = rowCount.ToString();
dr.Close(); 
Clamatorial answered 7/11, 2010 at 18:32 Comment(1)
no man there is an error , there is nothing like HasRows can u plz verify and tell me . shall i need to include any other specific namespace .Viridescent
C
0

You should read this page

 Namespace:  System.Data 
 Assembly:  System.Data (in System.Data.dll)
Clamatorial answered 12/11, 2010 at 6:8 Comment(0)
V
0

To fetch your rows, declare sqlcommand and sqlconnection:

Dim cmd as sqlcommand
Dim con as new sqlconnection("Ur connection string")
cmd.connection=con
cmd.commandtext="SELECT count(username) FROM A1login WHERE username=@username AND password=@password"
Dim rowsAffected As Integer = cmd.executescalar()

In rowsaffected you will get the total number of rows fetched

Vilma answered 23/11, 2010 at 9:55 Comment(0)
S
0
SELECT Count(*) FROM A1login WHERE username=@username AND password=@password"

This Code return Count value

Spunky answered 24/8, 2013 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.