Wednesday, 17 September 2014

Dictionary(Generics) in c# asp.net | example and output

In this article i am going to show how to use generics using dictionary .
basically dictionary uses key value pair to display record.
we can add , remove , insert data in dictionary using key / value pair .
but this is that you need to  follow some rules.


To declare or define dictionary is

Dictionary <string , Int32> myFirstdictionary=new Dictionary<string,int>();


here my dictionary name is myFirstdictionary.
where i m pasing key as string and value will be int32,
now to add value in data using key we use key and pair value.
like this...

myFirstdictionary.Add("dev", 1);

where "dev" is string
and 1 is value type of  int.


Now to fetch data fromdictionary row by row we need to use for or foreach loop.


foreach(KeyValuePair<string, Int32> keys in myFirstdictionary)
        {
            Response.Write(keys.Key +"/"+ keys.Value + "<br/>");

        }



it will print data like this as printed below.





Monday, 15 September 2014

List(Generic) | DataTable | Bind Gridview with examples

In this article i amGgoing to show how to create temporary table and fetch data in list to perform generics process and later on to bind Gridview. with examples.

   private void Genericswithtable()
    {
        DataTable temporarytableDt = new DataTable();
        temporarytableDt.Columns.Add("Id");
        temporarytableDt.Columns.Add("Name", typeof(String));
        temporarytableDt.Columns.Add("Email", typeof(String));
        temporarytableDt.Rows.Add(1, "dev", "dev@cxom");
        temporarytableDt.Rows.Add(2, "deva", "deva@cxom");

//created temporary table  so here  i am not  using any sql server connection just created
temporary table for example.

   
     
        List<string> listtable = new List<string>();
Declaration of list type string
     

        for (int i = 0; i < temporarytableDt.Rows.Count;i++)
        {
            listtable.Insert(i, temporarytableDt.Rows[i]["Name"].ToString() + 
                " / " + temporarytableDt.Rows[i]["Email"].ToString());
            
        }
//untill there will be rows in table all rows value will be inserted in List 
        //listtable.RemoveAt(1);
        listtable.Insert(2, "Raj");
        listtable.Add("Raja");
//Now here we can add more value wchich will appear after table value will be displayed.

        gridlist.DataSource = listtable;
        gridlist.DataBind();
//now we bind list to gridview there are many ways to take table value in list but this was simple method .
no need to create class or implement any kind of interface.
    }




Generics in c# with examples | Generics demo

Generics are type safe because there is no boxing and unboxing so
i will take  single datatype not heterogeneous like array list
so execution will be faster compare to collections.
Just we need to import System.collection.generics;


 private void PrivateGenerics()
    {
        List<int> newlist = new List<int>(); //declaring generics type int
        newlist.Add(3); // adding values
        //newlist.Add("5"); //will give compile time error;
        newlist.Add(2);
        int i = Convert.ToInt32("5"); 
        newlist.Add(i);
        newlist.Insert(0, 7); // giving index for in flow.
        newlist.Insert(1, 900000001);
        newlist.Insert(2, 900000002);
        newlist.Add(20);
        foreach (int myValue in newlist)
        {
            Response.Write(myValue +"<br/>");
        }

    }


 newlist.Sort();
        foreach (int myValue in newlist)
        {
            Response.Write(myValue +"<br/>");
        }
Generics With Array List .here i have shown how to add your arrayList in List<type int>
.

ArrayList Myarlist = new ArrayList();
        Myarlist.Insert(0, 40);
        Myarlist.Insert(1, 45);
        //Myarlist.Insert(2, "raj");
        var list = Myarlist.Cast<int>().ToList();//to add arraylist in list
        newlist.InsertRange(3, list);
        foreach (int myValue in newlist)
        {
            Response.Write(myValue +"<br/>");
        }


Now last code we added int value in array List so it works Fine.
Now we are going to add one string value in ArrayList And then See what would be
result.will it gives result or  there will be error.
Its showing Error because we have done casting of Arraylist to
integer type but we are providing String value also.


How to Bind Gridview using ArrayList in C# | Bind DropDown list

In This Article I am going to show You How to Bind GridView with Array List using Datatable and multidimensional array.

to bind dropdown
 string[,] mularray = {

                    { "Dev", "0001","6" },

                    { "Raj", "0003" ,"7"},

                    { "Tirth", "0004" , "8"},

                 };
   ArrayList arrList = new ArrayList();
            for (int i = 0; i < mularray.GetLength(0); i++)
            {
               
                arrList.Add(new ListItem(mularray[i, 0], mularray[i, 2]));
               
            }

            ddlp.DataSource = arrList;
            ddlp.DataTextField = "text";
            ddlp.DataValueField = "value";
            ddlp.DataBind();


