Tuesday, December 8, 2015

Cascading DropDown List in ASP.NET C#


In this article, I am going to explain how to code for cascading dropdown list in asp.net c#

Step 1

In VS File->New->WebSite->ASP.NET Empty WebSite->OK



Step 2

At the Solution Explorer. Right Click at Solution Explorer->Add New Item




Step 3

After Clicking on New Item. Select Web Service and rename it to "CascadingDropDownList.asmx.cs" and then click ADD Button


Step 5

Add the refrence of Ajax Contol ToolKit at Solution Explorer

Step 6
After Adding the ToolKit , Write code in (DropdownWebService.cs)
using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections.Specialized;
using AjaxControlToolkit;
using System.Configuration;
using System.Data;
/// <summary>
/// Summary description for DropdownWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class DropdownWebService: System.Web.Services.WebService
{
    [WebMethod]
    public CascadingDropDownNameValue[] BindStatedropdown(string knownCategoryValues, string category)
    {
        SqlConnection constate = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString);
        constate.Open();
        SqlCommand cmdstate = new SqlCommand("select StateCode, StateName from tbl_StateMaster", constate);
        cmdstate.ExecuteNonQuery();
        SqlDataAdapter dastate = new SqlDataAdapter(cmdstate);
        DataSet dsstate = new DataSet();
        dastate.Fill(dsstate);
        constate.Close();
        List<CascadingDropDownNameValue> statedetails = new List<CascadingDropDownNameValue>();
        statedetails.Add(new CascadingDropDownNameValue("Select State", "0"));
        foreach (DataRow dtstaterow in dsstate.Tables[0].Rows)
        {
            string stateID = dtstaterow["StateCode"].ToString();
            string statename = dtstaterow["StateName"].ToString();
            statedetails.Add(new CascadingDropDownNameValue(statename, stateID));
        }
        return statedetails.ToArray();
    }
    [WebMethod]
    public CascadingDropDownNameValue[] BindRegiondropdown(string knownCategoryValues, string category)
    {
        int stateID;
        StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        stateID = Convert.ToInt32(statedetails["State"]);
        SqlConnection conregion = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnection"].ConnectionString);
        conregion.Open();
        SqlCommand cmdregion = new SqlCommand("select CityCode,CityName from tbl_CityMaster where StateCode=@StateID", conregion);
        cmdregion.Parameters.AddWithValue("@StateID", stateID);
        cmdregion.ExecuteNonQuery();
        SqlDataAdapter daregion = new SqlDataAdapter(cmdregion);
        DataSet dsregion = new DataSet();
        daregion.Fill(dsregion);
        conregion.Close();
        List<CascadingDropDownNameValue> regiondetails = new List<CascadingDropDownNameValue>();
        regiondetails.Add(new CascadingDropDownNameValue("select", "0"));
        foreach (DataRow dtregionrow in dsregion.Tables[0].Rows)
        {
            string regionID = dtregionrow["CityCode"].ToString();
            string regionname = dtregionrow["CityName"].ToString();
            regiondetails.Add(new CascadingDropDownNameValue(regionname, regionID));
        }
        return regiondetails.ToArray();
    }
}

Step 7

After writing the code in (DropdownWebService.asmx.cs). Again write click at Solution Explorer ->Add New Item->Web Form->(rename CascadingDropDown.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CascadingDropDown.aspx.cs"
    Inherits="CascadingDropDown" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <table style="width: 100%">
        <tr>
            <td style="width: 100px;">
                State:
            </td>
            <td>
                <asp:DropDownList ID="ddlState" runat="server" CssClass="inpt">
                </asp:DropDownList>
                <asp:CascadingDropDown ID="StateCascading" runat="server" Category="State" TargetControlID="ddlState"
                    LoadingText="Loading States..." ServiceMethod="BindStatedropdown" ServicePath="~/DropdownWebService.asmx">
                </asp:CascadingDropDown>
            </td>
        </tr>
        <tr>
            <td style="width: 100px">
                City
            </td>
            <td>
                <asp:DropDownList ID="ddlCity" runat="server" CssClass="inpt">
                </asp:DropDownList>
                <asp:CascadingDropDown ID="RegionCascading" runat="server" Category="Region" TargetControlID="ddlCity"
                    ParentControlID="ddlState" LoadingText="Loading Cities..." ServiceMethod="BindRegiondropdown"
                    ServicePath="~/DropdownWebService.asmx">
                </asp:CascadingDropDown>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Step 7

After the Writing this Code Press F5 and get the Output

Sunday, June 14, 2015

C# - Indexers

Indexers allow you to use an index on an object to obtain the values stored within the object. The behavior of the indexers is similar to properties; we use get and set methods for define indexers. Properties return a specific data member while indexer returns a particular value from the object.
Properties are defined by the properties name while defining the indexers we use this keyword. Indexers modifier cab be public private, protected or internal. Indexer must have at least one parameter.

Example.   - using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication39
{
   public class Class1
    {
       string[] s = new string[3];
       public string this[int index]
       {
           get
           {
               return s[index];
           }
           set
           {
               s[index] = value;
           }
       }

    }
}


.cs code - 

private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            obj[1] = "Sachin";
           MessageBox.Show(obj[1]).ToString();
          

        }

