Hi guys,

I have an issue with the code I've written in C# where the listview will connect to the database without a problem but it will only populate the 1st row of the table BUT.... it will duplicate it for the exact amount of records I am expecting to see.

For Example: First Name, Last Name, Job Title are the Column names. When viewing the table in SQL I have 16 rows of data.

but when I run my code it picks up the 1st row of data and then duplicates it all the way down the list view 16 times. Its driving me nuts!

Below is the code I have written.

namespace AccessDB_and_load_selected_Item_into_Text_Boxes
    public partial class Form1 : Form
        public Form1()

        System.Data.SqlClient.SqlConnection con;
        System.Data.SqlClient.SqlDataAdapter da;
        DataSet ds1;
        int inc = 0;

        private void Form1_Load(object sender, EventArgs e)
            con = new System.Data.SqlClient.SqlConnection();
            ds1 = new DataSet();

            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            string sql = "SELECT * FROM tblWorkers";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);


            da.Fill(ds1, "Workers");

        private void NavigationRecords()
            DataTable dtable = ds1.Tables["Workers"];

            for (int i = 0; i < dtable.Rows.Count; i++)
                DataRow dRow = ds1.Tables["Workers"].Rows[inc];

                ListViewItem item = new ListViewItem(dRow["first_Name"].ToString());




Hope someone is able to point out where I'm going wrong here.

Kind regards

Hi mark,

Line number 38. Increment your Row with i instead of inc.

Wen Cai

Hi Wen,

Excellent! Many thanks for that. Worked Like a dream. Now onto my next little goal :)

Thank you for your help !


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.