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








Saturday, December 17, 2011

Deleting selected rows from gridview using checkbox in vb.net

This program will show how to deleting selected rows from gridview using checkbox in vb.net

Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page
    Public temptable As DataTable
    Public ds As DataSet
    Public editrow As DataRow
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        temptable = Session("cart")
        editrow = temptable.NewRow
        editrow("Item_Name") = TextBox1.Text
        temptable.Rows.Add(editrow)
        Session("cart") = temptable
        GridView1.DataSource = temptable
        GridView1.DataBind()
    End Sub
'function for filling datatable
    Public Function makecart()
        Try
            temptable = New DataTable("cart")
            temptable.Columns.Add("id", GetType(Integer))
            temptable.Columns("id").AutoIncrement = True
            temptable.Columns("id").AutoIncrementSeed = 1
            temptable.Columns.Add("Item_Name", GetType(String))

            'put table in session
            Session("cart") = temptable

        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            makecart()

        End If
    End Sub

    'delete selected items
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        temptable = Session("cart")
        For i As Integer = 0 To temptable.Rows.Count - 1
            If CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True Then
                temptable.Rows(i).Delete()
            Else
                Dim a As Integer = 0
            End If
        Next
        Session("cart") = temptable
        GridView1.DataSource = temptable
        GridView1.DataBind()
    End Sub

    'selecting all at a time this handel check box in header row  
    Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.RowCommand
        If CType(GridView1.HeaderRow.FindControl("CheckBox2"), CheckBox).Checked = True Then
            For i As Integer = 0 To GridView1.Rows.Count - 1
                CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = True
            Next
        Else
            For i As Integer = 0 To GridView1.Rows.Count - 1
                CType(GridView1.Rows(i).FindControl("CheckBox1"), CheckBox).Checked = False

            Next
        End If
   End Sub
End Class

Thursday, December 15, 2011

Java Script for Confirm Box or (yes/no) box

Have Fun
<input id="Button3" onclick="return confirm('Are You Sure?');" type="button" value="button" />
Eg.

Creating Shopping Cart in vb.net Through DataTable

Imports System.Data
Imports System.Data.SqlClient

Partial Class neworeder
Inherits System.Web.UI.Page
Public con As SqlConnection
Public temptable As DataTable
Public da As SqlDataAdapter
Public ds As DataSet
Public editrow As DataRow
Public cmd As SqlCommand

' function for making column of table
Public Function makecart()
Try
temptable = New DataTable("cart")
temptable.Columns.Add("id", GetType(Integer))
temptable.Columns("id").AutoIncrement = True
temptable.Columns("id").AutoIncrementSeed = 1
temptable.Columns.Add("Item_Name", GetType(String))
temptable.Columns.Add("Item_Quantity", GetType(String))

temptable.Columns.Add("Punched_Date", GetType(String))
temptable.Columns.Add("Item_Unit", GetType(String))


temptable.Columns.Add("Comments", GetType(String))
'put table in session
Session("cart") = temptable

Catch ex As Exception
Response.Write(ex.Message)
End Try
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
makecart()

txtdate.Text = Now.Date.ToShortDateString

End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Try

temptable = Session("cart")
Dim imatch As Boolean = False
For Each editrow In temptable.Rows
'checking duplicate item
If editrow("Item_Name") = ddlmeterial.SelectedItem.Text Then
If editrow("Item_Unit") = "Tons" And ddlunit.SelectedItem.Text = "Kg" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) + Convert.ToInt32(editrow("Item_Quantity")) * 1000).ToString
editrow("Item_Unit") = "Kg"
ElseIf editrow("Item_Unit") = "Kg" And ddlunit.SelectedItem.Text = "Tons" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) * 1000 + Convert.ToInt32(editrow("Item_Quantity"))).ToString
editrow("Item_Unit") = "Kg"
ElseIf editrow("Item_Unit") = "Kg" And ddlunit.SelectedItem.Text = "Kg" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) + Convert.ToInt32(editrow("Item_Quantity"))).ToString
ElseIf editrow("Item_Unit") = "Tons" And ddlunit.SelectedItem.Text = "Tons" Then
editrow("Item_Quantity") = (Convert.ToInt32(txtquan.Text) + Convert.ToInt32(editrow("Item_Quantity"))).ToString
End If

imatch = True
End If
Next
If Not imatch Then
editrow = temptable.NewRow
editrow("Item_Name") = ddlmeterial.SelectedItem.Text
editrow("Item_Quantity") = txtquan.Text.Replace("'", "`").ToString
editrow("Punched_Date") = txtdate.Text
editrow("Item_Unit") = ddlunit.SelectedItem.Text
editrow("Comments") = txtcomment.Text

temptable.Rows.Add(editrow)

End If
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()

Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Try
temptable = Session("cart")
'delete at specific row
temptable.Rows(e.RowIndex).Delete()
Session("cart") = temptable
GridView1.DataSource = temptable
GridView1.DataBind()

Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub

Tuesday, November 15, 2011

Code For Null Value Exception in vb.net

This is the code for null value exception in vb.net :-

Enum enumObjectType
StrType = 0
IntType = 1
DblType = 2
End Enum

Public Function CheckDBNull(ByVal obj As Object, _
Optional ByVal ObjectType As enumObjectType = enumObjectType.StrType) As Object
Dim objReturn As Object
objReturn = obj
If ObjectType = enumObjectType.StrType And IsDBNull(obj) Then
objReturn = ""
ElseIf ObjectType = enumObjectType.IntType And IsDBNull(obj) Then
objReturn = 0
ElseIf ObjectType = enumObjectType.DblType And IsDBNull(obj) Then
objReturn = 0.0
End If
Return objReturn
End Function

you just need to call "CheckDBNull()" Function

like eg:

"If (CheckDBNull(Session("test")) = "") Then

End If