Link to home
Start Free TrialLog in
Avatar of vcgDevelopers
vcgDevelopers

asked on

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);
}
Avatar of absong
absong

ArrayList = Vector, or almost.
unless you have any special requirement, substitute one for the other.
Avatar of vcgDevelopers

ASKER

how do you declare a vector in C#?
ASKER CERTIFIED SOLUTION
Avatar of PeteMc
PeteMc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
>>  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.
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
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();
}