Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am adding the following elements in a proper sequence at par with my table in database.

C#
objHT.Add("P_NAME", sName);
                objHT.Add("P_EMAIL", sEmail);
                objHT.Add("P_CONTACTNO", sContact);
                objHT.Add("P_COMPANY", sCompany);
                objHT.Add("P_ADDRESS", sAddress);
                objHT.Add("P_TIMELINE", sTimeline);
                objHT.Add("P_BUDGET", sBudget);
                objHT.Add("P_DETAILS", sPrjDtls);
                objHT.Add("FILEPATH", sFilePath);


but once the code is executed I find that they are added in the following order:

email, address, detail, name, contact, timeline, filelpath, budget, company

This is causing a problem as for fields there data type clash.

Please suggest me a solution or if 'am missing something.

Thanks

IB
Posted

You are not guaranteed order when adding to a Hashtable.
 
Share this answer
 
Comments
CPallini 31-Oct-13 4:29am    
5.
Mehdi Gholam 31-Oct-13 4:39am    
Cheers!
That's how hastables work. I suppose you have to use a "OrderedDictionary Class"[^] instead.
 
Share this answer
 
Comments
Mehdi Gholam 31-Oct-13 4:40am    
5'ed, although in the case for the question a normal Dictionary might be better since it seems to keep the add order.
CPallini 31-Oct-13 4:56am    
It is not guaranteed either: see
http://stackoverflow.com/questions/16694182/ordereddictionary-and-dictionary

By the way, thank you.
Indrojeet_Bhattacharya 31-Oct-13 5:11am    
Can you suggest using any other data structure which can maintain the order.
CPallini 31-Oct-13 5:21am    
I already did. Check out the documentation of OrderedDictionary class.
Indrojeet_Bhattacharya 31-Oct-13 5:21am    
I saw your link and it says, when you are removing the elements then the order is not guaranteed. In my case I am NOT removing any element, I am simple using this Hashtable as:

if (con.State == ConnectionState.Closed)
{
con.Open();
}
sqlTran = con.BeginTransaction();
sqlCmd = new SqlCommand();
sqlCmd.Transaction = sqlTran;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlParam = sqlCmd.Parameters.Add("@return", System.Data.SqlDbType.VarChar);
sqlParam.Direction = ParameterDirection.ReturnValue;
sqlCmd.CommandText = sProcName;
sqlCmd.Connection = con;
IDictionaryEnumerator objEnum = objHT.GetEnumerator();
while (objEnum.MoveNext())
{
sqlCmd.Parameters.Add(new SqlParameter("@" + (objEnum.Key.ToString()), objEnum.Value));
}
sqlCmd.ExecuteNonQuery();

sqlTran.Commit();
sRetVal = sqlCmd.Parameters["@return"].Value.ToString();

whats your opinion ?

IB
Thanks for your efforts guys! but since i cannot use an unreliable data structure i fell back to procedure execution and solved it myself. Actually i was trying to create a generalized library method which could have eased the task for any insert operation. :)
 
Share this answer
 

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