more debugs

Wednesday, December 14, 2011

Dynamic Menu In ASP.NET Using MSSQL, and XML

Creating a dynamic menu which gets the menu items and sub menu item during the execution time of the asp.net project requires to construct a table say Menu where we insert the information of the menu items to be rendered in the asp page.


Design Table for Dynamic Menu
Design Table for Dynamic Menu


First drag a menu control from the toolbox of the Visual Studio to the form where you want to see your dynamic menu. Then Create a table having a MenuId as primary key, MenuName where we insert the name of the menu to be displayed in the view page,UnderMenu where we insert sub menu which are to be shown on mouse hover. For example, on the mouse hover of Tutorials Main Menu we need to show different topics such as HTML,Ruby On Rails, C#, ASP.NET, JAVA, MSSQL etc. OR any thing else according to your choice.So now insert the data in the above table according to requirement.

Dynamic Menu
Insert Data Into Table


similarly go on adding menus in parent child relationship.




Here you can see that the parent menu are given under menu id '0' and its child are given its parents menu id as under menu id. Here you can see that MenuId of Company is 2, and its under menu is 0 which indicates that menu Company is parent menu. Then the other two menu Board of Directors and Organization Structure are filled with UnderMenu of 2, Which says that these two menu are child of Company menu.


Now Its time to do some coding to create xml consisting dynamically of all the values in the table given above. The PostUrl field in the above table give the url to follow on click of the menu such the you dont need to hard code Response.Redirect manually to redirect a click to different url.
So now the C# code goes here, Its a bit complicated in viewing but try to understand it.

XmlDataSource xd = new XmlDataSource();
string xmlstr;
xmlstr = "";
xmlstr += "";


Here you will have to make some connections to your database table, open it and call it con.

string sql = "select * from Menu where MenuId order by MenuId";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
string menuName = reader["MenuName"].ToString();
string MenuId = reader["MenuId"].ToString();
string underMenu = reader["UnderMenu"].ToString();

Checks if it is the parent menu. If its parent menu goes to find own child menu in the table

if (underMenu == "0")
{
xmlstr += " ";
string innerSql = "select * from Menu;

make another connection to your database and call it innercon
SqlCommand innerCmd = new SqlCommand(innerSql, innercon);
SqlDataReader innerReader = innerCmd.ExecuteReader();
while (innerReader.Read())
{
string innerMenuName = innerReader["MenuName"].ToString();
string postUrl = innerReader["PostUrl"].ToString();
}
innerReader.Close();
innercon.Close();

Dont forget to close inner connections
}
}
xmlstr += "";
xd.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
xd.CacheDuration = 1;
xd.EnableCaching = false;
xd.Data = xmlstr;

MainMenu is the ID of the menu dragged from toolbox to the form in aspx. You can get or change your ID easily from the view.

MainMenu.DataSource = xd;
MainMenu.DataBind();
}
}


Now the only thing left is the DataBinding portion of the program. But if you like to have some designs in your menu you can add which will not affect the entire process how the menu is shown.In aspx page add the databinding functionality.

<asp:Menu ID="MainMenu" Orientation="Horizontal" BackColor="#000000">

<DataBindings> <asp:MenuItemBinding DataMember="TopMenu" TextField="text" ValueField="text" SelectableField="isSelectable" ToolTipField="toolTipMsg" EnabledField="isEnabled" /> <asp:MenuItemBinding DataMember="Menu" TextField="text" ValueField="text" NavigateUrlField="url" SelectableField="isSelectable" ToolTipField="toolTipMsg"/> <asp:MenuItemBinding DataMember="SubMenu" TextField="text" ValueField="text" NavigateUrlField="url" SelectableField="isSelectable" /> </DataBindings> </MainMenu>



The above mentioned MainMenu is ID of the menu dragged and dropped into the aspx page
default color taken by the menu may have been white so try to add colors to the menu styles.Menu Style will be covered later on this blog.



Monday, December 12, 2011

Difference between Store Procedures and Triggers


We can call procedures explicitly but trigger is automatically invoked when the action defined in trigger is performed. Store procedure can't be inactive but triggers can be inactive.


With this short introduction of store procedure and trigger I want to give idea of creating store procedures in any phpmyadmin . It's all same as creating store procedure in MSSQL but the little different part is the delimiter determination. So we have to set delimiter from ';' to '//' which is only one tricky part of store procedure in MySQL.



