Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Sunday, November 1, 2009

Inserting HTML tags

The problem with writing HTML, ASP etc. codes in your page is that the browser considers anything starting with an angle bracket as HTML code, it will try to make something of the code and hence your page will not be the way you intended it to be.

Let's say you are writing a tutorial for HTML and you want to convey to your readers how to make certain words appear bold in html. You'll naturally use the tag for bold... you'll try to use the tag, write some text within the opening and closing tags and try to show your users how the tag is used... instead, what you'll get will be the text appearing as bold and the tags used to make words appear bold absent.. this is because the browser considered the bold tag to be what it actually is.. the bold tag, it would think that you intended the text to appear in bold.. how is it supposed to know you are writing a tutorial anyways... (on second thought, if you are good enough to be writing HTML tutorials.. you would know this already)

What makes a tag, a tag are the surrounding angle brackets.. if you omit them anything you write won't be considered HTML or ASP tags.. the trick is to use the codes for the angle brackets...
The codes are as follows









<&lt;
>&gt;


So for the above example, you'll write something like..

<b>This is bold text.</b>

Refer to the following link for more codes..
http://codex.wordpress.org/Writing_Code_in_Your_Posts

Sunday, October 26, 2008

Programming in SQL Server 2005

A lot of programming can be done within Stored Procedures. Also when you are writing scripts to accomplish a complex task, it can be done more easily with programming. You can easily loose your way writing sub-queries, and you can end up compromising the actual task with something close to it but not really it.


There are a lot of things a person needs to learn in order to get the needed results and fast. And for that you need to have a little more knowledge then SELECT.

I haven't been able to find any good tutorials that would take me from the basics of programming in SQL to higher levels. There are a bulk of resources for Oracle but none that I could find for SQL. Yeah, maybe I lack adequate searching skills but whatever the reason, I have been restrained to use Oracle PL-SQL guide for learning SQL.
Now what I am doing is that I read something from Oracle Guide and then I try it out in SQL Server 2005 and when it doesn't work I search for that particular topic or syntax equivalent in SQL.

And what I am going to do here at my blog is that I'll write small posts giving the syntax for programming in SQL (something beyond the basic Transaction-SQL statements).

I don't have much to offer, but well.......

Anyone who can give me a reason for why I didn't get any decent search results is always welcome to explain it to me through comment.


Declaring Variables:

Syntax
:

DECLARE @variable_name data_type


http://msdn.microsoft.com/en-us/library/ms188927(SQL.90).aspx



Declaring Cursors:
I don't know how to exactly define a cursor. (After all I am no DB Programming Guru, just in my learning phase.)

But cursors can be used when you want to select a number of rows and then iterate through each row one at a time, or manipulate them in any way.

Here's the
Sytanx:

DECLARE cursor_name CURSOR FOR sql_stmt


Example:
DECLARE my_cur CURSOR FOR SELECT * FROM Table_1


You don't have to start the cursor name with '@'.

http://msdn.microsoft.com/en-us/library/ms180169(SQL.90).aspx

In order to execute the statement and populate cursor OPEN statement is used.

OPEN:

Syntax:
OPEN cursor_name

http://msdn.microsoft.com/en-us/library/ms190500(SQL.90).aspx

After the rows have been retrieved and stored in cursor, they can be worked on by using the FETCH statement.

FETCH:

Syntax:
FETCH position FROM cursor_name

where
position can be any of the following:
NEXT
PRIOR
FIRST
LAST

http://msdn.microsoft.com/en-us/library/ms180152(SQL.90).aspx


Note:
I know you may not find this information very useful. It's all just basic information, and my resource is too the internet, where anyone could get it easily. But as I said I too am in learning stages. I'll try to update my blog with the new things I learn.
And these are just an overview of possibilities. You get to know what things you can do, and then you can refer to the links I have provided to learn more about them.
This post is not in any way a tutorial, just a baby-tutorial, to provide you with guidelines.

References:
I am using the following book for Oracle Sytax:
PL/SQL
User’s Guide and Reference
Release 2 (9.2)
March 2002
Part No. A96624-01


The following links were referred to for SQL Syntax (Some of these have been stated above as well)






Wednesday, September 10, 2008

The Output Parameters

Sometimes when you write a function, you may want it to manipulate and return several values rather than one. Whenever I needed to do this I used to return an object.
But there might be a possibility that the returned object has no other purpose than to be used just to return multiple values from a function.
And then I discovered output parameters. Though I haven't really used them. (Just used them to check the functionality, nothing flashy. :) )

