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

Dani AI

Generated

Best practice: let the database do the alphabetizing and return only the columns needed (as suggested). Ordering on the server reduces traffic and uses the database collation for a predictable alphabetical order. For an address-book style display, order by last name then first name rather than by first name alone.

Example (server-side ORDER BY + safe reader pattern):

using (var conn = DB.getConnection())
using (var cmd = new SqlCommand(
    "SELECT FirstName, LastName FROM Employee ORDER BY LastName, FirstName", conn))
{
    conn.Open();
    var names = new List<string>();
    using (var rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            string fn = rdr.IsDBNull(0) ? "" : rdr.GetString(0).Trim();
            string ln = rdr.IsDBNull(1) ? "" : rdr.GetString(1).Trim();
            names.Add((fn + " " + ln).Trim());
        }
    }
    return names.ToArray();
}

If the DataTable is already filled, a client-side LINQ approach is simple and culture-aware:

var fullNames = dt.AsEnumerable()
    .Select(r => ((r.Field<string>("LastName") ?? "").Trim() + ", " + (r.Field<string>("FirstName") ?? "").Trim()).Trim())
    .OrderBy(s => s, StringComparer.CurrentCultureIgnoreCase)
    .ToArray();

Notes and troubleshooting: correctly pointed out the Array.Sort issue — Array.Sort sorts in place and returns void, so calling return Array.Sort(...) causes the "cannot convert void to string" error. Use Array.Sort(fullName, StringComparer.CurrentCultureIgnoreCase); then return fullName; if sorting client-side. Also avoid SELECT * (request only needed columns), use using blocks (to always close connections), handle DBNull/trim whitespace, and pick server vs client sorting based on dataset size and desired collation/case rules.

Recommended Answers

All 4 Replies

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

hey it says cannot convert void to string with return Array.Sort(fullName);

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;

        }
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.