Sunday, December 11, 2011

Model View Controller Framework

Model View Controller framework(MVC) is a software architecture. The Pattern isolates "domain logic" (the application logic for the user) from the user interface(input and presentation), permitting independent development, testing and maintenance of each(separation of concerns).


MODEL is not necessarily merely a database; the model in MVC is both data and the business/domain.
The model manages the behaviour and data of the application domain, responds to request for information about its state (usually from view) and responds to instruction to change state(usually from the controller).

The CONTROLLER receiver the input and initiates a response by making calls on model objects. A controller accepts input from the user and view part to perform actions based on that input.

The VIEW renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exists for a single model for different purpose. A view part typically has a one to one correspondence with a display surface and known to render to it.

Wednesday, December 7, 2011

Some Definations

What is Serialization?

- The process of  writing the state of an object to a byte system is called serialization, also, it is used to save the program to persistent storage area, like a file and easily restore using deserialization process.

 Why do we need finalize() method when Garbage Collection is there?

-Some time an object (which is lost there object reference) is holding some JAVA resources like file handle, Character font, need to free from the resource before that object is collected by JVM or say garbage collection.

Why finally methods in exceptions?
- The finally method will be executed after a try or catch execution, and mainly used to free up resources. Any code that must be execute before a method returns is put in a finally block.

                               try {
                                     // block of code to monitor for errors
                                    }
                              catch(Exception exObject)
                                             {
                                              // exception handler for Exception Type1
                                              }
                                finally 
                                           {
                                              // block of code to be executed before method returns
                                         }

Method Overloading ?
   -   Operator Overloading is a specific case of polymorphism in which some or all of opertors like    +,=,or == have different implementations depending on the types of their arguments.

        Function Overloading or method overloading allows to creation of several methods with the same name which differ from each other in terms of the type of input and the type of the output of the function(not available in JAVA).

Method overloading is usually associated with statically-typed programming languages which enforce type checking in functions call.


Method Overriding ?
   - In overriding, a method in a super class is overridden the subclass. The method in the subclass will have the same signature as that of the super class, Since the method in the subclass has the same signature and name as the method of its superclass, it is termed as overriding.
                         


Monday, December 5, 2011

Telnet - SMTP Commands (sending mail using telnet)

Telnet - SMTP Commands (sending mail using telnet)

In order to access your mailbox you will need three things.

1. An active internet connection.

2. The address of a mail server capable of relaying for you - usually
provided by your internet provider.(e.g. mail.domain.ext)

3. A valid email address(e.g. mail@domain.ext)

After verification of the above mentioned three points go to following steps.

1. The first thing is to do is open a connection from your computer
to your mail server.

telnet mail.domain.ext 25

you should receive a reply like:

Trying ???.???.???.???...
Connected to mail.domain.ext.
Escape character is '^]'.
220 mail.domain.ext ESMTP Sendmail ?version-number?; ?date+time+gmtoffset?

2. You will then need to delcare where you are sending the email from:

HELO

- dont worry too much about your local domain name although you
really should use your exact fully qualified domain name as seen by
the outside world the mail server has no choice but to take your
word for it as of RFC822-RFC1123.
This should give you:

250 mail.domain.ext Hello [loc.al.i.p], pleased
to meet you

3. Now give your email address:

(On many mailservers the space after the : is
required rather that optional.)

MAIL FROM: mail@domain.ext
Should yeild:
250 2.1.0 mail@domain.ext... Sender ok

4. If it doesn't please see possible problems.

5. Now give the recipients address:

RCPT TO: mail@otherdomain.ext
Should yeild:
250 2.1.0 mail@otherdomain.ext... Recipient ok
6. If it doesn't please see possible problems. Note Google mail
servers require < and > around the email address.

7. To start composing the message issue the command DATA

8. If you want a subject for your email type Subject:-type subject
here- then press enter twice (these are needed to conform to RFC 882)

9. You may now proceed to type the body of your message (e.g. hello
mail@otherdomain.ext from mail@domain.ext)

10. To tell the mail server that you have completed the message enter
a single "." on a line on it's own.

The mail server should reply with: 250 2.0.0 ???????? Message
accepted for delivery

11. You can close the connection by issuing the QUIT command.

The mailserver should reply with something like:221 2.0.0
mail.domain.ext closing connection
Connection closed by foreign host.