Saturday, December 13, 2008

Remove selected items from listview in C#

I killed some time figuring this out myself though the solution was there on the first link given out by google..

thanx to the guy who posted this one..

list is a listview here...

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

vaibhav

Monday, December 8, 2008

How to find file system disk space in linux

My linux VM started showing me that it ran outof space... So I tried to figure out what is the space left over... so I found this on the man page of df command...

df - report file system disk space usage

Show information about the file system on which each FILE resides, or all file systems by default.

Mandatory arguments to long options are mandatory for short options too.

-a, --all
include dummy file systems

-B, --block-size=SIZE use SIZE-byte blocks

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)

-H, --si
likewise, but use powers of 1000 not 1024

-i, --inodes
list inode information instead of block usage

-k like --block-size=1K

-l, --local
limit listing to local file systems

--no-sync
do not invoke sync before getting usage info (default)

-P, --portability
use the POSIX output format

--sync invoke sync before getting usage info

-t, --type=TYPE
limit listing to file systems of type TYPE

-T, --print-type
print file system type

-x, --exclude-type=TYPE
limit listing to file systems not of type TYPE

-v (ignored)

--help display this help and exit

--version
output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following: kB 1000, K 1024, MB
1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.


n thats what my VM gave me :)

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 3945128 3945112 0 100% /
none 385124 0 385124 0% /dev/shm

//14.4.2.55/CodePart
117218300 58824100 58394200 51% /mnt/myshare


vaibhav

Friday, October 3, 2008

export not working in shell script - solution

Another small problem and another nice thing learnt :o)

I was trying to make a shell script that exports all my environment variables as I was tired exporting them manually each time I connected to my lab machine from my computer. I wrote the script and tried running. to my surprise it was printing the echo statements but not exporting the variable. After searching for 5 minutes on the web I found that when we try exporting some variables it runs in a private shell. This make the changes not visible in the current shell.

I found this on a linux forum and found it interesting:
The program that you use to access the shell is called a terminal emulator. Examples are Konsole, Gnome Terminal, xterm, Eterm, aterm, etc. When you open one of these, the program also creates a subshell. The shell that you enter commands into is different from the one running your X server, though it has inherited all the attributes of the main shell.

When you run a script in a shell, that script runs in a subshell that works very similarly. A script runs in its own environment, and does not affect the parent at all. When the "source" command is used, changes to the subshell are duplicated in the parent shell


http://www.linuxforums.org/forum/linux-newbie/55168-export-doesnt-work-shell-script.html
so the solution:
simple ... run your shell script with the following command
test_script.sh=>

#!/bin/sh
export MY_TRACE_FILE=/root/mytrace.trc;

and then do a

. ./test_script.sh

or

source ./test_script.sh


I tried the second one and it worked just fine :)

Wednesday, October 1, 2008

Code for Random Number Generator

This is something I always thought of doing but it was always a priority 2 task for me. Today when I was in a fix of keying in make and make install each time I had to run my test I finally replaced the libuuid patch with a simple 3 line code of this random number generator function.

int temp_id=0;

void myRandomNumber()
{
unsigned int myseed;
myseed = (unsigned)time(NULL);
srand(myseed); // Initialize the random number generator
temp_id = rand(); // Random number
}



Reference:
http://www.randombots.com/random_control.htm


Vaibhav

Tuesday, August 26, 2008

How to unzip a RPM file and a bz2 file on unix/linux

You may use
1. Unzip RPM file
$ rpm2cpio myrpmfile.rpm | cpio -idmv

for detailed explaination look at
http://www.cyberciti.biz/tips/how-to-extract-an-rpm-package-without-installing-it.html

2. for a bz2 file

$ bunzip2 filename.txt.bz2

To compress a file using bzip2, execute the following command:

$ bzip2 filename.txt

for more details you may visit
http://www.debianadmin.com/create-and-extract-bz2-and-gz-files.html

cheers
Vaibhav

Zipping and Unzipping Files in UNIX

taken from http://www.hostingmanual.net/other/zip.shtml

