arraylist?

Is an arraylist the best way to do this?  If so how do I get my items out of my array list?  I'm a java developer (thrust into a new language with a short time frame) and I'm ued to Vectors and Enumerations....

ArrayList al = new System.Collections.ArrayList();

try
{
      cn.Open();
      
      cmd = new SqlCommand("SELECT SubCat, Question FROM Questions WHERE SubCat = '" + this.ComboBoxValueType.ToString() + "'", cn);
      SqlDataReader readr = cmd.ExecuteReader();
      //questions.Items.Clear();
      while (readr.Read())
      {
            String SubCat = readr.GetString(0);
            String quest = readr.GetString(1);            

            questions.Text = quest.ToString();
            al.Add(quest.ToString());

      }

      readr.Close();
      
}
catch (Exception excp)
{
      MessageBox.Show("Exception: " + excp.Message);
      MessageBox.Show("Base Exception: " +
      excp.GetBaseException().Message);
}
vcgDevelopersAsked:
Who is Participating?
 
PeteMcCommented:
you can iterate through an array list using a for loop

for(int i = 0;i<al.Count;i++)
{
        Console.WriteLine(al[i].ToString());
}

However without knowing where you are trying to put the information I can't say if this is the correct method to use if you are trying to place the informationinto another control you should look at dataviews and binding.

 a quick note about the code above the line
    cmd = new SqlCommand("SELECT SubCat, Question FROM Questions WHERE SubCat = '" + this.ComboBoxValueType.ToString() + "'", cn);

should be
SqlCommand cmd = new SqlCommand("SELECT SubCat, Question FROM Questions WHERE SubCat = '" + this.ComboBoxValueType.ToString() + "'", cn);
0
 
absongCommented:
ArrayList = Vector, or almost.
unless you have any special requirement, substitute one for the other.
0
 
vcgDevelopersAuthor Commented:
how do you declare a vector in C#?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
vcgDevelopersAuthor Commented:
basically how i want it to work is that once a question is answered it will load the next question in a text box (non-editable) on the form itself
0
 
absongCommented:
>>  how do you declare a vector in C#?
What I meant was, ArrayList in C# is like Vector in Java.
Just use ArrayList the way you would use Vector in Java.
0
 
PeteMcCommented:
if You are expecting only one value back from the reader you could place it directly into the text box                               textBox1.Text = readr.GetString(1).ToString();
 and use the ExecuteScalar instead of reader as this returns the first row
0
 
PeteMcCommented:
here is a example using databinding to achieve the selecting an item in a combo box  and placing the result in a text box using the northwind database
private void Form1_Load(object sender, System.EventArgs e)
{
      SqlConnection cn = new SqlConnection("Integrated Security=SSPI;data source= SERVERNAME ;persist security info=True;initial catalog=northwind");
      try
      {

            SqlDataAdapter myAdapter = new SqlDataAdapter();
            SqlCommand cmd = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", cn);
            cmd.CommandType = CommandType.Text;
            myAdapter.SelectCommand = cmd;
            DataSet myDataSet = new DataSet("Customers");
            myAdapter.Fill(myDataSet,"Customers");
                               // set what the user will see
            comboBox1.DisplayMember = "CustomerID";
                               //set the corresponding value
            comboBox1.ValueMember = "CompanyName";
            comboBox1.DataSource = myDataSet.Tables["Customers"];
            
      }
      catch (Exception excp)
      {
            MessageBox.Show("Exception: " + excp.Message);
            MessageBox.Show("Base Exception: " +
            excp.GetBaseException().Message);
      }
      finally
      {
            cn.Close();
      }
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
                //When an item is selected in the combo box place its value in the text box
      textBox1.Text = comboBox1.SelectedValue.ToString();
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.