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.ArrayLi st();
try
{
cn.Open();
cmd = new SqlCommand("SELECT SubCat, Question FROM Questions WHERE SubCat = '" + this.ComboBoxValueType.ToS tring() + "'", 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().Me ssage);
}
ArrayList al = new System.Collections.ArrayLi
try
{
cn.Open();
cmd = new SqlCommand("SELECT SubCat, Question FROM Questions WHERE SubCat = '" + this.ComboBoxValueType.ToS
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
MessageBox.Show("Base Exception: " +
excp.GetBaseException().Me
}
ASKER
how do you declare a vector in C#?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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).ToStrin g();
and use the ExecuteScalar instead of reader as this returns the first row
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["Customer s"];
}
catch (Exception excp)
{
MessageBox.Show("Exception : " + excp.Message);
MessageBox.Show("Base Exception: " +
excp.GetBaseException().Me ssage);
}
finally
{
cn.Close();
}
}
private void comboBox1_SelectedIndexCha nged(objec t sender, System.EventArgs e)
{
//When an item is selected in the combo box place its value in the text box
textBox1.Text = comboBox1.SelectedValue.To String();
}
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,"
// set what the user will see
comboBox1.DisplayMember = "CustomerID";
//set the corresponding value
comboBox1.ValueMember = "CompanyName";
comboBox1.DataSource = myDataSet.Tables["Customer
}
catch (Exception excp)
{
MessageBox.Show("Exception
MessageBox.Show("Base Exception: " +
excp.GetBaseException().Me
}
finally
{
cn.Close();
}
}
private void comboBox1_SelectedIndexCha
{
//When an item is selected in the combo box place its value in the text box
textBox1.Text = comboBox1.SelectedValue.To
}
unless you have any special requirement, substitute one for the other.