hey,
i have written method to get the names of the employees from the database. how can i sort this to alphabetical order the code is below

C# Syntax

public String[] FillComboBox()
        {
            int NoOfRows;
            String firstName;
            String lastName;

            db.openConnection();
            
            DisplayLoginUserName();


            String query = @"Select * From Employee;";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            da.Fill(dt);

            NoOfRows = dt.Rows.Count;
            String[] fullName = new String[NoOfRows];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                firstName = dt.Rows[i]["FirstName"].ToString();
                lastName = dt.Rows[i]["LastName"].ToString();
                fullName[i] = firstName + " " + lastName;

            }
            da.Dispose();

            db.closeConnection();
            return fullName;

        }

hey help me
thanxxxxxxxxxxx

Hey,
You should add this to your existing SQL query:

String query = @"Select * From Employee ORDER BY <Put the column name that you want the result to be sorted out>;";

Hope i helped! :)

You can use:

public String[] FillComboBox()
        {
            int NoOfRows;
            String firstName;
            String lastName;

            db.openConnection();
            
            DisplayLoginUserName();


            String query = @"Select * From Employee;";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            da.Fill(dt);

            NoOfRows = dt.Rows.Count;
            String[] fullName = new String[NoOfRows];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                firstName = dt.Rows[i]["FirstName"].ToString();
                lastName = dt.Rows[i]["LastName"].ToString();
                fullName[i] = firstName + " " + lastName;

            }
            da.Dispose();

            db.closeConnection();
            [B]return Array.Sort(fullName);[/B]

        }

Thanks

I suggest you use Alexpap's idea. Edit your sql query. I think this is how you want your query:

String query = @"Select * From Employee [B]ORDER BY FirstName ASC[/B];";

Oh and sorry. My code has a problem. Don't use my method. The one suggested by Alex is better. By the way, here's the working code:

Don't use this code with the above query.

public String[] FillComboBox()
        {
            int NoOfRows;
            String firstName;
            String lastName;

            db.openConnection();
            
            DisplayLoginUserName();


            String query = @"Select * From Employee;";
            SqlCommand command = new SqlCommand(query, DB.getConnection());
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();

            da.Fill(dt);

            NoOfRows = dt.Rows.Count;
            String[] fullName = new String[NoOfRows];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                firstName = dt.Rows[i]["FirstName"].ToString();
                lastName = dt.Rows[i]["LastName"].ToString();
                fullName[i] = firstName + " " + lastName;

            }
            da.Dispose();

            db.closeConnection();
            Array.Sort(fullName);
            return fullName;

        }

Edited 6 Years Ago by farooqaaa: n/a

This article has been dead for over six months. Start a new discussion instead.