There are several methods of archiving files and retrieving archives. We recommend using the "zip" function to compress your files for its ease of use and portability. (Files zipped in Unix can be extracted using WinZip or PKunzip in Windows). We have provided various "unzip" methods. The "right" unzip method depends upon the method used to zip the file. You can tell the zip method by the file extension (e.g., .zip, .tar, .gz, etc.)


Zipping Files Using ZIP

This Unix program is compatible with the zip program for DOS and Windows. To zip files, first have the files uploaded to your server, then log into your account with Telnet. Navigate to the directory where the files are that you want to zip (for instance by typing cd www then cd sounds to move to your /www/sounds directory). Then type:

zip myzip file1 file2 file3

This puts the files named file1, file2, and file3 into a new zip archive called myzip.zip.


Unzipping Files

Please note that the unzip method you use is defined by the filename you are trying to unzip. For example, if you are trying to unzip a file called file.tar - you would use the method described in "tar". Files ending in .gzip or .gz need to be extracted with the method described in "gunzip".


Zip

If you have an archive named myzip.zip and want to get back the files, you would type:

unzip myzip.zip

Typing zip or unzip by itself will give you a usage summary, showing nearly all the options available.


Tar

To extract a file compressed with tar (e.g., filename.tar), type the following command from your telnet prompt:

tar xvf filename.tar

Basically, this command means that you will see the file "explode", so don't worry when you see your screen scrolling wildly. It also means that you will see any errors in the archive.


Gunzip

To extract a file compressed with gunzip, type the following:

gunzip filename_tar.gz

then if you receive no errors, type:

tar xvf filename_tar

Monday, August 18, 2008

adding a persistent route to windows server 2003

You may use the following command on your cmd prompt to add a persistent route to windows server 2003.

C:\Route add -p network [network address] mask [subnet mask] [gateway] metric [value]

for help on this command type
C:\ route ADD

to print all added routes type in

C:\route print

hope it helps.

vaibhav chugh

Sunday, July 20, 2008

RadioButtons in gridview. Select one at a time

Hi,

This is another problem I just solved. I found all possible stuff on the internet and no one had solved it in an easier fashion. Though people might call it JUGAD Tech but I must say what I have made just worked for me.

The problem with using radio buttons in a gridview is that even if u use the groupname property of the radio button it will select multiple radio buttons.

So what i did is
1. To the column tag of the gridview add the following code



2. make this function in your .CS file
protected void uncheckOthers(object sender, EventArgs e)
{

int count = GridView1.Rows.Count;
for (int i = 0; i < count; i++)
{
RadioButton rb = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("selectItem");
rb.Checked = false;
}
RadioButton test=(RadioButton)sender;
test.Checked = true;

}

Isn't it easy..


Cheers!!!
Vaibhav

Wednesday, July 16, 2008

Fetching data into a variable from SqlDataSource in Asp.NET with C#

After my struggle of 3 hours I finally got it working. I thought it would be easy but MSDN was of no help at all and the web is full of all the stupid stuff but not the correct information. People just put in the concept and nobody even bothers to write down the steps to fetch the data after the execution of query.

Here is the code I made with the help of a post on some discussion forum. The title of the topic i read was "Getting Variable value out of a SqlDataSource in asp 2.0".

Following is the automatically generated markup when you configure the sqldatasource from the data tab.



Then to you onclick function just add the following lines:

protected bool CustomerAuthenticate(string username, string password)
{

DataView dv = ((System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)));

TextBox1.Text=dv.Table.Rows[0]["password"].ToString();


//check and return
}


I hope it will help

Saturday, July 12, 2008

Master Pages and Themes in .NET

following are a few nice links for master pages and themes that can be used in .NET. I will be using them for my final project for my Internet Programming course.

http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
http://www.simple-talk.com/dotnet/asp.net/beginning-asp.net-2.0/
http://msdn.microsoft.com/en-us/magazine/cc163967.aspx
http://www.odetocode.com/articles/450.aspx
http://www.west-wind.com/WebLog/posts/4899.aspx

Special thanks to Dr. Fawcett.


Vaibhav

Wednesday, June 11, 2008

mounting and unmounting pendrive

This is for Damani Sir,

