Nave Tseva
Nave Tseva

Reputation: 401

for loop in aspx

I have data base when there are user id there; and I have a for loop, I want that the loop will run until the last ID example: for (i=0; i > something; i++) my question is what should be this something? I also have the start of the code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    public string strLname;
    public string strEmail;
    public string strFname;
    protected void Page_Load(object sender, EventArgs e)
    {

        string dbPath = Server.MapPath(@"App_Data") + "/my_site.mdb";
        string connectionString = @"Data Source='" + dbPath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
        OleDbConnection con = new OleDbConnection(connectionString);
        con.Open();
        string QueryString = "SELECT * FROM tbl_users";
        OleDbCommand cmd = new OleDbCommand(QueryString, con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "tbl");
        con.Close();
        for (i=0; i < *something*; i++)

        }
    }

Upvotes: 0

Views: 3801

Answers (3)

Vish Soni
Vish Soni

Reputation: 43

I think if you use foreach is much more better. something like,,,

foreach(DataRow dRow in ds.Table["Your Table Name"].Rows)
{
    //    dRow["id"] is your id column and you can access the value in that 
    //some sort of your operation code comparison or anything you want
}

Also i have noticed something in your code that you haven't used fail-safe. so remember when you write code please don't forget to use try catch as you are dealing with database connection and related work.

Upvotes: 0

Ry-
Ry-

Reputation: 225054

A foreach loop would be more convenient, wouldn't it?

foreach(DataRow row in ds.Tables["tbl"].Rows) {
    // ...
}

The ID would be row["ID"] in each row. (Or whatever you called it.)

And just in case you were planning on using i in a for loop as the ID, be careful if you ever delete rows.

Upvotes: 3

Adil
Adil

Reputation: 148150

You can use the rows count of table at index 0 or what ever index you have.

for (i=0; i < ds.Tables[0].Rows.Count; i++)
{

}

Upvotes: 4

Related Questions