Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

C# Hashtable write to listbox???

I'm deciding whether or not my sql is writing to my hashtable.

Q. Does this code look correct???

string SQL1 = "SELECT col_1, col_2 FROM myTable";
SqlConnection cn1 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da1 = new SqlDataAdapter(SQL1,cn1);
cn1.Open();
SqlDataReader SqlReader1 = da1.SelectCommand.ExecuteReader();

hash = new Hashtable();      
            
listBox1.Items.Clear();

while(SqlReader1.Read())
      {                  
            str_col_1 = SqlReader1.GetString(0);
            str_col_2 = SqlReader1.GetString(1);

            hash.Add(str_col_1.Trim(), str_col_2.Trim());
                  {      
                        string key = hash[0].ToString();
                        string val = hash [1].ToString();
                        listBox1.Items.Add(key.ToString() + "\t" + val.ToString());
                  }

ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

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
Avatar of MrGhost
MrGhost

Why don't you use DataSource to fill your ListBox why do you need hastable!
Avatar of kvnsdr

ASKER

Well, I definitly need the 'key - value' pair concept. I've read that a SortedList is very similar to a hashtable. The hashtable was suggested to me and that's why I began using it, although I can see it may be and old inefficient way of doing things, so I'm open to suggestions.

Q. How can you make the DataSource work with with my SQL command?  

Q. How can you make the DataSource writes it's stored information out to a listbox?
to get you started:

                        OleDbConnection conn = new OleDbConnection("<connection string>");
                  OleDbDataAdapter da = new OleDbDataAdapter("SELECT col_1, col_2 FROM myTable", conn);
                  DataSet ds = new DataSet();
                  da.Fill(ds);
                  listBox1.DataSource = ds.Tables[0].DefaultView;
                  also check listBox1.DisplayMember and listbox1.ValueMember
are you doing anything else with the hashtable ? like using it to search later ?
Avatar of kvnsdr

ASKER

This is the raw code. I left out the 'catch' and 'finally' to conserve space. Basically I'm quickly loading a Hashtable from an active (threaded) SQL DataTable for later key-value pair retrieval by the 'LoadListView' method. I like the idea of a DataSet better than a hashtable, but not sure how to get the results I need. I was reader (splitting) to the hashtable from a Text.txt file with a slightly different coding with great success. That's why I tried to convert to reading an SQL DataTable, thought it would be easy....

Viewing the final results in a the ListView is my ultimate goal.

The ListBox is to meerly 'peek' inside the hashtable to asure it actually contains data.


private void LoadHashtable()

string SQL1 = "SELECT col_1, col_2 FROM myTable";
SqlConnection cn1 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da1 = new SqlDataAdapter(SQL1,cn1);
cn1.Open();
SqlDataReader SqlReader1 = da1.SelectCommand.ExecuteReader();

hash = new Hashtable();    
listBox1.Items.Clear();

while(SqlReader1.Read())
     {              
          str_col_1 = SqlReader1.GetString(0);
          str_col_2 = SqlReader1.GetString(1);

          hash.Add(str_col_1.Trim(), str_col_2.Trim());
       }
          listBox1.Items.Add(key.ToString() + "\t" + val.ToString());


private void ShowListView1()

/// Various SQL commands that work are here, I'm conserving space.......

listView1.Items.Clear();
listView1.BeginUpdate();

{
try
{
while(SqlReader2.Read())
{
string str_col_1 = SqlReader2.GetString(0);
string str_col_2 = SqlReader2.GetString(1);
                        
if (hash.ContainsKey(str_col_1))
{
strMatch = hash[str_col_9].ToString();      
}
else
{
strMatch = "No Match Found";
}

ListViewItem item1 = new ListViewItem(str_col_1.ToString());
item1.SubItems.Add(strMatch.ToString());
listView1.Items.Add(item1);
}