Solved

C# Hashtable write to listbox???

Posted on 2004-08-27
6
3,529 Views
Last Modified: 2008-02-20
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
Comment
Question by:kvnsdr
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 11921303
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
 
LVL 4

Expert Comment

by:MrGhost
ID: 11921345
Why don't you use DataSource to fill your ListBox why do you need hastable!
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11921466
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:MrGhost
ID: 11921562
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11921812
are you doing anything else with the hashtable ? like using it to search later ?
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11922556
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now