Switch to full style
You can find discussions, solutions and codes related to ASP.NET
Post a reply

Asp.net insert update delete Examples

Sun Jul 18, 2010 5:01 am

ASP.NET insert, update, delete commands examples
===========Class1.vb==============
Code:

Imports Microsoft
.VisualBasic
Imports System
.Data.SqlClient
Imports System
.Data


Public Class Class1

    Dim objcon As New SqlConnection
(ConfigurationManager.ConnectionStrings("student").ConnectionString)
    Dim objadp As SqlDataAdapter
    Dim ds As Data
.DataSet
    Dim cmd As SqlCommand


    Public Function selectdata
(ByVal qry As String) As DataSet

        objcon
.Open()
        objadp = New SqlDataAdapter(qry, objcon)
        ds = New Data.DataSet
        objadp
.Fill(ds)
        objcon.Close()
        Return ds

    End Function


    Public Function qryexe
(ByVal qry As String) As Boolean
        objcon
.Open()
        cmd = New SqlCommand(qry, objcon)
        Dim i As Integer = cmd.ExecuteNonQuery()
        objcon.Close()
        If i = 0 Then
            Return False
        Else
            Return True
        End If

    End Function
End Class
 
====================Default.aspx.vb========================
Code:

Partial Class _Default
    Inherits System
.Web.UI.Page

    Dim qry As String
    Dim ds As New System
.Data.DataSet
    Dim obj As New Class1
    Shared flag As Boolean 
= False
    Shared chk As Boolean

    Protected Sub Button1_Click
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        chk 
= True
    End Sub

    Protected Sub Button2_Click
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        chk 
= False
    End Sub

    Protected Sub Button3_Click
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        qry 
= "delete from stud where name='" + DropDownList1.SelectedItem.Text + "'"
        obj.qryexe(qry)
        flag = False
        Page_Load
(sender, e)
        trno.Text = ""
        tname.Text = ""
        tresult.Text = ""
    End Sub

    Public Sub display
()
        qry = "select * from stud where name='" + DropDownList1.SelectedItem.Text + "'"
        ds = obj.selectdata(qry)
        trno.Text = ds.Tables(0).Rows(0).Item("rno").ToString()
        tname.Text = ds.Tables(0).Rows(0).Item("name")
        tresult.Text = ds.Tables(0).Rows(0).Item("result")
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged
(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        ds
.Clear()
        display()
    End Sub

   

    Protected Sub Page_Load
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If flag 
= False Then
            ds
.Clear()
            qry = "select * from stud"
            ds = obj.selectdata(qry)
            DropDownList1.DataSource = ds
            DropDownList1
.DataTextField = ds.Tables(0).Columns("name").ToString()
            DropDownList1.DataValueField = ds.Tables(0).Columns("rno").ToString()
            DropDownList1.DataBind()

            GridView1.DataSource = ds
            GridView1
.DataBind()

           

            flag 
= True
        End If
    End Sub

    Protected Sub Button4_Click
(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        If chk 
= True Then
            qry 
= "insert into stud(name,result) values('" + tname.Text.Trim + "','" + tresult.Text.Trim + "')"
            obj.qryexe(qry)
        Else
            qry 
= "update stud set name='" + tname.Text.Trim + "',result='" + tresult.Text.Trim + "' where rno=" + trno.Text.Trim
            obj
.qryexe(qry)
        End If
        flag 
= False
        Page_Load
(sender, e)
        trno.Text = ""
        tname.Text = ""
        tresult.Text = ""
    End Sub

   
End Class
 
======================
:duel:


Attachments
Class1.vb.txt
(1.76 KiB) Downloaded 943 times
Default.aspx.vb.txt
(4.38 KiB) Downloaded 1100 times

Post a reply
  Related Posts  to : Asp.net insert update delete Examples
 DML Statements, insert row, delete row     -  
 examples with c     -  
 Examples in C#     -  
 few examples of c using ascii     -  
 Examples of heading tags     -  
 Matlab basics examples     -  
 vBulletin update     -  
 insert query example     -  
 update query example     -  
 Get affected rows for an update in php     -  

Topic Tags

ASP MYSQL