Monday, 17 February 2014

Adrotator Example in asp.net


how to use Adrotator in asp.net

Introduction:

Here I will explain how to use Adrotator in asp.net using c# or how to create Automatic image changes in page without page refresh. If you want to create simple Adrotator application in asp.net using c# then do following step.

So, Now I explaining how to use Adrotator in asp.net using c#.

Description: 

In previously post I explained to  
Three tier architecture in asp.net
Remove .aspx from url in asp.net
Temp table in sql
Change the column name or datatype or size in sql server
Stored procedure with paging sql server
Calling web service without adding web reference

Three tier architecture in Asp.net

Two tier architecture in Asp.net

One tier architecture in Asp.net

Simple login code in asp.net using c#

Log out code in asp.net with C#

How to create chat application in asp.net using c#

Send mail in asp.net

Get IP address from client machine

how to publish website

Connectionstring in asp.net

 

OR
Automatic image changes in page without page refresh

--> Some times require to create page for change image after specific time without page refresh
--> then do following

-->  copy the following code in your .aspx page
------------------------------------------------------------------------------------------------

<div>
Adrotator Control : Rotates Ads without page refresh
<br />
<asp:scriptmanager id="ScriptManager1" runat="server">
</asp:scriptmanager>
<asp:timer id="Timer1" runat="server" interval="1000">
<%-- interval="1000" = >1 second--%>
</asp:timer>
<asp:updatepanel id="UpdatePanel1" runat="server">
<contenttemplate>
<center>
<asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/adver.xml"
Height="120px" Width="500px" BackColor="#CC33FF" BorderColor="#3366FF"
BorderStyle="Solid" />
</center>
</contenttemplate>
</asp:updatepanel>
</div>
-- >Next , Add one XML file from solution explorer(current project)
---> and set this XML file name= adver

--> next following code copy in your XML file
------------------------------------------------------------------------------------------------



<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <ImageUrl>~\img\kk.GIF</ImageUrl>
  </Ad>
  <Ad>
    <ImageUrl>~\img\014.JPG</ImageUrl>
  </Ad>
  <Ad>
    <ImageUrl>~\img\gh.gif</ImageUrl>
  </Ad>

</Advertisements>

-->  In above code "<ImageUrl>~\img\gh.gif</ImageUrl> " it is your image path
-->Here all images in img folder

-->Next , Run this page and every 1 second image change without page refresh.

XML example in asp.net


XML example in asp.net  OR  XML CRUD operation in asp.net 


Introduction:

Here I will explain how to use XML in asp.net using c# or how to work with XML using asp.net with C#. If you want to create simple XML file with data in asp.net using c# then do following step.

we can perform CRUD operation on XML file.

So, Now I explaining CRUD operation on XML file in asp.net using c#.

Description: 

In previously post I explained to  
Three tier architecture in asp.net
Remove .aspx from url in asp.net
Temp table in sql
Change the column name or datatype or size in sql server
Stored procedure with paging sql server
Calling web service without adding web reference

-- > If you want to
1) insert data in XML OR
2) Update data in XML OR
3) Delete data in XML OR
4) Display data from XML file

then Do following

--> following code  paste in your .aspx file
------------------------------------------------------------------------------------------------

  

<div>
<table border="1">
<tr>
    <th>ID : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox1" />
    </td>
</tr>
<tr>
    <th>FirstName : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox2" />
    </td>
</tr>
<tr>
    <th>Last Name : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox3" />
    </td>
</tr>
<tr>
    <th>City Name : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox4" />
    </td>
</tr>
<tr>
    <th>Pincode : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox5" />
    </td>
</tr>
<tr>
<th colspan="2">
<asp:Button Text="Insert" ID="Button1" runat="server" onclick="Button1_Click" />
&nbsp;<asp:Button Text="Update" ID="Button2" runat="server"
    onclick="Button2_Click" />
&nbsp;<asp:Button Text="Delete" ID="Button3" runat="server"
    onclick="Button3_Click" />
</th>               
</tr>
<tr>
    <th colspan="2">
        <asp:GridView runat="server"  ID="gv" />
            </asp:GridView>
    </th>
</tr>
</table>
</div>



--> paste following namespace in your code behind
------------------------------------------------------------------------------------------------


