Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my following code i get the error -
the type or namespace for hashtable is not found .are you missing an assembly reference?
code is-
C#
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=SureshDasari; Initial Catalog=MySampleDB; Integrated Security=true");
con.Open();
SqlCommand cmd = new SqlCommand("select * from SampleTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"UserDetails");
DataTable dt = ds.Tables["UserDetails"];
RemoveDuplicateRows(dt, "UserName"); // Here UserName is Column name of table
gvDetails.DataSource = ds;
gvDetails.DataBind();
}
}
// This method is used to delete duplicate rows of table
public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
    Hashtable hTable = new Hashtable();
    ArrayList duplicateList = new ArrayList();
    foreach (DataRow dtRow in dTable.Rows)
    {
        if (hTable.Contains(dtRow[colName]))
            duplicateList.Add(dtRow);
        else
            hTable.Add(dtRow[colName], string.Empty);
    }
    foreach (DataRow dtRow in duplicateList)
        dTable.Rows.Remove(dtRow);
    return dTable;
}

the references used are-
C#
using System;
using System.Configuration;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using MySql.Data.MySqlClient;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Data;


please suggest what to do
Posted
Updated 15-Mar-15 10:47am
v3
Comments
Sergey Alexandrovich Kryukov 15-Mar-15 19:26pm    
Don't use this type.
—SA

1 solution

You need a
C#
using System.Collections;

Hashtable is a non-generic collection, so it sits in that namespace.

Edit:

I strongly suggest you use generic collections whenever there is no very good reason not to. So, no Hashtable, no ArrayList and whatever else there is*. Instead Dictionary<TKey, TValue>, List<T>, HashSet<T> and so forth. You gain compile-time type-safety which is worth a lot (in order to avoid bugs in the first place) and also small performance gains for value-types (no boxing/unboxing).

Another Edit:
* Please see Sergey's comments below. There are some collections of which there is no generic implementation, so for those my above suggestion would not be applicable.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 15-Mar-15 19:25pm    
Agree, a 5.
I would also add:
1) the assembly is mscorlib.dll, it's very unlikely that it is not referenced;
2) using System.Collection is not mandatory (as any other user directive), one can always use full names, such as System.Collections.Hashtable.
Yes, not-generic collections types have been rendered obsolete as early as of v.2.0, and using them makes no sense at all, but using non-generic specialized collections, some of them, still may make sense.
—SA
[no name] 15-Mar-15 19:44pm    
Thank you, Sergey!
You're right there. Though off the top of my head I can't think of a non-generic specialized collection besides BitArray (which isn't so much of a collection in my eyes) - what do have in mind there?
Btw, BillWoodruff suggested I make a Tip/Trick about that Bind-Enum-To-ComboBox-Answer from yesterday and I did, please take a look :-) http://www.codeproject.com/Tips/886694/Binding-Enum-Values-to-a-ComboBox
- Sebastian
Sergey Alexandrovich Kryukov 15-Mar-15 20:21pm    
I rather formally mentioned them: if some collection is specialized, it just does not need to be generic.

Some useful examples: BitVector32, CollectionUtil (not really a collection itself)...

My note that "non-generic" shoud be added to your statement remain correct, but actually I think this namespace is not so good. String-specific collections are redundant. Once some down-voted me for using List<string>, which is one of my habits, I fixed the answer, but later noticed the it lacks some members I really need; and there is no real reason to use StringCollection.

Now, its pretty apparent that HybridDictionary and ListDictionary could be made also generic, but they are not, which apparently makes no sense.

—SA
[no name] 15-Mar-15 20:50pm    
Got it. When reading your first comment I thought you meant specialized in that there's no non-generic version, I didn't consider type-specialized collections. I updated my answer.
Does the inquirer get a notification about an edited answer?
Sergey Alexandrovich Kryukov 15-Mar-15 21:03pm    
Even if the update of the answer does not send notification to the inquirer (logically, it should, I just never had a chance to observe it), your comment above, the one I'm commenting on, will cause the notification, so not to worry...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900