Thursday, December 24, 2009

How to remove all the screens in display stack in Blackberry

In recently i want to delete all the screens in display stack.  I used following code to do it.

 
int screenCount = UiApplication.getUiApplication().getScreenCount();
for (int i = 0; i < screenCount; i++) {
Screen screen = UiApplication.getUiApplication().getActiveScreen();
UiApplication.getUiApplication().popScreen(screen);
}

Monday, December 21, 2009

Simple Socket Client to connect to a server

private void ConnectToServer()
{
using (TcpClient tc = new TcpClient("127.0.0.1", 12))
{
NetworkStream ns = tc.GetStream();
StreamWriter sw = new StreamWriter(ns);
StreamReader sr = new StreamReader(ns);

sw.WriteLine("test message");
sw.Flush();
System.Console.WriteLine(sr.ReadLine());
}
}
This code can be used to connect to a server using a socket and send a simple message to it.

How to listen to TCP socket in C#

The code below illustrate how to listen to Tcp/Ip port for incoming stream.  The code can use to listen any port but some times you may encountered an exception (Refer).  

private void Run_Server()
{
// Listen to our own port 80 (HTTP protocol port).
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener listen = new TcpListener(localAddr, 80);

try
{
// Start listening
listen.Start();
Byte[] bytes = new byte[256];
TcpClient client = listen.AcceptTcpClient();
// This waits until we getting a connection
NetworkStream stream = client.GetStream();

int i;
String data = null;

while (stream.DataAvailable)
{
i = stream.Read(bytes, 0, bytes.Length);
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
txtMessages.Text += data.ToString();
}
client.Close();
}
catch (SocketException exceptionError)
{
MessageBox.Show(exceptionError.Message);
}
finally
{
listen.Stop();
}
}

An attempt was made to access a socket in a way forbidden by its access permissions

// Listen to our own port 80 (HTTP protocol port).
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener listen = new TcpListener(localAddr, 80);
This exception occurred me in Vista because of Port. This exception rise some well-known ports thus using other port (Ex:- 12 , 8008) you may resolve this.

Thursday, December 17, 2009

Ugly Number Calculation Algorithm

package javaapplication1;
/**
** @author sachika
*/
public class Main {

public static void main(String[] args) {

Main mainObj = new Main();
int no = mainObj.getNthUglyNo(11);
System.out.print(no);
try{
System.in.read();
}
catch(Exception s) {}

}

int maxDivide(int a, int b)
{
while (a%b == 0)
a = a/b;
return a;
}

boolean isUgly(int no)
{
no = maxDivide(no, 2);
no = maxDivide(no, 3);
no = maxDivide(no, 5);

return (no == 1)? true : false;
}

int getNthUglyNo(int n)
{
int i = 1;
int count = 1; /* ugly number count */

/*Check for all integers untill ugly count
becomes n*/
while (n > count)
{
i++;
if (isUgly(i))
count++;
}
return i;
}

}

Thursday, December 10, 2009

Multiple Columns in Crystal Report

Some times you might want to print the report Column wise

image

so this is simple. what you have to do is go to Section Expert and –>Details and check the Format with Multiple Columns.

image

then Layout tab will appear . then  configure the relevant values.

image

after pressing ok , you can see your detail section is resized.

image

Now you are done.

The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.

This is a common error when you are binding data to the data grid. you can correct it as giving following code to the  PageIndexChanging event.

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}