vijay

welcome Netizen

welcome Netizen
Hi folks this site Share Basic Knowledge in .NET, DotNet, C#,DOT NET CODE, ASP.NET, VB.NET, DOT NET TRICKS,Dotnetcode,Android and Latest Technology Articles in VB,Android and ASP.NET

Wednesday, July 27, 2011

Insert/edit/delete in Xml using Asp.net


Hi..
Its very interesting when talk about Xml(extensible Markup language.).Xml can support for all language. its used to design and transport the data. important to know and easy to learn. Now we discuss about to insert, update and delete in xml.
Xml  act as database. So we can store any information in xml.Also we can retrieve  information by using any language. Here I store the image in xml,without using database I store the image url in XML.
For that create an folder an image in project. So we can store the image in that folder and save the image url in xml.
Create one xml file in the name imagefile.xml
Initially add an gridview ,button and one link button in client side ,Hence Coding will be look like below……
<div>
    <table cellpadding ="0" cellspacing ="0" width="500" align="center" >
    <tr>
    <td height="100">
     
    </td>
   
    </tr>
    <tr>
    <td>
    Name:
    </td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </td>
   
    </tr>
    <tr>
    <td>
    Image:
    </td>
    <td>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    </td>
    </tr>
    <tr>
    <td height="30">
   
    </td>
   
    </tr>
    <tr>
    <td  align="justify"  >
   
        <asp:Button ID="Button1" runat="server" Text="ADD" />
    </td>
    <td>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </td>
    </tr>
    <tr>
    <td colspan ="2">
    <asp:Button ID="Button2" runat="server" Text="Show all" />
    </td>
       
    </tr>
    <tr>
    <td colspan ="2">
        <asp:GridView ID="imggrid" runat="server" AutoGenerateColumns ="false" Width="250px" DataKeyNames ="ID">
        <Columns>
        <asp:TemplateField HeaderText ="id">
         <ItemTemplate>
        <%#Eval("ID")%>
        </ItemTemplate>
              
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Name" >
        <ItemTemplate>
        <%#Eval("imagename")%>
        </ItemTemplate>
       
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Image">
        <ItemTemplate >
            <asp:Image ID="Image1" runat="server" ImageUrl ='<%#eval("URL") %>' height="100px" Width="100px" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField Headertext="Delete">
        <ItemTemplate >
                    <asp:LinkButton ID="lnkdelete" runat="server" CommandName ="Delete">Delete</asp:LinkButton>
        </ItemTemplate>
       
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
       
   
    </td>
   
    </tr>
    <tr>
    <td>
  
       
    </td>
      
    </tr>
   
    </table>
    </div>


Xml structure should be like below…

<?xml version="1.0" encoding="utf-8"?>
<picture>
</picture>

