Wednesday, April 15, 2015

Write data in XML file and Read form XML file

//.aspx file

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        id<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        name<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        city<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

//.cs file

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //fillgrid();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (File.Exists(Server.MapPath("test.xml")))
        {
            appendxml();
        }
        else
        {
            createxml();
        }
    }

    private void createxml()
    {
        string path = Server.MapPath("test.xml");

        XmlTextWriter xtr = new XmlTextWriter(path, null);

        xtr.WriteStartDocument();

        xtr.WriteStartElement("root");
        xtr.WriteStartElement("student");
        xtr.WriteElementString("id", TextBox1.Text);
        xtr.WriteElementString("name", TextBox2.Text);
        xtr.WriteElementString("city", TextBox3.Text);
        xtr.WriteEndElement();
        xtr.WriteEndElement();

        xtr.WriteEndDocument();
        xtr.Close();
        fillgrid();
    }

    private void fillgrid()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("test.xml"));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    private void appendxml()
    {
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("test.xml"));

        XmlElement std = xdoc.CreateElement("student");
        XmlElement id = xdoc.CreateElement("id");
        XmlElement name = xdoc.CreateElement("name");
        XmlElement city = xdoc.CreateElement("city");

        XmlText idtext = xdoc.CreateTextNode(TextBox1.Text);
        id.AppendChild(idtext);
        XmlText nametext = xdoc.CreateTextNode(TextBox2.Text);
        name.AppendChild(nametext);
        XmlText citytext = xdoc.CreateTextNode(TextBox3.Text);
        city.AppendChild(citytext);

        std.AppendChild(id);
        std.AppendChild(name);
        std.AppendChild(city);

        xdoc.DocumentElement.AppendChild(std);
        xdoc.Save(Server.MapPath("test.xml"));

        fillgrid();

    }

count total number if rows in table

    protected void Button2_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("select count(id) from std", con);
        con.Open();
        int c = Convert.ToInt32(cmd.ExecuteScalar());//ExecuteScalar is use when you want single value result like number of rows, total of salary average etc.
        Response.Write("total no of records are : " + c);

    }

Display data in aspx page in asp.net

SqlConnection con = new SqlConnection(@"your connection string");
    SqlCommand cmd;
    SqlDataReader rd;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("select * from std", con);
        con.Open();
        rd = cmd.ExecuteReader();
        if (rd.HasRows)
        {
            while (rd.Read())
            {
                Response.Write(rd["fieldname1"].ToString() + "  " + rd["fieldname2"].ToString() + "  " + rd["lname"].ToString()+"<br>");
            }
            con.Close();
        }

    }
    

Monday, April 13, 2015

insert data in SQL SERVER database in ASP.NET

//.cd file

 SqlConnection con = new           SqlConnection(ConfigurationManager.ConnectionStrings["str"].ConnectionString);

 SqlCommand cmd;

cmd = new SqlCommand("insert into student values(@fnames,@lname)", con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@fnames", "some values");
        cmd.Parameters.AddWithValue("@lname", "some values");

        con.Open();
        cmd.ExecuteNonQuery();

        con.Close();

Saturday, April 4, 2015

Array List in c#

an array list is like array but we does not need to specify the size of array list.

ArrayList is a class in collection name space

Some Common Property 


Capacity : Get or set the number of element that ArrayList contain

Count : get the number of element that array list contain

IsFixedSize : Gets a value indicating whether a ArrayList object has a fixed size. A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but does allow the modification of existing elements.

Item : Gets or sets the element at the specified index.

Mothod


Add() : add on object at the end of array list

Example


     ArrayList arr4 = new ArrayList();
            arr4.Add(25);
            arr4.Add(26);
            arr4.Add(36);
            arr4.Add(43);

            foreach (int ok in arr4)
            {
                Console.WriteLine(ok);
            }


            Console.ReadKey();

what is Array, Types of Array in C#

what is Array, Types of Array in C#

An array is the collection of the variable of the same data type that is referred to by a common name.  
In C#,arrays can have one or more dimension.

The principle advantage of array is that it arranges data in such a way that it can be easily manipulated.

In c#,the arrays are implemented as objects.

One-Dimension Array

One dimension array is the list of variables. 
This list is common in programming.

First you must declare a variable that can be referred to an array.

Second you must create an instance of an array by use of new.

Syntax
Type[] arrayname=new type[size];

Example:

            int[] arr1 = new int[10];

            for (int i = 0; i < 10; i++)
            {
                Console.Write("enter value : ");
                arr1[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("value : " + arr1[i]);

            }
//-----------------------


Arrays can be initialized when they are created.

The general form for initializing one dimensional array is shown here:

Type[] arrayname={val1,val2…};


Multidimensional Array

A multidimensional array is an array that has two or more dimensions and an individual element is accessed through the combination of two or more indices.

Two-dimensional array—simple form of multidimensional array.
Location of element is specified by two indexes.

Think of two dimensional array as a table of information, one index indicates the row ,other indicates the column.

Int[,] table=new int[10,20].
Separated by comma.
To access an element in a two dimension array, you must specify both indices ,separating the two with a comma.
like.
Table[3,5]=10;

                  int[,] arr2 = new int[3, 3];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write("Enter Value : ");
                    arr2[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine("value : " + arr2[i, j]);
                }
            }


Jagged Array

A jagged array is an array of array in which length of each array can differ.

Thus a jagged array can be used to create a table in which the row length are not the same.

Jagged array are declared by using sets of square brackets to indicate each dimension.

Type[][] arrayname=new type[size][];

Size indicates number of rows in the array.

The rows themselves have not been allocated.

Instead the rows are allocated individually.

Example

            int[][] arr3 = new int[3][];
            arr3[0] = new int[2];
            arr3[1] = new int[3];
            arr3[2] = new int[4];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < arr3[i].Length; j++)
                {
                    Console.Write("Enter value : ");
                    arr3[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < arr3[i].Length; j++)
                {
                    Console.WriteLine("arr3[{0}][{1}] : {2} ", i, i, arr3[i][j]);
                }

            }