Output -



                                                                   Author:Sachin Pathak

Properties in C#

In C#, properties are the natural extension of data fields. They are also known as ‘smart field’.
Inside a class, we declare a data field as a private and provide a set of public set and get method to access the data fields, because the data field cannot be accessed directly outside the class so we use get and set methods.

Example:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication38
{
   public class Class1
    {
       
        private int i;
        public int prop
        {
            get
            {
                return i;
            }
       
            set
            {
                i = value;
            }
        }

    }
}

.cs code:
  private void button1_Click(object sender, EventArgs e)
        {
            Class1 obj = new Class1();
            int x = obj.prop;
            x = 10;
            MessageBox.Show(x.ToString());

        }


Output :



Tuesday, April 7, 2015

Learn Java-PART 1



History of java

In 1991, Sun Microsystems engineers led James Gosling decided to develop a language for consumer device like (Cable boxes etc). Consumers want to be hardware independent (means independent application, suppose you develop a application software in windows but it not capable to run on Unix) but hands on those person who free from these tenses. Since different manufactures would use different CPUs, different systems configurations but this language is ready to run on all platforms. The project as code name ‘GREEN’.

In this time, Sun microsystem uses UNIX for their project. We uses C++ language in this project because c++ language was used object oriented’. The original name of the language was oak. And later  they changed the name(oak) to java in January 1995.

Finally, one big step was taken on 7 dec 1995 when Microsoft signed a letter of interest with sun for java technology source license.

Where it used?

Probably Java is used.
1.      Desktop Application such as acrobat reader, Antivirus etc.
2.      Web Application
3.      Enterprise application such as banking application
4.      Mobile such as android and java ME
5.      Embedded system
6.      Smart card
7.      Robotics
8.      Games etc.

Java is high level language. It is completely hardware  independent language means “write once, run anywhere and at anytime, forever”. Programs are run by an interpreter that converts the byte code to the appropriate native machine code.

Byte code: byte code consists of optimized set of instruction that are not specific to processor. We get byte code after compiling the java program using a compiler called javac.

Native code: native code is computer programming (code) that is compiled to run with a particular processor and its set of instruction.

JVM, JRE, JIT and JDK

JVM (java virtual machine):- the bytecode is to be executed by java runtime environment (JRE) which is called as java virtual machine. The program that are running on JVM must be compiled into a binary format which is denoted by .class files.
The  JVM execute .class or .jar files, by either interpreting it or using a just in time compiler (JITc).

Note: the JIT is used for compiling and not for interpreting the file. 

1.      JVM is like a specification (map) of documentation.

2.      JRE is like a implementation of document, it is not an open source.

3.      JDK (java development kit)- it is the collection of JRE and package file.

Note: every source code (.java) generate the .class file.

Firstly we are install the jdk in our computer.

Path set: there are two way  of run the java program.

1.      Computer icon right click -> property ->advanced system -> environment variable -> System variable -> new -> variable name is path and Variable value is address of the bin file.

C:/program files/java/jdk/bin

This the permanent method of path save of java program. You can be save anywhere in computer and run program.

2.      C drives -> program file -> java -> jdk -> bin -> save the program.

Simple program

Class A
{
Public static void main(String arr[])
{
System.out.println(“hello”);
}
}
Compile -> javac A.java
Execution -> java A

BY default java classes and packages are distributed in .jar format.
.jar file is the compress format of the file.

Note:  Java is open source. So you can check rt.java. it is the compress file of classes and package. This is provided by sun microsystem as part of JDK.
C:\program file\java\jdk\jre\lib\rt.jar

E:\jar -> describe all keyword
E:\jar –xf rt.jar

Void: No return type. If there is an error in the program then o.s check it and give the message.

Public : JRE is the outside part of the class. JRE call the main method and access specifier.

Static: In a class there are two type of members.

1.      Instance Member
2.      Class Member

Instance member: this member represent attributes

And behavior of individual object.

Class Member:- class member represent attribute & behavior of the whole class.

Note:  static keyword denotes class members.

By default all the member of a class are instance member.

Method: first letter of each word except the first word of the method is capitalized.
print();

getPrioprity();

getKeyMathodMap();       etc.

String arr[] : main method is to used command line argument. It is represent I/P provided by command prompt with name of command.

System: it is the final class in java.lang package. System class facility provided input, output, error, loading file & library.

Out: out is static member of the system class and is type printstream class.

Println: it is the method of printstream class.

Example:

 class printstream
{
Public void print(String s)
{
-------
}
Public void println(String s)
{
-------
}
Class system
{
Public static printstream out;
{------
}


                                                          Author-Ravi Kumar