using System.Xml;  //required namespace
using System.IO;    //required namespace

--> paste following code  in your page load
------------------------------------------------------------------------------------------------

    

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            FillGridView();
        }
}


--> And also copy all following event in your code behind file
------------------------------------------------------------------------------------------------

   

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (File.Exists(Server.MapPath("StudentDetail.xml")) == false)
        {
            CreateNewFileStruct();
            //if file not exist then create file
        }
        else
        {
            InsertNewRecord();
            // if file exist then insert record in xml file
        }
    }

    private void InsertNewRecord()
    {
    //this is for insert new record
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("StudentDetail.xml"));

        XmlElement Student = xdoc.CreateElement("Student");

        XmlElement ID = xdoc.CreateElement("ID");
        XmlText xmlID = xdoc.CreateTextNode(TextBox1.Text);

        XmlElement FirstName = xdoc.CreateElement("FirstName");
        XmlText xmlFirstName = xdoc.CreateTextNode(TextBox2.Text);

        XmlElement LastName = xdoc.CreateElement("LastName");
        XmlText xmlLastName = xdoc.CreateTextNode(TextBox3.Text);

        XmlElement City = xdoc.CreateElement("City");
        XmlText xmlCity = xdoc.CreateTextNode(TextBox4.Text);

        XmlElement Pincode = xdoc.CreateElement("Pincode");
        XmlText xmlPincode = xdoc.CreateTextNode(TextBox5.Text);

        ID.AppendChild(xmlID);
        FirstName.AppendChild(xmlFirstName);
        LastName.AppendChild(xmlLastName);
        City.AppendChild(xmlCity);
        Pincode.AppendChild(xmlPincode);

        Student.AppendChild(ID);
        Student.AppendChild(FirstName);
        Student.AppendChild(LastName);
        Student.AppendChild(City);
        Student.AppendChild(Pincode);

        xdoc.DocumentElement.AppendChild(Student);

        xdoc.Save(Server.MapPath("StudentDetail.xml"));
        FillGridView();
    }
    private void FillGridView()
    {
    //this is for fill gridview
        System.Data.DataSet ds = new System.Data.DataSet();
        ds.ReadXml(Server.MapPath("StudentDetail.xml"));
        gv.DataSource = ds;
        gv.DataBind();
    }

    private void CreateNewFileStruct()
    {
    //this is create a new file
        string path = Server.MapPath("StudentDetail.xml");
        XmlTextWriter xtw = new XmlTextWriter(path, null);
        xtw.WriteStartDocument();
       
        xtw.WriteStartElement("Dataset");
        xtw.WriteStartElement("Student");

        xtw.WriteElementString("ID", TextBox1.Text);
        xtw.WriteElementString("FirstName", TextBox2.Text);
        xtw.WriteElementString("LastName", TextBox3.Text);
        xtw.WriteElementString("City", TextBox4.Text);
        xtw.WriteElementString("Pincode", TextBox5.Text);

        xtw.WriteEndElement();
        xtw.WriteEndElement();

        xtw.WriteEndDocument();
        xtw.Close();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
    //this is for one Delete record from XML file
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("StudentDetail.xml"));

        XmlNodeList list =  xdoc.SelectNodes("/Dataset/Student");

        foreach (XmlNode item in list)
        {
            if (item.ChildNodes[0].InnerText == TextBox1.Text)
            {
                item.ParentNode.RemoveChild(item);
            }
        }

        xdoc.Save(Server.MapPath("StudentDetail.xml"));
        FillGridView();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    //this is for update record in XML file
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("StudentDetail.xml"));

        XmlNodeList list = xdoc.SelectNodes("/Dataset/Student");

        foreach (XmlNode item in list)
        {
            if (item.ChildNodes[0].InnerText == TextBox1.Text)
            {
                item.ChildNodes[1].InnerText = TextBox2.Text;
                item.ChildNodes[2].InnerText = TextBox3.Text;
                item.ChildNodes[3].InnerText = TextBox4.Text;
                item.ChildNodes[4].InnerText = TextBox5.Text;
            }
        }
        xdoc.Save(Server.MapPath("StudentDetail.xml"));
        FillGridView();
    }


Asp.net tutorials