To mount a pendrive on ur freeBSD running on VMWARE SERVER or WORKSTATION. just plug in ur pendrive when vmware screen is in focus. It will show you some device information.
In case you dont see the device information coming up. go to
VM tab on the top of your screen > choose Detachable devices > and from the list choose the pendrive.
After doing the above
write
#cd /mnt
#mkdir pendrive
#mount -t msdos /dev/da0s1/ /mnt/pendrive/

and your pendrive is ready to use,

to unmount

#cd /mnt/pendrive
#sync
#cd ..
#umount /mnt/pendrive/


and u r all set to go...

ciao
vaibhav

Saturday, April 26, 2008

A simple C program for writing to a file

Source:http://www.mycplus.com/cplus.asp?CID=8

#include "stdio.h"
main( )
{
FILE *fp;
char stuff[25];
int index;
fp = fopen("TENLINES.TXT","w"); /* open for writing */
strcpy(stuff,"This is an example line.");
for (index = 1; index <= 10; index++)
fprintf(fp,"%s Line number %d\n", stuff, index);
fclose(fp); /* close the file before ending program */
}

Wednesday, April 9, 2008

B+ Tree Tutorial links.

For my DBMS course I studied the B+ tree implementation. I came across a few nice links.. I enjoyed reading the stuff and things were made pretty clear by the following links...This might be of some help to you as well..

http://www.scribd.com/doc/18211/B-TREE-TUTORIAL-PPT

http://www.seanster.com/BplusTree/BplusTree.html

http://www.ceng.metu.edu.tr/~karagoz/ceng302/302-B+tree-ind-hash.pdf



Take Care
Vaibhav

Monday, March 10, 2008

Process Memory Layout

Just came across a nice link that describes the memory layout of a running process.
It explains & covers

-stack layout
-heap layout
-Executable code
-static data
-Virtual Address Space Of A Linux Process with an example


http://dsrg.mff.cuni.cz/~ceres/sch/osy/text/ch03s02s01.php

Thanks to Christian Sarmoria for the link..

Sunday, March 2, 2008

Difference between MUTEX and SEMAPHORE

Source: http://koti.mbnet.fi/niclasw/MutexSemaphore.html

Mutex vs. Semaphore, what is the difference?
The Toilet Example (c) Copyright 2005, Niclas Winquist ;)

Mutex:

Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: "Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."
Ref: Symbian Developer Library

(A mutex is really a semaphore with value 1.)

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)."
Ref: Symbian Developer Library
http://geekswithblogs.net/shahed/archive/2006/06/09/81268.aspx

Wednesday, February 27, 2008

LinkList or linked list --- An easy way to implement it

Linked list or linklist has always been the favorite Data structure of all the interviewers. So here is the easiest way to implement it( I think so..) :o)

Njoy Coding!!!


Linked list or linklist has always been the favorite Data structure of all the interviewers. So here is the easiest way to implement it( I think so..) :o)

Njoy Coding!!!


///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
// //
// Linklist program by vaibhav chugh //
// Platform : windows vista business ed //
// Language : VC++ / std cpp //
// Date : 2/25/2008 //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

#include
using namespace std;

class linklist
{
private:
struct node
{
int data;
node *next;
}*p;
public:
linklist(); //--- 1
int count_nodes(); //--- 2 count the nodes in the link list
void addLinkbegin(int d); //--- 3
void add_at_end(int d); //--- 4
void add_at_pos(int d,int pos); //--- 5
void del_node_num(int pos); //--- 6
void del_node_val(int d); //--- 7
void showLinks(); //--- 8
void sortLinklist(); //--- 9
void addvalues(); //---10 // Sum all the values in the linklist

};

linklist::linklist() //--------------1
{
cout<<"\n Link said Hi";
p=NULL;
}
/////////////////////////////
// 2 Counting nodes
////////////////////////////
int linklist::count_nodes()
{

node *t;
int cnt=0;
t=p;
if(p==NULL)
{
cnt=0;
}
else
{
while(t != NULL)
{
cnt++;
t=t->next;
}

}
return cnt;
}

