Friday, November 9, 2012

Download File To Client PC From Database In VB.net

We need to save :-
Filename nvarchar(150),
FileData Image,
contentType nvarchar(max),
FileLength int

In database i am using  SQL server 2008

now just use this mehod.

Protected Sub Download_File(ByVal fileName As String, ByVal contentType As String, ByVal fileData As Byte(), ByVal fileLength As Integer)
        Try
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName.Replace(" ", "_").ToString)
            Response.ContentType = contentType
            Response.OutputStream.Write(fileData, 0, fileLength)
            Response.End()
            Response.Flush()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

This is also a good  code to remember.



Friday, June 22, 2012

Sending Mail from your domain name server

Imports System.Net
Imports System.Net.Mail
Imports System.Web
Imports System.IO
Partial Class contact
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New Net.NetworkCredential("username", "password")
        SmtpServer.Port = 25
        SmtpServer.Host = "mail.xyz.in"
        mail = New MailMessage()
        mail.From = New MailAddress("info@xyz.in")
        mail.To.Add("emailaddress for send to")
        mail.Subject = "From Aspirant"
        mail.IsBodyHtml = True
        Dim st As String = "Name : " + TextBox1.Text + Environment.NewLine + " Email : " + txtemail.Text + Environment.NewLine + " Query : " + TextBox2.Text
        mail.Body = st
        SmtpServer.Send(mail)
           End Sub
End Class

Friday, February 17, 2012

Reading And adding In Xml File Through Vb.net Code

My xml file is like:-



<?xml version="1.0" encoding="utf-8"?>
<Student>
  <Name id="1">
    <FirstName>Ajay</FirstName>
    <LastName>Gupta</LastName>
  </Name>
  <Name id="2">
    <FirstName>Sandeep</FirstName>
    <LastName>Yadav</LastName>
  </Name>
  <Name id="3">
    <FirstName>Namrata</FirstName>
    <LastName>Singh</LastName>
  </Name>
  <Name id="4">
    <FirstName>Amy</FirstName>
    <LastName>Jackson</LastName>
  </Name>
  <Name id="5">
    <FirstName>Lalit</FirstName>
    <LastName>Kumar</LastName>
  </Name>
  <Name id="6">
    <FirstName>Ranjit</FirstName>
    <LastName>Winy</LastName>
  </Name>
</Student>
***************************
For Reading



Protected Sub ShowRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowRecord.Click
        Dim xd As New XmlDocument()
        xd.Load(Server.MapPath("database/XMLFile.xml"))
        Dim node = xd.SelectSingleNode("/Student/Name[@id='" + Txtid2.Text + "']")
        If node IsNot Nothing Then
            TxtFN2.Text = node.ChildNodes(0).InnerText
            TxtLN2.Text = node.ChildNodes(1).InnerText
            div1.Visible = True
        Else
            ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "clientscript", "alert('Record Not Found.')", True)
        End If
End Sub






For Adding :-



Protected Sub AddRecord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRecord.Click
        Dim newStudent As String = "<Name id=""" + Txtid1.Text + """>" & Environment.NewLine & " " & _
        "   <FirstName>" + TxtFN1.Text + "</FirstName>" & Environment.NewLine & " " & _
        "   <LastName>" + TxtLN1.Text + "</LastName>" & Environment.NewLine & " " & _
        "</Name>"
        Dim xd As New XmlDocument()
        xd.Load(Server.MapPath("database/XMLFile.xml"))
        Dim DocFrag As XmlDocumentFragment = xd.CreateDocumentFragment()
        DocFrag.InnerXml = newStudent
        Dim Root As XmlNode = xd.DocumentElement
        Root.AppendChild(DocFrag)
        xd.Save(Server.MapPath("database/XMLFile.xml"))
        xd = New XmlDocument()
        xd.Load(Server.MapPath("database/XMLFile.xml"))
        ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "clientScript", "alert('Record Successfully Saved')", True)


    End Sub