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

Monday, October 24, 2011

Sending Email From Your Gmail Account in vb.net


Use these namespace and add function to your event on which you want to send email.
*****************************************************************
Imports System.Web
Imports System.Net
Imports System.Net.Mail

Public Function sndmail()
Try

 Dim client As New SmtpClient()
               Dim sendTo As New MailAddress("email of other person")
                Dim from As MailAddress = New MailAddress("youremail")
                Dim message As New MailMessage(from, sendTo)
                message.IsBodyHtml = True
                message.Subject = "Mail subject"
                message.IsBodyHtml = True
                Dim st As String = "Your Mail Body "              
                message.Body = st
                message.IsBodyHtml = True
                Dim basicAuthenticationInfo As New System.Net.NetworkCredential("yourfullemail", "password")
                client.Host = "smtp.gmail.com"
                client.UseDefaultCredentials = False
                client.Credentials = basicAuthenticationInfo
                client.EnableSsl = True
                client.Send(message)

         Catch ex As Exception
            Response.Write("<script>alert('send fail');</script>")
        End Try
End Function


******************************************************************

Friday, September 2, 2011

Printing and updating the inner text of a xml node in vb.net

Hi guys this is the code for printing and updating the inner text of node in a xml file through vb.net code which i used update my xml file name as content.xml
****************************
content.xml
*************************
<?xml version="1.0" encoding="utf-8"?>
<main>
  <node1 id="1">my old text</node1>
</main>


******************************



This function is to print inner text of a xml node

Public Function shw()

Dim strxmlpath As String = Server.MapPath("content.xml")
            Dim doc As New XmlDocument
            doc.Load(strxmlpath)
            Dim elem As XmlNode = doc.DocumentElement.FirstChild
Response.Write(elem.InnerText)
             doc.Save(strxmlpath)

End Function


This function is to update inner text of a xml file

Public Function updt()
 Dim strxmlpath As String = Server.MapPath("content.xml")
            Dim doc As New XmlDocument
            doc.Load(strxmlpath)
            Dim node As XmlNode = doc.DocumentElement.FirstChild
            node.InnerText = " enter your new text"


            doc.Save(strxmlpath)
End Function














Wednesday, August 31, 2011

Deleting Selected row from gridview in asp.net, vb.net

1.add a buttonfield in grid view set command name as "Delete" then add this code

Imports System.Data
Imports System.Data.OleDb
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.IO

Partial Class _Default
    Inherits System.Web.UI.Page
    Public con As OleDbConnection
    Public cmd As OleDbCommand
    Public da As OleDbDataAdapter
    Public dr As OleDbDataReader
    Public ds As DataSet
    Public cmb As OleDbCommandBuilder



Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("youraccessdatabase/db.mdb") & "")
        con.Open()
        da = New OleDbDataAdapter("select * from cust_details order by ID desc", con)
        ds = New DataSet()
        cmb = New OleDbCommandBuilder(da)
        da.Fill(ds)
        ds.Tables.Item(0).Rows.Item(e.RowIndex).Delete()
        da.Update(ds)
        GridView1.DataSource = ds
        GridView1.DataBind()
    End Sub

End Class

Saturday, August 27, 2011

Creating a temporary table in vb.net and binding to a gridview

This is a example that how to create temporary table in  vb.net

**************************
Imports System.Data



Partial Class _Default
    Inherits System.Web.UI.Page
    Public table1 As DataTable
    Public ds As DataSet

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

    End Sub
    Public Function table()
        Try
            table1 = New DataTable
            table1.Columns.Add("rollno", Type.GetType("System.Int32"))
            table1.Columns.Add("name", Type.GetType("System.String"))
            table1.Columns.Add("result", Type.GetType("System.String"))

            Dim editrow As DataRow
            editrow = table1.NewRow
            editrow(0) = 41
            editrow(1) = "lalit kumar"
            editrow(2) = "Pass"
            table1.Rows.Add(editrow)
            ds = New DataSet
            ds.Tables.Add(table1)
            GridView1.DataSource = ds
            GridView1.DataBind()

        Catch ex As Exception
            Response.Write(ex.Message)

        End Try
    End Function
End Class

****************************

Thursday, August 25, 2011

Print A Web Page Javascript


It is just a small function name window.print() you need to call on a button click:-
<input type="button" name="Print" value="Print" onclick="window.print();" />






No need to write anything else

Wednesday, August 24, 2011

General Page Life-cycle Stages

In general terms, the page goes through the stages outlined in the following table. In addition to the page life-cycle stages, there are application stages that occur before and after a request but are not specific to a page..




Stage Description
Page request The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.
Start In the start step, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostback property. Additionally, during the start step, the page's UICulture property is set.
Page initialization During page initialization, controls on the page are available and each control's UniqueID property is set. Any themes are also applied to the page. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.
Load During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Validation During validation, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
Postback event handling If the request is a postback, any event handlers are called.
Rendering Before rendering, view state is saved for the page and all controls. During the rendering phase, the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream of the page's Response property.
Unload Unload is called after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and any cleanup is performed.

Thursday, August 18, 2011

Welcome






Hello friends my name Lalit Kumar and this my blog to update my recent achievements. First of all i want to tell something about me .

If i talk about my educational background i have completed BCA,MCA and certification in .net 3.5.

In concern to my professional background from two years i am in this industry and currently i am working with Dreamtree Educare Pvt. Ltd..
MY key skills are c#,vb.net,xml,ms-sql,html,css,java,javascript and i am still trying to improve myself.
Here are some live projects i have worked.