With output parameters you define the variables that will be returned. But you don't use the return keyword. With return keyword you can return a single value or object not multiple.
But with output parameters you don't use the return keyword to return multiple values rather you just specify that these variables are output parameters and their values can be used by the calling program and the called program can manipulate their values (in fact, assign them values).
For the above stated purposes you use the keyword out.

Example Code:
Say, you have a class "Class1.cs" (a very innovative name, right?)
In this class you have a function called ProcessNum which takes two integers as input and as output gives their sum and difference. This is how it will work:

public void ProcessNum(int a, int b, out int Sum, out int Diff)
{
Sum = a + b;
Diff = a - b;
}

Calling ProcessNum:

Class1 obj = new Class1();
int S;
int D;
int num1 = 5;
int num2 = 5;
obj.ProcessNum(num1, num2, out S, out D);
Console.WriteLine("Called ProcessNum of Class1 with parameters " +
"a: " + num1 + Environment.NewLine + " b: " + num2 +
Environment.NewLine + "Returned Values:::: " +
Environment.NewLine + "Sum: " + S + Environment.NewLine +
"Diff: " + D);


Source:

Apress Beginning ASP.Net 2.0 in C# 2005
Chapter # 3: Types, Objects and Namespaces
Pg. #: 75

Tuesday, February 5, 2008

Word Net

So here's a somewhat technical post.
See how hard I am trying to write a technical blog?

So, we (a group of 4 people) are currently working on a project, will tell you the details in another post.
During this project, we faced a problem of identifying whether a word falls under the noun category or not. We found the answer immediately. Word Net.

Word Net is basically a database which has classified words into the parts of speech it falls under. There are many other things that Word Net can help you with. Getting Hypernym, for instance.

You can find the Word Net application on it's website:
http://wordnet.princeton.edu/

We downloaded the application from this site, it also gives you the code of word net (after all it's open source)

But the problem was, using word net in our code.
We are working in C#. Word Net is a C++ project.
We tried to call a C++ code from C#.

For this we first tried to get a hang of dll. We made a sample dll in C++ code. Called it through C++ code. Worked perfectly. Yahooooo!!!!
But then we tried to call the same C++ dll through C# code.
Unsuccessful.
Tried hard to get it right.
Surfed through blogs and articles.
Posted questions on the forum.
Got answers too.
But nothing worked.
Yeah, I know we are dumb. :(


But then while trying to get our hands on the Word Net Database. We found a site:
http://sourceforge.net/project/showfiles.php?group_id=135112
which provides the Word Net database in mySQL.

We downloaded it. and yes we thought that we can use it.

Unfortunately we are working on SQL. Not a big problem. We could just as easily have used mySQL. not much difference.
But we always try to do the hardest, most dumbest and senseless thing on earth.
And we continued with the tradition here too.
We decided to convert the database in SQL.


And yeah I did think about downloading a software that could convert mySQL to SQL.
But was only able to find softwares that could convert mySQL database into SQL database. Found none that could do the same with query files.

Then I tried to make a mySQL database with the query files.
again faced some problems.

Now when you download the zip file from the above mentioned site, it provides you with the query files to create and populate database.
On the website the schema is also given.
Using the schema, we created the database on Microsoft SQL Server 2005.
And then we used the query files provided to populate the database. But ofcourse they were in mySQL, so we created a small C# code which reads from .txt file the queries in mySQL, does the necessary manipulation on them and then writes the SQL Queries in a .txt file.

Seems easy enough na?
Well it was easy, but very time consuming. Or maybe I am dumb and couldn't find a less time consuming way.

What I did was I used to open the query file in Firefox. (Wasn't able to open it with Microsoft SQL Server, Internet Explorer or Notepad).
I wanted to populate one table at a time. So I went through the page and found the queries for the table I wanted to populate. Then I would copy paste one line on a note pad. That one line would actually comprise a large number of queries. Then I saved the .txt file and converted into a .txt file with SQL queries through the following C# code:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConvertMySQLtoSQL
{
class Program
{
static void Main(string[] args)
{
String Path = "Osense.txt";
String TbName = "sense";
StreamReader Reader = new StreamReader(Path);
String Content = Reader.ReadToEnd();
Content = Content.Replace("),", ");\n");


String[] Lines = Content.Split("\n".ToCharArray());
for (int i = 0; i < Lines.Length; i++)
{
Lines[i] = "INSERT INTO " + TbName + " VALUES " + Lines[i];
}


Reader.Close();
String OutputFile = TbName + ".txt";
StreamWriter Writer = new StreamWriter(OutputFile);
for (int i = 0; i < Lines.Length; i++)
Writer.Write(Lines[i] + Environment.NewLine);
Writer.Close();

}
}
}