+ Reply to Thread
Results 1 to 2 of 2

Thread: Error saving database records

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    40

    Error saving database records

    I'm trying to read all the records of a database using .net, and I have a logical error in the while loop, I can't understand what the problem is, here is the code :
    Code:
    {
        System.Data.OleDb.OleDbConnection  con;
        DataSet dsl;
        System.Data.OleDb.OleDbDataAdapter da;
        public String accessDatabase()
        {
            initializecomponent();
    
            con = new System.Data.OleDb.OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Owner\\Documents\\CIS3052.mdb";
            dsl = new DataSet();
            String displayID = null;
             String displayFname = null;
             String displayLname = null;
             String displayAge = null;
             String displayJob = null;
             String viewAll = null;
             int inc = 0;
             int MaxRows = 0;
    
    
            string sql = "SELECT * FROM Employee";
            da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
            con.Open();
            da.Fill(dsl, "Employee");
    
            MaxRows = dsl.Tables["Employee"].Rows.Count;
            while (inc != MaxRows -1)
            {
    
            DataRow dRow = dsl.Tables["Employee"].Rows[inc];
            displayID = dRow.ItemArray.GetValue(0).ToString();
            displayFname = dRow.ItemArray.GetValue(1).ToString();
            displayLname = dRow.ItemArray.GetValue(2).ToString();
            displayAge = dRow.ItemArray.GetValue(3).ToString();
            displayJob = dRow.ItemArray.GetValue(4).ToString();
            viewAll = viewAll + displayID + " " + displayFname + " " + displayLname + " " + displayAge + " " + displayJob + " ";
            }
    
    
    
            con.Close();
            con.Dispose();
    
    
    
            return viewAll;
        }
    
    
    }

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    45
    I guess you should try it with a FOR Loop , as it may help :
    Code:
    for(int i = 0; i < MaxRows; i++)
    {    
        DataRow dRow = dsl.Tables["Employee"].Rows[i];
        displayID = dRow.ItemArray.GetValue(0).ToString();
        displayFname = dRow.ItemArray.GetValue(1).ToString();
        displayLname = dRow.ItemArray.GetValue(2).ToString();
        displayAge = dRow.ItemArray.GetValue(3).ToString();
        displayJob = dRow.ItemArray.GetValue(4).ToString();
        viewAll = viewAll + displayID + " " + displayFname + " " + displayLname + " " + displayAge + " " + displayJob + " ";
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts