?
Solved

C# Hashtable write to listbox???

Posted on 2004-08-27
6
Medium Priority
?
3,587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 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
Industry Leaders: 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!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

764 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