Wednesday, April 1, 2009
Group By on DataSet using DataView
public static DataTable GroupBy(DataTable table, string[] aggregate_columns, string[] aggregate_functions, Type[] column_types, string[] group_by_columns)
{
DataView view = new DataView(table);
DataTable grouped = view.ToTable(true, group_by_columns);
for (int i = 0 ; i < aggregate_columns.Length ; i++)
{
grouped.Columns.Add(aggregate_columns[i], column_types[i]);
}
foreach (DataRow row in grouped.Rows)
{
List <string> filter_parts = new List &l;string> ();
for (int i = 0; i < group_by_columns.Length; i++)
{
filter_parts.Add(string.Format("[{0}] = '{1}'", group_by_columns[i], row[group_by_columns[i]].ToString().Replace("'", "''")));
}
string filter = string.Join(" AND ", filter_parts.ToArray());
for (int i = 0; i < aggregate_columns.Length; i++)
{
row[aggregate_columns[i]] = table.Compute(aggregate_functions[i], filter);
}
}
return grouped;
}
And you should use something like
string[] column_names = new string[] { "number", "avg_price", "total" };
string[] column_functions = new string[] { "count(products)", "avg(price)", "sum(charge)" };
Type[] column_types = new Type[] { typeof(int), typeof(decimal), typeof(decimal) };
string[] group_by = new string[] { "order_id" };
DataTable grouped = GroupBy(data, column_names, column_functions, column_types, group_by);
Which will be the equivilant of the SQL command:
select order_id, count(products) as number, avg(price) as avg_price, sum(charge) as total from data group by order_id
it may need some extra handling to account for DBNull values in the columns that you are grouping by.
Synchronous process execution from VC++
Following is the Sample code for executing a process in VC++ synchronously. We can use System command also but that display a command propmt flash also. To avoid command prompt flash i have used earlier ShellExecute API but that runs a process Asynchronously.
ShellExecute(NULL,NULL,"C:\\Test.exe","Test Argument Param",NULL,SW_HIDE);
ShellExecute just returns the value 42 [If it fails return value is suppose to be <= 32] and program execution proceed to next line of code. This can create problem when one want ShellExecute to complete it's processing before it go to next line of code in program. Following is the solution i implemented for the same after avoiding ShellExecute and ShellExecuteEx APIs.
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW; // For enabling custon flags e.g. wShowWindow
si.wShowWindow = SW_HIDE; // For hiding the window of application
ZeroMemory( &pi, sizeof(pi) );
if (!CreateProcess( "C:\\Test.exe"," Test Argument Param",NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
{ printf("%s\n","ProcessFailed"); }
WaitForSingleObject( pi.hProcess, INFINITE ); // Wait until child process exits.
CloseHandle( pi.hProcess ); // Close process and thread handles.
CloseHandle( pi.hThread );
Please do comment if need anything furthur.
Saturday, March 28, 2009
fatal error C1083: Cannot open type library file: '<<AnyName>>.dll': No such file or directory
This is one of the error you can encounter while building a project and it means whether developer don't have <<AnyName>>.dll on the machine or dll is not registered. I faced the same error as follows
fatal error C1083: Cannot open type library file: 'regobj.dll': No such file or directory
Later i corrected it by downloading the regobj.dll from
http://download.microsoft.com/download/vb60pro/update/6.0/w9x2k/en-us/regobji.exe
i place the dll in System32 but one can keep it anywhere they want.
Same can be done for <<AnyName>>.dll , just check do you have <<AnyName>>.dll on your machine. if not then get it on you machine by downloading or taking from any other machine. Once you have it on your machine then register it using one of the following
regsvr32 <<AnyName>>.dll
or
regasm <<AnyName>>.dll /codebase
Virat
Monday, October 20, 2008
Airport Hassle
While i was thinking about what to do next after missing the flight I paid attention on announcement. it was about my flight and those fellows were calling my name. Hurra !.. i picked up my baggage and started running towards the runway entry. As soon as i reached there i saw two eyes staring at me like i am a waste fellow who is causing delay for others. :) what big deal if one airplane is waiting for me. They called cab to board me on plane. I started feeling like prince who was getting a royal treatment. People are waiting for me and i was getting treated very nicely.
All this day dreaming ends when i boarded on plane and come to know plane was delayed because of fog not because of me. They were giving just a formal announcement before takeoff and luckily i woke up. :)
When i finished this recalling moment, person sitting next to me was amused why i am smiling without reason and i kept on smiling leaving him guessing.
Memories
If I cry in the rain
can u see my tears..
if i blew a kiss in the dark
can u catch it..
If i hug u in my dreams
can u feel it..
if answer is no then y
don't u try it once for me...
If i sing a song far away from u
can u hear it...
if i didn't show u my feelings
can u know about it...
if i say "i coudn't live without u"
can u belive it...
If answer is no then y
don't u try it once for me
and in the last
if i die without your love
can u bear it....
if answer is no then y
don't you....
Wednesday, September 3, 2008
Google Chrome :- Beta Can't Make User Happy
Wednesday, August 20, 2008
Another Glorious day of my life
As soos as the day started it started to spread it's glory on me [Now i thanks god for starting this particular day late with 10:30 AM wake up]. i found my mobile is dead as there is not charge sooner i checked this my heart told there is some thing about the day. i found there is no clean cloth in my wardrobe so i have to manage with cloths i already sported one day. as i was late to office so started to be ready for office in hurry. left for office around 11:00 AM. after 500 Meter ride i realized i left my wallet at home. as usually i don't need my wallet so i decided to move on for office as i am getting late. after 2 KM i reached to the forum to diary circle road here was a big traffic jam. i started cursing the fellows who started the jam because of wrong traffic sense [later i realized it was whole because of me as my day was great]. anyway after 5 min struggle i crossed dairy circle flyover and moved towards office. i hardly covered 2 KM traffic cop ask me to pull over. I parked my bike with full fear as i come to know i am in big trouble, my licence and emission test paper was in my wallet. Anyway i tried to ask cop that i forgotten my wallet and these docs are in there he asked me to show the bike papers i shown those to cop and bingo !!!! my insurance for bike is expired i have to pay for this blunder now. anyway cop told me the sum of 1000 Rs for all these missing papers so only one thing can save my money now is go home and get as much as papers i can. so they shown me way towards auto. Autowaala looked at me like dog look at meat. as closer as i was coming to him he gettig that much happy. i didn't realized why but i sensed this. when the distance was 20 Feet he started his auto and run aways bloody hell why it's happening ????
Anyway i recovered from this shock sooner then i expect and started chasing other autos but see the glory of the day i found 100s of full auto none of vacant auto. after walking 2 KM and looking for auto i reached to the dairy circle here one autowaala was resting i asked him to come with me he asked for 200 Rs bucks only for 10 KM ride. i was shocked again. and i decided to move ahead without auto. after walking one more kilometer i was in fornt on a IT comp. there i found one auto who was vacant and i just run for 10 meter to get him as he just started his auto when i ask him to stop. i asked will he give me ride we asked "vehicle hai wahan par" i said yes. so he said no i will not take you there i asked why he told no waiting.. then i understood he was asking "waiting hai wahan par". i told him i just have get my wallet and have to come back. he agreed and finally after tedious 3 KM with paining leg [history of paining leg is we played cricket in recent weekend and we did this after an year]. finally i reached at home after this much happening. i found my wallet is missing i searched for full home but no luck. then i called my Jr. cum room partner who might have know where my wallet is ? he told last night you carried your wallet to phone shop. i recalled i might have putten that in my jeans i was sporting last night. i checked and bingoo !!! again i got my wallet after 5 min of search. now i collected all papers and came out of home. autowaala was waiting luckily but he scolded me for holding him on wait. After this all he took my back and in middle of way to traffic cop. few guys just jumped infront of auto they pulled out the key of auto and started scolding my autowaala and me in Kannada [native language of karnataka] i was surprized why it's happening but innerself i was aware it's the glory and beauty of the day. sooner i come to know there is auto strike in bangalore today. and my autowaala somehow managed to escape from there. he dropped me at traffic cop place and asked me for some ammount which was greater than fare but not very much so i paid that and move towards cops. And glory flashed again till the moment i reach there all the cops were moved from there only my bike with few other bikes were parked there but no one was there. i was having no clue where to go where to get key for my bike. after some 10 minute one traffic cop was walking towards me i got some light in my eyes. i asked him to raise a ticket for no isurance but he asked if i want to manage without ticket but i insisted on paying fine instead of bribe. after paying fine i was not happy but was in pride was feeling stupid but feeling no guilt. after this all i guess day has taken it's toll. so might be nothing will happen in rest of the day..
i have already reached to the office. small things are happening like already compiled code is throwing error but nothing major so i hope glory of the day is ended now.
Wow what was the day.. i have many days like this but from last week onward i will write down these here.. for remembrance