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()
        {
            InitializeComponent();
        }

        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);

            con.Open();

            da.Fill(ds1, "Workers");
            NavigationRecords();
            con.Close();
        }

        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());
                item.SubItems.Add(dRow["last_Name"].ToString());
                item.SubItems.Add(dRow["job_Title"].ToString());

                listView1.Items.Add(item);

            }
        }

    }
}

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

Kind regards
Mark

Hi mark,

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

Regards,
Wen Cai

Hi Wen,

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

Thank you for your help !

regards
Mark.

Be a part of the DaniWeb community

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