• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3611
  • Last Modified:

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());
                  }

0
kvnsdr
Asked:
kvnsdr
  • 2
  • 2
  • 2
1 Solution
 
gregoryyoungCommented:
not really I am assuming this is actually in later code and just copy / pasted here...

               {    
                    string key = hash[0].ToString();
                    string val = hash [1].ToString();
                    listBox1.Items.Add(key.ToString() + "\t" + val.ToString());
               }
try this instead.

foreach (DictionaryEntry de in myHashtable) {
    listBox1.Items.Add(de.Key.ToString() + "\t" + de,Value.ToString());
}
0
 
MrGhostCommented:
Why don't you use DataSource to fill your ListBox why do you need hastable!
0
 
kvnsdrAuthor Commented:
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?
0
Independent Software Vendors: 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!

 
MrGhostCommented:
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
0
 
gregoryyoungCommented:
are you doing anything else with the hashtable ? like using it to search later ?
0
 
kvnsdrAuthor Commented:
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);
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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