Posts

Showing posts from 2013

ASP.NET 4.0 Web forms URL Routing

Hi, while working on a ASP.NET project I saw that URLs of the application were quite large and were not much meaningful to the user. Also they were showing the actual folder structure in the project that I found a bit unsecured. So, here I get the requirement to configure application to accept URL of webpages which are meaningful and short.

To fulfill this requirement I used URL routing feature of ASP.NET. URL routing lets you to configure your application to accept URL which do not point to physical files. Instead of this we can show some semantically meaningful URL to user.

For example -

Initial URL -
http://www.yoursite.com/products.aspx?category=laptop

Using routing you can configure your application render same page using following URL -
http://www.yoursite.com/products/laptop

You can also find that new URL is SEO - friendly and can help in search engine optimization (SEO).


Implementing URL routing in ASP.NET web forms In the implementation part of routing we need to do mapping …

How to add ActiveX control at run time using C#.NET

Hi, recently I worked on a project where the requirement was to add an ActiveX control dynamically to a UserControl type project. Here I was trying to add that activeX control to multiple tab pages of a tab control.

Here is the code that I used -

for(int i = 0; i < 4; i++)
{
      this.LineTabs.TabPages.Add(i);
      AxTree treeadd = treeload(this.LineTabs.TabPages[i]);
}

In the above code I am creating four tab pages at runtime and adding ActiveX control "AxTree" to these pages dynamically using function "treeload".

Here is the code for "treeload" function -

private AxTree treeload(TabPage tab)
    {
        AxTree treeobject = new AxTree();
        ((System.ComponentModel.ISupportInitialize)(treeobject)).BeginInit();
        SuspendLayout();
        tab.Controls.Add(treeobject);
        treeobject.Dock = DockStyle.Fill;
        ResumeLayout();
        ((System.ComponentModel.ISupportInitialize)(treeobject)).EndInit();

        return treeobject;
    }

Dynamically adding controls to web form and their event handling in ASP.NET using C#

Add control dynamically using code on web form Sometimes in web form we need to add controls using code. Following C# code can be used to add button control dynamically to web form.

    Button btn1 = new Button();
    btn1.Text = "New Button";
    this.Controls.Add(btn1);

To add the dynamically created button control to a Panel control use following code -

Panel1.Controls.Add(btn1);
Event Handler for dynamically added control We can use following code to declare click event for dynamically added button code-   btn1.Click += new EventHandler(btnClick);
After declaring the click event, define the event handler using C# - private void btnClick(object sender, EventArgs e)
{
      Button btn = (Button)sender;  //get the button which is                                       clicked
      // write your code
}

So, the complete code becomes -

 {
    Button btn1 = new Button();
    btn1.Text = "New Button";
    this.Controls.Add(btn1);
    btn1.Click += new EventHandler(btnClick);
 }

Paging in MS SQL Server 2005/2008 using ROW_NUMBER()

Sometimes we need to implement paging functionality in our database. We can use ROW_NUMBER() function for this purpose. ROW_NUMBER function is one of the ranking function of SQL Server. Here is an example of ROW_NUMBER -

SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY EmpID) AS Row, * FROM Employee AS tbl ) WHERE Row >= 1 AND Row <= 10
ROW_NUMBER is used for assigning sequential numbers to the records of record-set. In the above query a sequel number is assigned to each record of the Employee table.

Here we have used following two clause -
OVER clause - It is used for specifying the ordering of records before assigning the sequence to each record.ORDER clause -  In order clause we define column or columns which is used to order the records in the record-sets. In our case we have used EmpId with ORDER clause, so first each record of Employee table is first ordered  according to EmpId and then sequence number is assigned to each record. *PARTITION cla…

Debug Java code in Java COM windows application. (Remote Java debugging)

Hi, I recently got the chance to setup Java debug environment for Java-COM based windows application. Here the vb6 code is used for showing the User interface of application while Java and C++ code includes business logic of the application. To fix some bugs in the application a debug environment was required which allows users to debug Java and C++ JNI code.

To debug C++, I used Visual studio attach to process method and for Java code remote Java debugging of eclispe was used.

Here is the details of process that I used to debug Java-

We need to implement following two steps -

Launch the application and tell the JVM that it will be debugged remotelyCreate debug configuration in eclispe IDE to debug application remotely. (in our case it is localhost)
Pass following argument to jvm -
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8998

The above code should be placed at the location where jvm is loaded in the code.
Here 8998 denotes the port and suspend=n will make sure …

.NET Interview questions ( around 2 Years experience)

What are assemblies?What is the process to create shared assembly?Difference between abstract class and interface?What is multilevel inheritance?What are the steps to fetch data from a database table?We have 5 classes created in .NET. As per one of the customer requirement we need to change the  implementation of the function. How can we do that without changing existing class structure?What is the difference between managed and unmanaged code?What is the difference between httpmodules and httphandlers?What is operator overloading?Difference between DataList and Hashtable?Difference between Hashtable and Dictionary?
I will update this post with the answers of above questions.

Display images using Datalist in ASP.NET C#

A Datalist control can be used to display images. We can bind the data from database to datalist. Here is the code to display images using datalist.

Page source:
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal">             <ItemTemplate >                 <asp:Image ID="imglst" runat="server" ImageUrl ='<%#Eval("image")%>' Width="70" Height="50" />             </ItemTemplate>  </asp:DataList>
C# code:  protected void Page_Load(object sender, EventArgs e)         {             DataSet ds = new DataSet();             string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;             SqlConnection conn = new SqlConnection(strcon);             conn.Open();             SqlCommand cmd = new SqlCommand("SELECT image from Image", conn);             SqlDataAdapter adpt = new SqlDataAdapter(cmd);             conn.Close…

