[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5359
  • Last Modified:

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);
}
0
vcgDevelopers
Asked:
vcgDevelopers
  • 3
  • 2
  • 2
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now