Google

Wednesday, November 7, 2007

To add value in Hashtable from SQLReader

If you want to add the values to HashTable which you are reading through SQLDataReader , then this is what you have to write, Hashtable myHash =new Hashtable(); while(reader.Read()) { string type = reader.GetString(0).Trim(); int num = reader.GetInt32(1).Trim(); myhash.Add(num,type); } Now if you want to add more than one values to Hashtable while reading it from a SQLDataReader, you will have to put the values in a class and make an object of it to insert in the value for Hashtable. It goes like this : public class myClass { private string sType; private int sNumber; private Datetime stimeType; public myClass(string Type,int Number,Datetime timeType) { sType = Type; sNumber = Number; stimeType = timeType; } public string getType() { return sType; } public int getNumber() { return sNumber; } public Datetime gettimeType() { return stimeType; } } public void readVal() { Hashtable myHash =new Hashtable(); while(reader.Read()) { string type = reader.GetString(0).Trim(); int num = reader.GetInt32(1).Trim(); Datetime time= reader.GetDateTime(2); myClass myObj = new myClass(type,num,time); myhash.Add(num,myObj); } }

No comments: