Solved

Dataset loads incorrect data from SQL server

Posted on 2006-10-21
3
241 Views
Last Modified: 2010-04-16
Hi all. I have an application that connects to a SQL server and loads data from it. Sometimes however it loads the columns in the wrong order. The "Users" table that I query has the columns in this order: Username, Policy, Tickets, OpenTickets, Notes, Image. Sometimes they load in this order: Policy, Username, Tickets, OpenTickets, Notes, Image.

The code that I use to load the data into a datatable is this, where userTable is my datatable.:

        public static void selectAllUsers()
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Users", Connections.SQLconnection);

            try
            {
                userTable.Clear();
                da.Fill(userTable);
            }
            catch (SqlException)
            {

            }
        }

Once in the dataset, the data is loaded into a listView on a form using this code:

        private void getUsers()
        {
            //Clear the ListView
            listViewUsers.Items.Clear();

            //Select all rows from the Users table
            UsersSQL.selectAllUsers();

            //Add each row of the DataTable to the ListView
            foreach (DataRow row in UsersSQL.userTable.Rows)
            {
                ListViewItem lvi = new ListViewItem(row[0].ToString());
                lvi.SubItems.Add(row[1].ToString());
                lvi.SubItems.Add(row[2].ToString());
                lvi.SubItems.Add(row[3].ToString());
                lvi.SubItems.Add(row[4].ToString());
                listViewUsers.Items.Add(lvi);
            }

            //Update controls
            if (listViewUsers.Items.Count > 0)
            {
                // Set the column number that is to be sorted; default to ascending.
                lvwColumnSorter.SortColumn = 0;
                lvwColumnSorter.Order = SortOrder.Ascending;

                // Perform the sort with these new sort options.
                this.listViewUsers.Sort();

                //listViewUsers.Items[0].Selected = true;
                toolStripButtonDelete.Enabled = true;
                toolStripButtonEdit.Enabled = true;
            }
            else
            {
                toolStripButtonDelete.Enabled = false;
                toolStripButtonEdit.Enabled = false;
            }

            toolStripProgressBar.Value = 100;
            toolStripStatusLabelStatus.Text = listViewUsers.Items.Count.ToString() + " Users Found";
        }

I think that the problem comes when loading data into the dataTable, rather than when populating the form, but I'm not sure. It's also worth noting that the problem only seems to occur after I change the user's policy and update the Policy column. For that I use this code:

        public static void updateUserPolicy(string oldPolicy, string newPolicy)
        {
            try
            {
                //Begin a new transaction on our main connection
                sqlTrans = Connections.SQLconnection.BeginTransaction();
                sqlCommand = new SqlCommand(@"UPDATE Users SET Policy='" + Gatekeeper.SQLShieldRemoveApostrophes(newPolicy) + "' WHERE Policy='" + Gatekeeper.SQLShieldRemoveApostrophes(oldPolicy) + "'", Connections.SQLconnection, sqlTrans);
                //Execute the transaction
                sqlCommand.ExecuteNonQuery();
                //Try to commit changes to the database
                sqlTrans.Commit();
            }
            catch (SqlException)
            {
                //If there is an error, roll back the transaction
                sqlTrans.Rollback();
            }
        }

Gatekeeper.SQLShieldRemoveApostrophes(); protects against some SQL injection attacks, but is not the cause of the problem. The transaction executes fine, but afterwards I find the two columns reversed. I hope someone can figure this out. Thanks.
0
Comment
Question by:pab89
  • 2
3 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17781343
Try changing your select statement to explicitly select the columns in the order that you want, instead of 'select *'. Just a suggestion, may not work.

An alternative would be when adding the ListView SubItems, reference the row by name instead of the ordinal position.
0
 
LVL 1

Accepted Solution

by:
pascalprob earned 325 total points
ID: 17791506
To guarantee the order in which your select statement retrieves columns change it to:
"SELECT Username, Policy, Tickets, OpenTickets, Notes, Image FROM Users"
If this does not solve the problem, you will at least know that the select statement is not at fault.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17817153
Hmm, that looks suspiciously like what I said - "Try changing your select statement to explicitly select the columns in the order that you want, instead of 'select *'".
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now