hi,
i have a method that get the firstname and the lastname of an employee in the database and merge it together and then put it to an array and returns the array how can i return the array from the method. the code is below.

C# Syntax

//This methods retrieves all the Eployee anes who are US citizense
        public String LoadUSEmp()
        {
            String firstName;
            String lastName;
            
             db.openConnection();
            String query = @"Select FirstName,LastName From Employee Where Citizenship = 'US' ";
            SqlCommand comm = new SqlCommand(query, DB.getConnection());
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataTable dt = new DataTable();

            da.Fill(dt);
            
            int no = dt.Rows.Count;
            MessageBox.Show(no.ToString());
           
            String[] fullName= new String[no];
            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[no];
        }

so that i can load this array information to a four combobox in a loop

this will be like :

cbPI.Items.Add(LoadUSEmp());
cbPI.SelectedIndex = 0;


Question 1:
how can i return a string array from the LoadUSEmp??


Question 2:
how can i add the details in an array to the combo box???

i need to make a method so that this can be call in several interfaces

please can someone give me an answer?

thankkkkkk

Recommended Answers

Edit your method and use "string[]" for the method type. Also use "return fullName;" instead of "return fullname[no]" because that will return a single string and not an array.

Here's the working method:

Changes are in bold.

public [B]string[][/B] LoadUSEmp()
        {
            String firstName;
            String lastName; …
Jump to Post

All 2 Replies

Edit your method and use "string[]" for the method type. Also use "return fullName;" instead of "return fullname[no]" because that will return a single string and not an array.

Here's the working method:

Changes are in bold.

public [B]string[][/B] LoadUSEmp()
        {
            String firstName;
            String lastName;
            
             db.openConnection();
            String query = @"Select FirstName,LastName From Employee Where Citizenship = 'US' ";
            SqlCommand comm = new SqlCommand(query, DB.getConnection());
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataTable dt = new DataTable();

            da.Fill(dt);
            
            int no = dt.Rows.Count;
            MessageBox.Show(no.ToString());
           
            String[] fullName= new String[no];
            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 fullName;[/B]
        }

To add them to the ComboBox you can use AddRange() method:

cbPI.Items.AddRange(LoadUSEmp());
cbPI.SelectedIndex = 0;

Thanks

hey thank you very much!!!!!
:)

Be a part of the DaniWeb community

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