//////////////////////////////////
// 3 add link begin
////////////////////////////////
void linklist::addLinkbegin(int d)
{
node *q = new node;

q->data=d;
q->next = p;
p=q;
cout<<"\n I guess I added link at beginning=="<data;
}

///////////////////////////////////
// 4 Add at end
//////////////////////////////////
void linklist::add_at_end(int d)
{
node *q,*t;

if(p==NULL)
{
p = new node;
p->data = d;
p->next=NULL;
}
else
{
t=p;

while(t->next !=NULL)
t=t->next;
q= new node;
q->data = d;
q->next=NULL;
t->next=q;
}
}

/////////////////////////////////////////
// 5 add at position
////////////////////////////////////////
void linklist::add_at_pos(int d, int pos)
{
node *t,*q;
int cnt=1;
int check=0;
if(pos ==1)
{
cout<<"\n calling add begin";
this->addLinkbegin(d);

}
else
{
cout<<"\n Search pos to add";
t=p;
while(cnt < pos-1)
{
t=t->next;
if(t==NULL)
{
cout<<"\nNot many nodes in the linklist";
check=1;
break;
}
cnt++;
}
if(check == 0)
{
q= new node;
q->data=d;
q->next=t->next;
t->next=q;
}
}
}

////////////////////////////////////////
// 6 del node number
///////////////////////////////////////
void linklist::del_node_num(int pos)
{
node *t;
int cnt=1;
t=p;
if(p==NULL)
cout<<"\n no node here";
else
{
while(cnt < pos)
{
t=t->next;
if(t==NULL)
{
cout<<"\n Not many nodes to delete the one u want";
break;
}
cnt++;
}
t->next=t->next->next;
}
}

///////////////////////////////////////
// 7. delete node with value
///////////////////////////////////////
void linklist::del_node_val(int d)
{
node *t;
t=p;
if(p==NULL)
cout<<"\n No node present";
else
{
while(t->next !=NULL)
{
if(t->next->data == d)
{
t->next=t->next->next;
}
else
t=t->next;
}
}
}

////////////////////////////////////
// 8. show link list
///////////////////////////////////
void linklist::showLinks()
{
node *q;
q=p;
while(q != NULL)
{
cout<<"->> "<data;
q=q->next;
}
cout<<"\n M done printing";

}

/////////////////////////////////////
//9. Sort a link list
///////////////////////////////////
void linklist::sortLinklist()
{
int cnt=0,i=0,j=0;
node *current,*nxt,*prev;
current=p;
prev=p;
nxt=p->next;
cnt=this->count_nodes();
for(i=0;i {
for(j=0;j {
if(current->data > nxt->data)
{
current->next = nxt->next;
nxt->next = current;
if(current==p)
{
p=nxt;
prev=nxt;
}
else
{
prev->next=nxt;
prev=nxt;
}
if(nxt != NULL)
nxt=current->next;
}
else
{
prev=current;
current=nxt;
nxt=current->next;
}
}
current=p;
prev=p;
nxt=current->next;
}

cout<<"\nSorted List ";
current=p;
while(current!=NULL)
{
cout<<"->"<data;
current=current->next;
}
}
///////////////////////////////////
//10. Adding values
//////////////////////////////////

void linklist::addvalues()
{
node *temp;
temp = p;
int sum=0;

while(temp != NULL)
{
sum+=p->data;
temp=temp->next;
}
cout<<"Sum = "<
}
////////////////////////////////////
// MAIN
///////////////////////////////////
void main()
{
linklist ll;
ll.addLinkbegin(33);
ll.addLinkbegin(23);
ll.addvalues();
ll.addLinkbegin(13);
ll.addLinkbegin(33);
ll.addLinkbegin(63);
ll.add_at_end(76);
ll.add_at_end(70);
ll.add_at_end(98);
ll.add_at_pos(45,4);
ll.add_at_pos(46,1);
ll.add_at_pos(909,34);
ll.showLinks();
cout<<"\nCount now = "<ll.del_node_num(5);
ll.showLinks();
ll.del_node_val(13);
ll.del_node_val(33);
ll.showLinks();
ll.sortLinklist();
ll.showLinks();
ll.addvalues();
cout<<"\n count now ="<
}