Storing Image url in MS Sql database

Image
For fast loading of images we can store the location of the image rather than the storing the image itself. Following can be used to store image url in the database -



protected void Button1_Click(object sender, EventArgs e)         {             if (FileUpload1.HasFile)             {                 string imagepath = "~/images/" + FileUpload1.PostedFile.FileName;                 string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;                 SqlConnection conn = new SqlConnection(strcon);                 conn.Open();                 SqlCommand cmd = new SqlCommand("INSERT into Image (name, image) VALUES (@name, @image)", conn);                 cmd.Parameters.AddWithValue("@name", FileUpload1.PostedFile.FileName);                 cmd.Parameters.AddWithValue("@image", imagepath);                 cmd.ExecuteNonQuery();                 conn.Close();
            }         }

The Microsoft AJAX Library Namespaces

Microsoft AJAX Library Namespaces

The Microsoft AJAX library is about two things -

Extending the Javascript language Providing a base framework for common AJAX tasks. Organization of AJAX library namespaces: Global : It extends many of the functionality of Javascript language, including Number, String, Date, Array. It also includes Type class which is used for registering object oriented items with Javascript.SysSys.netSys.SerializationSys.ServicesSys.UISys.Webforms

Basic commands used in Linux

Hey Guys, I am a newbie to linux. So started learning linux. Here I am sharing basic commands which are required to perform basic operations in linux.


Common commands useful for beginners-
1. man - man command is used to display manual pages called "man pages" .  Manual pages provides detail documentation about commands and other aspects of the system, including configuration files, system calls, library routines and the kernal (core of OS).


e.g. $  man ls                                                  provides information about "ls" command.
Other commands line "whatis" and "whereis" can also be used to access information about maual pages.
whatis display description of a manual page while whereis  display location of manual page.

2. pwd- it is an acronym for print working directory. The basic use of pwd  is to find the full path to the current directory 
                 e.g $  pwd
                 /root

3. cd- this command is used to change current …

Basics of LINQ

LINQ
It stands for Language Integrated Query. By using LINQ we can query database using statements that are built into LINQ-enabled languages such as MS C# and VB 2010.
LINQ works with any collection that implements the IEnumerable or the generic IEnumerable<T> interface. The generic IEnumerable interface has only one method, GetEnumerator, that returns an object that implements the generic IEnumertaor interface. The generic IEnumertaor interface has a Current property, which references the current item in the collection, and two methods, MoveNext and Reset. 
The result of the LINQ query is assigned to a range variable called the query object. The data source is not touched until you iterate on the query object, but if you iterate on the query object multiple times, you access the data source each time.

LINQ Providers:
.NET framework comes with a LINQ to Objects provider. The LINQ provider work as a middle tier between the datastore and the language environment.
To create a LINQ provi…

Introduction to MS SQL Server

In the task management software which we will be discussion in my upcoming posts, I used MS SQL server for database management. In this post I am going to discuss an introduction of MS SQL server.
MS SQL server is a relational database management system developed by Microsoft. MS SQL server uses SQL as its standard database language.
A relational database management system is a means of storing information in such a way that information can be retrived from it. In simplest terms, a relational database is one that present information in tables with rows and columns. A table is referred to as a relation in the sense that it is a collection of objects of same type (rows). Data in a table can be related according to common keys or concepts, and ability to retrive related data from a table is the basis for the term relational database.
MS SQL server was designed to have many hundreds, or even thousands of users accessing it at any point in time. SQL server also contains some advanced data…

Task Management Software

A Task Management software can have following basic features-

To-do listRemindersNotes User can be provided with following options with respect to above features: Priority set for each task of To-do list.Deadlines setup for each task of To-do list.Reminders of due dates of each task.Reminders about birthdays dates and important meetingsTaking notes about specific task of To-do list.Maintaining the status of each task of To-do list. In my upcoming posts I will show you how these functionalities can be implemented using C#.NET. For maintaining the database I will use MS SQL Server.

Send email using C# - details

Here are the details for the classes and properties used in my previous post:

Simple Mail Transfer Protocol is an Internet standard for email transmission across IP networks.

System.Net.Mail: It is namespace contains classes used to send e-mail to a Simple Mail Transfer Protocol server for delivery.

In the previous post,  following two classes of System.Net.Mail are used:

MailMessage - represents the contents of mail  message.SmtpClient - transmit email to the SMTP host that you designate for mail delivery.Note: We can also use "Attachment class" to create mail attachment.
Details about properties and functions of MailMessage class which are used in our program: MailMessage.From - Gets or Sets the from address for current email messageMailMessage.Subject - Gets or Sets the subject line for current email messageMailMessage.Body - Gets or Sets the message bodyMail.To - Gets the address collection that contains the recipients of current email messageMail.To.Add() adds the recipients …

Send email using C#

In my previous post I wrote about sticky notes type application. By using that application a user is able to take small notes. As soon as user closes the notes, the application saves the user notes. So next time when user again launches the application, then user is provided with his notes.

Now I am adding one more functionality to this application. By using this functionality user will be able to send his notes to a specified email. 
To implement this functionality we need to add following libraries in the project: using System.Net.Mailusing System.Net.Mime
Add a link label at the bottom of the form. On the Click event of link label write the logic for sending email. 
Here is the code snippets:


            SmtpClient SmtpServer = new SmtpClient();  SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@gmail.com", "xyz");
            SmtpServer.Port = 587;
            SmtpServer.Host = "smtp.gmail.com";             SmtpServer.EnableSsl = true;             …