lets have a code to bind Gridview using simple table which will swap in table and bind gridview.





            ArrayList MyarrayList = new ArrayList();
            DataTable temporarytableDt = new DataTable();
            temporarytableDt.Columns.Add("Id");
            temporarytableDt.Columns.Add("Name", typeof(String));
            temporarytableDt.Columns.Add("Email", typeof(String));
            temporarytableDt.Rows.Add(1, "dev", "dev@cxom");
            temporarytableDt.Rows.Add(2, "deva", "deva@cxom");
          



.cs code


        
            for (int i = 0; i < temporarytableDt.Rows.Count; i++)
            {
                string strbinfval = temporarytableDt.Rows[i]["Name"].ToString();
                MyarrayList.Insert(Convert.ToInt32(temporarytableDt.Rows[i]["Id"]) - 1,               

temporarytableDt.Rows[i]["Name"].ToString());
              
            }
            grdarraylist.DataSource = temporarytableDt;
            grdarraylist.DataBind();


.aspx code.


<div>
    <asp:GridView runat="server"  ID="grdarraylist" AutoGenerateColumns="false"    Caption = "Bind Grid With  Table"

Width="400px">
    <AlternatingRowStyle BackColor ="ActiveBorder" BorderColor="ActiveCaption" VerticalAlign="Middle"
   />
   <Columns>
 
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />


   </Columns>
   </asp:GridView>
    </div>


now i wanted to bind gridview using multidimensional or two dimensional array.


.cs page

 string[,] mularray = {

                    { "Dev", "0001","6" },

                    { "Raj", "0003" ,"7"},

                    { "Tirth", "0004" , "8"},

                 };
            ArrayList arrList = new ArrayList();
            for (int i = 0; i < mularray.GetLength(0); i++)
            {
              
                arrList.Add(new ListItem(mularray[i, 0], mularray[i, 2]));
            }
            grid.DataSource = arrList;
            grid.DataBind();


just fetched data into dimensional array and added in array list.

.aspx page
<asp:GridView runat="server"  ID="grid" AutoGenerateColumns="false"  Caption = "Bind Grid With  ArrayList" Width="400px">
    <AlternatingRowStyle BackColor ="ActiveBorder" BorderColor="ActiveCaption" VerticalAlign="Middle"/>
   <Columns>
  

<asp:BoundField DataField="Text" HeaderText="Id" />
<asp:BoundField DataField="Value" HeaderText="Name" />
   </Columns>
    </asp:GridView>




ArrayList in c# with examples

How to use arraylist in c#.
theses are basically collection so we need to import system.Collection;

Then to add array and array list

Here i have shown the code and examples to include array list in ur application.


private void arraylistdefault()
    {
        ArrayList myValue = new ArrayList { "MyFirstValue", "2", "3" };

        int indexofmyva = myValue.IndexOf("MyFirstValue");
      //  Response.Write(indexofmyva);//to get index of  MyFirstValue string
        //myValue.RemoveAt(indexofmyva); //to to removce  MyFirstValue
        //using Index
        myValue.Insert(3, "Third Value"); // inserting Third Value string
        myValue.Insert(4, "insert4");
        myValue.Add("add5");
        string[] ek = { "5col-name", "6-col-name" }; //adding range
        //it will insert 5col-name at 6 th position and 6-col-name at 6th position

        //myValue.Remove("added5"); //will remove  string added5
        //myValue.RemoveAt(3); will remove word which occupies value of index number 3;
     
        myValue.InsertRange(5,ek);
        myValue.Insert(7, "addedT7");
        myValue.RemoveAt(6); // will remove index 6th value
        myValue.Insert(6, ">new"); // will add >new string on 6th  position
        myValue.SetRange(7, ek);
        myValue.GetRange(3, 2);
        //myValue.Add(3); //Unable to cast object of type 'System.Int32' to type 'System.String'.

     
     even we can add int type data but we need to do casting like boxing and unboxing.

            foreach(string strvalue in myValue)
            {
                Response.Write(strvalue +"<br/>");
            }
    }

Now add array in array List.


Now here we are going to take int string value and wanted to convert in integer .
ArrayList new_array = new ArrayList(); 
new_array.AddRange(mystrarray); 
int forty = Convert.ToInt32("40"); 
new_array.Add(forty);