Tuesday, 22 April 2014

Transfer the content from DropdownList1 to DropdownList2 and vice versa.



Create a web application with two DropdownList1 & DropdownList2 and design application to transfer the  content from  DropdownList1 to DropdownList2  and vice versa.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADO.net_Tutorial
{
    public partial class WebForm6 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListItem selecteditem = new ListItem("Select", "7");

                ListItem item1 = new ListItem("One", "1");
                ListItem item2 = new ListItem("Two", "2");
                ListItem item3 = new ListItem("Three", "3");

                ListItem item4 = new ListItem("Four", "4");
                ListItem item5 = new ListItem("Five", "5");
                ListItem item6 = new ListItem("Six", "6");

                selecteditem.Selected = true;
               DropDownList1.Items.Add(selecteditem);
               DropDownList2.Items.Add(selecteditem);

               DropDownList1.Items.Add(item1);
               DropDownList1.Items.Add(item2);
                DropDownList1.Items.Add(item3);

             DropDownList2.Items.Add(item4);
             DropDownList2.Items.Add(item5);
             DropDownList2.Items.Add(item6);

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            ListItem[] List1Array = new ListItem[DropDownList1.Items.Count];
           DropDownList1.Items.CopyTo(List1Array, 0);

            ListItem[] List2Array = new ListItem[DropDownList2.Items.Count];
           DropDownList2.Items.CopyTo(List2Array, 0);

           DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
          
            foreach (ListItem myItem in List1Array)
            {                
                DropDownList2.Items.Add(myItem.Text);
            }

            foreach (ListItem myItem in List2Array)
            {
           DropDownList1.Items.Add(myItem.Text);
            }
        }
    }
}

Output:-



If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)

Sunday, 20 April 2014

Single Dimensional Array

Accept and print the single dimensional array.

using System;

namespace TestArray
{
class program
{
   static void Main(string[] args)
       {
           int[] arr1 = new int[5];
           Console.WriteLine("Enter the value for array:");
           for(int i=0; i<5; i++)
           {
           arr1[i]= Convert.ToInt16(Console.ReadLine());
           }
           Console.WriteLine("Display the single dimensional array:");
           foreach (int i in arr1)
           {
               Console.WriteLine(i);
           }
           Console.ReadKey();
       }       
}
}

Multidimensional Array

Accept the value for multidimensional  array from user and print the it  in matrix format.

using System;
namespace testArray
{
    class Program
    {
        static void Main(string[] args)
        {Console.WriteLine("Enter the value for multidimensional array:");
            int[,] arr1 = new int[3, 2];
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    arr1[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
                      Console.WriteLine("Displaying the multidimensional array in matrix format :");

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.Write(arr1[i, j] +"  ");
                }
                Console.WriteLine( "\n");
               }
            Console.ReadKey();
        }
    }
}

Output:-
Enter the value for multidimensional array:
2
3
5
9
1
0

Displaying the multidimensional array in matrix format :
2  3
5  9
1  0


Jagged array.


Accept and print the value of jagged array.

using System;

namespace JaggedArray
{
    class Program
    {
        static void Main(string[] args)
        {         
            int[][] jagged = new int[3][];

            Console.WriteLine("Enter value for 1st row:");
            jagged[0] = new int[3];
            for (int i = 0; i < 1; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    jagged[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.WriteLine("Enter value for 2nd row:");
            jagged[1] = new int[2];
            for (int i = 1; i < 2; i++)
            {
                for (int j = 0; j <2; j++)
                {
                    jagged[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.WriteLine("Enter value for 3rd row;");
            jagged[2] = new int[4];
            for (int i = 2; i < 3; i++)
            {
                for (int j = 0; j<4; j++)
                {
                    jagged[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            // Print out all elements in the jagged array.
            Console.WriteLine("\n Jagged array:");
            for (int i = 0; i <3; i++)
            {
                int[] newArray = jagged[i];
                for (int j = 0; j < newArray.Length; j++)
                {
                    Console.Write(newArray[j] + " ");   
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

Output:- 

Enter value for 1st row:
0
9
3
Enter value for 2nd row:
8
4
Enter value for 3rd row:
1
5
3
6

Jagged array:
0  9  3
8  4
1  5  3  6

If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)


Bind (display) data in gridview using ADO.net disconneted achitechture

WAP to display data in ASP.Net GridView Control from SQL  Database using ADO.Net disconnected Architecture.

Explanation:- Please check the explanation of ADO.Net connected Architecture program.

WebForm1.aspx.cs



using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace ADO.net_Tutorial
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //bind gridviw using disconnted architechture of ado.net
            string constr = ConfigurationManager.ConnectionStrings["testconstr"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand(" select * from EmployeeDetails", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
}

Bind (display) data in gridview using ADO.net conneted achitechture



WAP to display data in ASP.Net GridView Control from SQL  Database using ADO.Net connected Architecture.

Explanation:- for this program, first  you have create a sql server database table ( "EmployeeDetails" ) with few Data fields like EmpID, EmName etc ,And then  create a connection string in web.config file with "testconstr" name, using  following code.
but make sure the Data Source path and AttachDbFilename  path is right or not because in following code I am using my database path.

<connectionStrings>
     <add name="testconstr" connectionString="Data Source=.\SQLEXPRESS; AttachDbFilename=C:\Users\MAKANSINGH\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

And then open a web page and drop a GridView control  on the webpage from ToolBox ->Data-> Gridview. then write the following code in code behind file and run your program.

If you have any doubt please contact me.

WebForm1.aspx.cs


using System;
using System.Data.SqlClient;
using System.Configuration;

namespace ADO.net_Tutorial
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string constr = ConfigurationManager.ConnectionStrings["testconstr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                SqlCommand cmd = new SqlCommand(" select * from EmployeeDetails", con);
                con.Open();
                GridView1.DataSource = cmd.ExecuteReader();
                GridView1.DataBind();
            }
        }
    }
}

If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)

Transfer data from one web page to another using cookie collection

Accept ItemId, Name and Quantity and store them in cookie collection and show them in next form.

Explanation:- First create two web page in asp.net with name of  Webform1 or WebForm2 ,  where you have to drop some asp.net control to accept  and display the input./output as per as the program requirement  (like TextBox, Label, Button etc) and then finally write down the the  code in the code behind  file respectively. 


WebForm1.aspx.cs

using System;
using System.Web;
using System.Web.UI;

namespace Architecture
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }        
        protected void Button1_Click(object sender, EventArgs e)
        {
            int itemid = Convert.ToInt32(TextBox1.Text);
            string itemname = TextBox2.Text;
            int qty = Convert.ToInt32(TextBox3.Text);

            HttpCookie cookies = new HttpCookie("productdetails");
            cookies["itemid"] = Convert.ToString(itemid);
            cookies["itemname"] = itemname;
            cookies["qty"] =Convert.ToString(qty);

            cookies.Expires = DateTime.Now.AddHours(1);
            Response.Cookies.Add(cookies);

           Response.Redirect("WebForm2.aspx");
           }
    }
}

WebForm2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Architecture
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookies = Request.Cookies["productdetails"];
            if (cookies != null)
            {
                Label1.Text = cookies["itemid"];
                Label2.Text = cookies["itemname"];
                Label3.Text = cookies["qty"];
            }
        }
    }
}
If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)