In the client side write the codeing in load event

        Dim filename As String
        Dim imgext As String
        Dim imgpath As String
        Dim y As String
        Dim i As String

        xmlpath = Server.MapPath("~/imagefile.xml")
        If FileUpload1.HasFile Then
            imgext = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1).ToLower


            filename = TextBox1.Text
            filename = FileUpload1.FileName

            If (imgext = "jpg" Or imgext = "gif" Or imgext = "bmp") Then
                imgpath = "~/image/" + filename
                FileUpload1.SaveAs(Server.MapPath("~/image/") + filename)
                'Label1.Text = Server.MapPath("~/image/") + filename
            Else
                Label1.Text = "update only jpg,gif and bmp file"
            End If
            Dim xmldoc As New XmlDocument
            xmldoc.Load(xmlpath)
            Dim max As String

            Dim nav As XPathNavigator = xmldoc.CreateNavigator()
            Dim sort As XPathExpression = nav.Compile("picture/image")
            sort.AddSort("ID", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number)
            max = nav.SelectSingleNode(sort).SelectSingleNode("ID").Value
            max += 1

            Dim root As XmlNode = xmldoc.DocumentElement
            Dim page As XmlElement = xmldoc.CreateElement("image")

            Dim ID As XmlElement = xmldoc.CreateElement("ID")
            Dim textid As XmlText = xmldoc.CreateTextNode("")
            textid.Value = max.ToString

            Dim imagename As XmlElement = xmldoc.CreateElement("imagename")
            Dim textImageName As XmlText = xmldoc.CreateTextNode("")
            textImageName.Value = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)

            Dim URL As XmlElement = xmldoc.CreateElement("URL")
            Dim textURL As XmlText = xmldoc.CreateTextNode("")
            textURL.Value = imgpath

            Dim CreateDate As XmlElement = xmldoc.CreateElement("CreateDate")
            Dim textCreateDate As XmlText = xmldoc.CreateTextNode("")
            textCreateDate.Value = DateTime.Now.ToString().Trim()

            root.AppendChild(page)
            page.AppendChild(ID)
            ID.AppendChild(textid)

            page.AppendChild(imagename)
            imagename.AppendChild(textImageName)

            page.AppendChild(URL)
            URL.AppendChild(textURL)

            page.AppendChild(CreateDate)
            CreateDate.AppendChild(textCreateDate)
            xmldoc.Save(xmlpath)
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ds As New dataset
        ds.ReadXml(Server.MapPath("imagefile.xml"))
        imggrid.DataSource = ds
        imggrid.Columns(0).Visible = True
        imggrid.DataBind()
        imggrid.Columns(0).Visible = False
    End Sub


    Protected Sub imggrid_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles imggrid.RowDeleting
        Dim id As String
        xmlpath = Server.MapPath("~/imagefile.xml")
        id = imggrid.DataKeys(e.RowIndex).Value.ToString
        Dim xmldoc As New XmlDocument
        xmldoc.Load(xmlpath)
        Dim child As XmlNode = xmldoc.SelectSingleNode("picture/image[ID='" & id & "']")
        xmldoc.DocumentElement.RemoveChild(child)
        xmldoc.Save(xmlpath)

    End Sub


So when upload a image that image information should be added in xml file look below…
<?xml version="1.0" encoding="utf-8"?>
<picture>

<image>
    <ID>2</ID>
    <imagename>73546383.jpg</imagename>
    <URL>~/image/73546383.jpg</URL>
    <CreateDate>16-07-2011 1:24:17 PM</CreateDate>
  </image>
</picture>
I hope  this one will be helpful to all………

Monday, July 25, 2011

Ping your blog with googleblog


hi..
    In this article we discuss about to add a blog or ping your blog content with googleblog by easy way..Its very easy update your blog with google search engine.once you added your sitein gogle blog then it will index in google search index..Hope this one helpful to those who seek to ping your with google...
Click the below link to add your blog with google...
http://blogsearch.google.com/ping

Friday, July 22, 2011

How to update,delete and edit in datalist control using asp.net


Hi..
In this article we discuss about to update, delete and edit data in datalist control. In an previous article we discuss about update, delete in datagrid.for your reference I mention link below…
Here first you can add a datalist control in an client seide..Check the below code for your refrence ..
The main difference datagrid and datalist.in datalist we can design table as per our own style…we can bind data horizontal or vertical, we can bind column repeated direction..this are all we can’t do in datagrid..Now we come to datalist updation…


