Friday, October 24, 2008

Upload document in .NET C#

Very simply, there are two parts to this, storing in inside DB and retrieving for display. This program works for C# + MS SQL. Before everything I assume that you created a table inside the database with a field picture with datatype image.

Part I: Storing in Database
a.) setup an input file with the name UploadFile.
<input id="UploadFile" type="file" runat="server">

b.) setup a button with the name btnUpload
<asp:imagebutton id="btnUpload" onclick="btnUpload_Click" runat="server" height="29" width="150" imageurl="images/buttons/upload_long.jpg"></asp:imagebutton>

c.) setup code behide for the button to be
if (Page.IsValid) {
String userId = Session["authenticated_userid"].ToString();
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
DB.userTableAdapter aAdapter = new // you need to set this up in your own DB.xsd

DB.userTableAdapter();
int rowsAffected = aAdapter.UpdatePicture(imgBinaryData, int.Parse(userId));
if (rowsAffected > 0) {

Response.Redirect("home.aspx"); // go back to same page
}
}


Part II: Retrieving stored image from DB
1.) Create a file call getImage.aspx and inside Page Load

protected void Page_Load(object sender, EventArgs e) {
String userId = Session["authenticated_userId"].ToString(); // get your own identified
DB.userTableAdapter aAdapter = new DB.userTableAdapter();
byte [] temp = aAdapter.GetPicture(int.Parse(userId));

if (temp == null) // in case we find nothing in db {
Stream s = File.OpenRead(Server.MapPath("images\\portrait.jpg"));
temp = new byte[s.Length];
s.Read(temp, 0, (int)s.Length);
Response.BinaryWrite(temp);
s.Dispose();
s.Close();
}
Response.BinaryWrite(temp);
}


2.) Wherever you want to show you image, just call the image as per normal
i.e. <img src="http://www.blogger.com/getImage.aspx" />

Done! Enjoy.

