Posts

Showing posts from September, 2011

Factorial Program using C#

This is the basic program for the beginners.Here's how to find the factorial of 3.

Mathematically it follows like this

3*2*1=6


class FactorialProgram
    {
staticvoid Main(string[] args)
        {
            var fp =
new FactorialProgram();
Console.Write("\n Enter the number:\t");
            var
result = fp.fact(int.Parse(Console.ReadLine()));
Console.WriteLine(result);
Console.ReadLine();

        }
int fact(int num)
        {
int temp = 1;
for (int i = num; i > 1; i--)
            {
                temp = temp * i;
             }
return temp;
        }

    }

How to Clear the Textbox Text using C#?

This is one of the common requirement we get in our projects.Text box has a wonderful method called "Clear".This method is used to clear the text in the Text box.I have given a simple example to explain this .I have implemenetd a common method to clear the Text of few Controls .One of that is Textbox.


void ClearInputs(ControlCollection ctrls) { foreach (Control ctrl in ctrls) { if (ctrl is TextBox) ((TextBox)ctrl).Text = string.Empty; ClearInputs(ctrl.Controls); } }You can call this method where ever you want to clear the Text from the Textbox.For more information please click this linkTextBoxHope this would help the beginners. Cheers.

Frame the SQL Query using String Function Format()

The main idea behind this FAQ is to frame our SQL Query using String Format Function. I think this is one of the best way to frame the SQL Query that i found. *** This code introduces SQL injection attacks and should not be used. ***The string.Format method is a static method that receives a string that specifies where the following arguments should be inserted, and these are called substitutions.

In source code we need to use to frame the SQL Query
publicbool SaveData(string firstName, string lastName)
    {
String connectionString = ConfigurationManager.ConnectionStrings["TESTDB"].ConnectionString;
bool result = false;
using (SqlConnection connection = new SqlConnection(connectionString))
        {
SqlCommand cmd = new SqlCommand();
            cmd.Connection = connection;
             cmd.CommandText = String.Format("insert into Test_DB.dbo.PersonName(FirstName,LastName) values ('{0}','{1}')", firstName, lastName);
             cmd.CommandTy…

A New Approach To .NET Key Value pairs in Collections

A new approach has been evolved in the Collections ,i.e The LookUp is one the Class in the Collection which represents a collection of keys each mapped to one or more values.We are very familiar with the Dictonary Collection which maps only one value per key mapping.But when we come across to map the keys with more values.Then we have this LookUp
Where
TKey    ----> The types of the keys in the LookUp
TValue ---->    The type of the elements of each IEnumerable value in the LookUp.

I have worked out on this concept to understand the LookUp class. Before going in deeply , let's create a Custom Class like this
Employee.cs
using System.Collections.Generic;

///
/// Summary description for Employee
///
publicclass Employee
{
public int ID { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public string Name { get; set; }
public bool MartialStatus { get; set; }
public Employee()
    {
//
// TODO: Add constructor logic here
//
    }

}
I have taken an aspx pagew…

How to apply watermark to the textbox and dropdownlist?

Create a watermark effect on your TextBox and display instructions to users, without taking up screen space.This warkmark style can applied by using the CSS and append this to your control using jquery.Hi this is basic where we you use to get to apply watermark to the Textbox or DropDown.I have implemented this by using the jquery.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WaterMark.aspx.cs" Inherits="WaterMark" %>


<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Water Mark using jquery title>
<styletype="text/css">
        .watermarkOn
        {
            color: #CCCCCC;
            font-style: italic;
        }
style>
<scripttype="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js">script>
<scripttype="text/javascript">
        $(document).ready(function () {

            // Define what…

ASP.NET Controls Calling using jQuery

As we all know the jQuery is one of the most powerful javascript library which simplifies the HTML DOM elements traversing,event handling e.t.c.  Please go through this where you can call the ASp.NET controls using jQuery.One thing, while creating object of any ASP.NET control, always use ClientID. As when Master pages are used then the ID of the ASP.NET controls is changed at run time.

1.Getting the Label Text

E.g:$('#<%=Label1.ClientID%>').text();

2. Set the label Text

E.g:$('#<%=Label1.ClientID%>').text("New Value");

3. Set TextBox Text

E.g:$('#<%=TextBox1.ClientID%>').val("New Value");

4. Get Dropdown value:

E.
g:$('#<%=DropDownList1.ClientID%>').val();

5. Set Dropdown value:

E.
g:$('#<%=DropDownList1.ClientID%>').val("New Value");

6. Get text of selected item in dropdown:

E.g:$('#<%=DropDownList1.ClientID%> option:selected').text();

7. Get Checkbox Status:

E.g:$('…

ASP.NET - Data insertion using jquery AJAX

The main idea behind this article is to insert data using jQuery AJAX to avoid postback. AJAX offers users a seamless way to work with your interface, no waiting for whole pages to load. jQuery has a set of tools to make it super simple to implement.Most everyone knows what iAJAX is. But for the beginners i would like to provide some information regarding the AJAX.
What is AJAX?
AJAX is a short hand for asynchronous JavaScript and XML.  Which plainly means that instead of waiting for the whole page to load, you can load only what you need to.  So if you only need to update one small text part of your site, you don’t have to worry about loading everything else on that page.  A vast majority of sites use this technology now.  Probably one of the most popular uses is an auto complete feature for the search box at Google and Yahoo.
If you see another term XHR, which is shorthand for XML HTTP request, it’s the same thing. Don’t be afraid of this jargon; AJAX is not rocket s…

Different ways to use String Split Function in .NET

This method finds all the substrings in a string that are seperated by one or more characters, returning a string array. Split function has more number of overload method where you can specifiy the maximum number of elements in an array to return from the string.Here i have two important things about Split function of String class. Split function of the string class split the string in array of string.
Split function to split string in array
String.Split( char[])

string words = "apple,bat,cat,dog,,fox,Ghost.";
string [] split = words.Split(newChar [] {' ', ','});
Output will be like this

apple
bat
cat
dog

fox
Ghost

If you want to remove the Empty String Please make use of the Overloaded Methods.
Overload method with option
1. String.Split(Char[], StringSplitOptions)
string words = "apple,bat,cat,dog,,fox,Ghost.";
string [] split = words.Split(newChar [] {' ', ','},StringSplitOptions.RemoveEmptyEntries);
The output would be like this
apple
bat
ca…