<div>
    <table cellpadding ="0" cellspacing ="0" width="1000">
    <tr>
    <td align="center">
        <asp:DataList ID="DataList1" runat="server" DataKeyField ="Number" class="list">
        <HeaderTemplate >
        <table class="list">
        <tr>
        <td>
        Number
        </td>
        <td>
        Name
        </td>
        <td>
        Address
        </td>
        <td>
        City
        </td>
        <td>
        State
        </td>
        <td>
        operation
        </td>
        </tr>
         </HeaderTemplate>
         <ItemTemplate >
         <tr>
         <td>
         <%#Eval("Number")%>
                  </td>
         <td>
         <%#Eval("Name")%>
         </td>
         <td>
         <%#Eval("Address")%>
                  </td>
         <td>
         <%#Eval("City")%>
         </td>
         <td>
         <%#Eval("State")%>
         </td>
         <td>
          <asp:LinkButton ID="LinkButton4" runat="server" CommandName ="edit">edit</asp:LinkButton>
                <asp:LinkButton ID="LinkButton2" runat="server" CommandName ="delete">Delete</asp:LinkButton>

         </td>
          </tr>
         </ItemTemplate>
         <EditItemTemplate>
         <tr>
        <td>
            <asp:TextBox ID="TextBox1" runat="server" Text=' <%#Eval("Number")%>' ReadOnly ="true" ></asp:TextBox>
                </td>
        <td>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Name")%>'></asp:TextBox>
        </td>
        <td>
                            <asp:TextBox ID="TextBox3" runat="server" Text='  <%#Eval("Address")%>'></asp:TextBox>
        </td>
        <td>
                                    <asp:TextBox ID="TextBox4" runat="server" Text=' <%#Eval("State")%>'></asp:TextBox>
        </td>
         <td>
                                            <asp:TextBox ID="TextBox5" runat="server" Text='<%#Eval("City")%>'></asp:TextBox>
             <asp:LinkButton ID="LinkButton1" runat="server" CommandName ="update">Update</asp:LinkButton>
          <asp:LinkButton ID="LinkButton3" runat="server" CommandName ="Cancel">Cancel</asp:LinkButton>
         </td>
         </tr>
        
        
         </EditItemTemplate>
        <FooterTemplate >
         </table>
        </FooterTemplate>
        </asp:DataList>
    </td>
    </tr>
    </table>
    </div>

Then follow the below code for an server side coding for datalist control

Sub bind()
        Dim query As String
        query = "select Number,Name,Address,City,State from phone"
        sqlcmd = New SqlCommand(query, sqlcon)
        sqlcon.Open()
        da = New SqlDataAdapter(sqlcmd)
        sqlcon.Close()
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            DataList1.DataSource = dt
            DataList1.DataBind()
        End If
    End Sub
    Protected Sub DataList1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.CancelCommand
        DataList1.EditItemIndex = -1
        bind()
    End Sub

    Protected Sub DataList1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.DeleteCommand
        Dim strqry As String
        Dim number As String
        number = DataList1.DataKeys(e.Item.ItemIndex)

        strqry = "Delete from phone where number='" & number & "'"
        sqlcmd = New SqlCommand(strqry, sqlcon)
        sqlcmd.CommandType = CommandType.Text
        sqlcon.Open()
        sqlcmd.ExecuteNonQuery()
        sqlcon.Close()
        bind()
    End Sub

    Protected Sub DataList1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.EditCommand
        DataList1.EditItemIndex = e.Item.ItemIndex
        bind()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            bind()
        End If
    End Sub

    Protected Sub DataList1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.UpdateCommand
        Dim number, name, address, city, state As TextBox
        number = CType(e.Item.FindControl("TextBox1"), TextBox)
        name = CType(e.Item.FindControl("TextBox2"), TextBox)
        address = CType(e.Item.FindControl("TextBox3"), TextBox)
        city = CType(e.Item.FindControl("TextBox4"), TextBox)
        state = CType(e.Item.FindControl("TextBox5"), TextBox)
        Dim sqlstr As String
        sqlstr = "update phone set Name=@name,Address=@address,City=@city,State=@state where Number=@number"
        sqlcmd = New SqlCommand(sqlstr, sqlcon)
        sqlcmd.Parameters.Add("@name", SqlDbType.VarChar).Value = name.Text

        sqlcmd.Parameters.Add("@address", SqlDbType.VarChar).Value = address.Text
        sqlcmd.Parameters.Add("@number", SqlDbType.BigInt).Value = number.Text
        sqlcmd.Parameters.Add("@city", SqlDbType.VarChar).Value = city.Text
        sqlcmd.Parameters.Add("@state", SqlDbType.VarChar).Value = state.Text
        sqlcon.Open()
        sqlcmd.ExecuteNonQuery()
        sqlcon.Close()
        DataList1.EditItemIndex = -1
        bind()
    End Sub

I hope this one helpful to all..
Plz post your valuable comments…..