ASP.NET (C#) unable to ENTER to submit form

The best way to solve this is to do the following

1. Create a login function. i.e.
protected void login() {
// get database connection
// get variables from form
// check if correct, set session variable, redirect
// else, display error, return to form
}

2. In page load, use the following code:
If (Page.IsPostBack()) {
login();
}

Viola! Weird bug in ASP.NET i read. But yah, Page.IsPostBack fixes it all.

G1 Phone is out, but it will not take off.

I honestly doubt this phone will scale. The reason why I think this would not scale really is because the mobile phone market is already satuarated with many small players and a few dominating ones. And we learnt clearly from strategy classes, that in a market like this, the incumbent will catch up and kill the market entrants. But then again who can kill Google?

But note, the fact that the phone (as a hardware) won't scale like that of Iphone, does not mean that the software(google apps etc) won't.

I believe Google, with their involvement in the Android opensource framework for mobile applications really show that they are in to revolutionize the way we use our phone, not so much to revolutionize phone. Of course, this might not seem feasible at a glance, but let's take a step back and see what happens if every phone is installed with Google Apps?

1. Their search hits will increased greatly, with the increased saturation of the online search market, this is their best way out to increase their market capture share - think about it, not everyone has a computer but most of us have a phone and that phones follows us everywhere. So far, not many search engines built applications that are meant for phones, and google i believe will continue to be the market leader in mobile and desktop searches for many years to come.
2. Increased advertisement sources. Instead of just being able to advertise for companies with an online presence (i.e. a website), Google will now be able to get business from small companies without any form of online presence. This is made possible by their ShopSavvy application (google app available from their g1 phone). There is now more reasons for these offline companies to advertise on Google.
3. Increased advertisement income. Google did an excellent job in increasing advertisement tagetting, in fact its so damn good, I click on the advertisements all the time. By being in the mobile phone industry, the software will be able to track where we are and feed to us advertisements that are more highly targeted. By being location-aware, the advertisements are getting more targeted and as a result, advertisers are definitely willing to pay a higher premium.

I believe the G1 phone will not really take off and capture a huge portion of the market. But one thing for sure, the softwares that google came up with, specifically google search, mail, maps and shopsavvy will revolutionize how we do our shopping and eventually our way of life.

I recalled from my economics class, that the professor once said. Economists like to think of the world in extreme scenarios that is, the market is either in a perfect or imperfect condition. In my humble opinion, with googleapps, information is getting perfect, we are not quite there yet, but we will one day.

I look forward to the day in my life that all barcodes are uniformlly standardized across the world. All product's barcodes uses electronic ink, compatible with RFID. All shops have a Standard Centralize Point of Sale System. If such a world exists, information of goods and services could then be truly perfect.

We are getting there, and googleapps - not the gphone. Will bring us there.

Monday, October 20, 2008

Web Hosting Suggestion

Just found a great web hosting company, here it is.

Saturday, October 11, 2008

Make borders around pictures

Instead of having to thumbnail pictures manually and putting a niceframe using Photoshop or Fireworks, the easiest way is actually CSS. Here it is:

<style type="text/css">
.float img {

background: #fff;
border: solid 1px #ccc;
padding: 4px;
}

</style>

Here is a very good tutorial on creating frames:
http://www.csstemplatesweb.com/2008/05/08/web-20-design-generators/

Pictures with margins

If you are looking at how to place pictures side by side text without the usage of tables in HTML, the usage of CSS is the way to go. The code is extremely simple as well. Here it is:

<style type="text/css">
.float img {
float:left;
margin-bottom:10px;
margin-right:10px;
}
</style>

Done! The key property here is float, with it specified your picture will float to the left and your text will be on the right of this text with a margin of 10 pixels bottom and right to the picture. Natutally if you need it the reverse, you have to change float to right and the margin should be specified for left instead.

Multiline variable for javascript

Setting single line variable is simple for javascript, but not multi line. In fact, its not possible to do it unless with some additional escape characters input into the line.

Take for example
var temp = "This is Aaron"; //this works perfectly
var temp = "This\
is\
Aaron"; // this works with the /escape character

var temp = "This
is
Aaron"; // this does not work.


Now in order to solve it, I used PHP, after a bit of experiment, there are two approaches. I made used of (include + regex) and (file_get_contents + regex). Let's start with include + regex first.

var temp = "<?php $string = include('synopsis.php'); echo preg_replace("#(\r\n\n\r)#s", ' ', $string); ?>";

Inside the file: synopsis.php is a content like this:

$var = "This is Aaron";
return $var;
?>

This works perfectly fine, but its very cumbersome because you need to assign the variables and all.

I therefore came up with file_get_contents + regex. Inside the file:
var temp = "<?php $string = file_get_contents("synopsis.php"); echo preg_replace("#(\r\n\n\r)#s", ' ', $string); ?>";
Synopsis.php can contain any normal HTML, no need for customization like return.

This works great for me, what I was trying to do was to dynamically update the innerHTML variable of a DIV tag using javascript. The thing was that the content I wanted to update the DIV is multi line and with that javascript cannot work unless I manually edited each HTML page's source code to a single. This is definitely not very productive, for every change the client may want to make, i will have to edit it on Dreamweaver and go through line by line on notepad to make them a single line.

Hopefully this may help someone :)

Saturday, October 4, 2008

VMPlayer Remote Desktop Display

While doing research, I was looking through various methods of performing thin client mode on computers. Mainly there was the VNC and X11. Well X11 would have offered the simplest solution to running the application across a server.

But they are essentially and some would argue that X11 is really not a thin client framework. (depending on how you define thin client). But anyhow, they are really different in the sense that VNC displays by pixel frame buffer refresh, it usually hosts a whole desktop (usually including a window manager) and reconnection to a lost connection would be simple as compared to X11.

Well, therefore I didn't had much choice and need to run VMPlayer using the VNC Protocol.
With the protocol settled, now I had to solve how to run multiple VMPlayer session on a computer and have it served simultaneously to different computers that connects to it. On top of that, computer 2 has to serve the client across the network to various clients remotely.

First of all, in computer 2(the server)
I have VMPlayer running in Linux, I added, the following lines to the VMX file, RemoteDesktop.add = "Enabled"
RemoteDesktop.port = "5901".

Then in computer 1(the client)
I have a VNC Player which will try to connect to port 5901 on Computer 2. Any normal VNC Player can run, e.g. UltraVNC or gtk-vnc (there is an example player for linux)

But VMPlayer does not render when we try to run it via a client. How do we actually run instance of VMPlayer remotely? I believe the answer lye in disabling the output of VMPlayer locally on the server. More research on this to come...

Wednesday, October 1, 2008

Looking for files inside Linux

A command that i somehow cannot remember, dead easy but oh well:

$> find / -name *isr