How to count extracted sqldatasource rows

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 on the corresponding page based on the returned strings. I had a problem finding the right function in the sqldatareader namespace so that it counts the number of rows affected. Can someone tell me which function should I use there? Thank you in advance.

+4
source share
6 answers
 protected void Button1_Click(object sender, EventArgs e) { DataView d =(DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty); Response.Write(d.Count); } 
+1
source

Select will return IEnumerable. You can ToList (). Count on it.

0
source

There is no row count property in SQLDataReader, the easiest way to get a counter is 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(); 
0
source

You must read this page.

  Namespace: System.Data Assembly: System.Data (in System.Data.dll) 
0
source

To get 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 selected rows

0
source
 SELECT Count(*) FROM A1login WHERE username=@username AND password=@password " 

This code returns the value of the account.

0
source

All Articles