Solved

Dataset loads incorrect data from SQL server

Posted on 2006-10-21
3
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ado.net AddWithValue when using IN condition 7 46
How to force output to ascii 2 65
VS C# how to design libary of functions 7 33
Which is best Image Resizing Web service 11 42
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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