Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
[prev. Lesson]  DataGrid: Customize Column Names and Value
     [next Lesson]  C# - DataGrid Editing

DataGrid Editing - VB.net by Charles Carroll

This page demonstrates how basic datagrid editing can be acoomplished!

   filename=/experiments/datagridedit/publishers_vb.aspx

<Test Script Below>


<%@ Trace="true" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
dim strConn as string

sub Page_Load(S as Object, E as EventArgs)
        strConn="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
        strConn &= server.mappath("/experiments/data/biblio.mdb") & ";" 
    If ispostback=false
        FillUpGrid()
    end if
end sub

sub FillUpGrid()
    Dim Conn as OLEDBConnection
    Dim Rdr as OLEDBDataReader
    TRY
        Dim strSQL as string
        strSQL="select * from publishers where state='NY'"
        Conn=New OLEDBConnection(strConn)
        Dim Cmd as New OLEDBCommand(strSQL,Conn)
        Conn.Open()
        Rdr=Cmd.ExecuteReader()
        d1.DataSource = Rdr
        d1.DataBind()
    CATCH exc1 as exception
       litExc.text=exc1.ToString()
    FINALLY
        IF Not(rdr Is Nothing)
            IF rdr.IsClosed=false THEN Rdr.Close()
        End If
        If not(conn is Nothing)
            IF Conn.State=System.Data.Connectionstate.Open THEN Conn.Close()
        End If
    END TRY
end sub

sub DataEdit(S as Object, E as DataGridCommandEventArgs)
    D1.EditItemIndex=Cint(E.Item.ItemIndex)
    FillUpGrid()
end sub


sub DataUpdate(S as Object, E as DataGridCommandEventArgs)
'    D1.EditItemIndex=Cint(E.Item.ItemIndex)
'    FillUpGrid()
    
    dim txtCompanyName as TextBox=E.Item.Cells(2).Controls(0)
        dim strCompanyName as string = txtCompanyName.text

    Dim txtCompName as TextBox=E.Item.Cells(3).Controls(0)
    Dim txtPhoneNum as TextBox=E.Item.Cells(4).Controls(0)
    Dim txtFaxNum as TextBox=E.Item.Cells(5).Controls(0)
    dim txtAddress as TextBox=E.Item.Cells(6).Controls(0)
    dim txtCity as TextBox=E.Item.Cells(7).Controls(0)
    dim txtState as TextBox=E.Item.Cells(8).Controls(0)
    dim txtZip as TextBox=E.Item.Cells(9).Controls(0)
    dim txtComments as TextBox=E.Item.Cells(10).Controls(0)
    
    '  Update publishers set Name='yyy',Company Name='Prentice Hall', Fax='555-1212' where PUBID=56
    
    dim strSQL as string=""
    strSQL &="Update publishers set "
    strSQL &="[Company Name]='" & strCompanyName & "', "
    strSQL &="Name='" & txtCompName.Text & "', "
    strSQL &="Telephone='" + txtPhoneNum.Text & "', "
    strSQL &="Fax='" & txtFaxNum.Text & "', "
    strSQL &="Address='" + txtAddress.Text & "', "
    strSQL &="City='" & txtCity.Text & "', "
    strSQL &="State='" & txtState.Text & "', "
    strSQL &="Zip='" & txtZip.Text & "', "
    dim strComments as string=""
    if txtComments.Text=""
        strComments="Null"
    else
        strComments=strComments.Replace("'","''")
        strComments="'" & txtComments.Text & "'"
    end if    
    strSQL &= "Comments=" & strComments & " "
    strSQL &= " Where PubID=" & D1.DataKeys(E.Item.ItemIndex)
    Trace.Write ("strSQL", strSQL)

    Dim Conn as OLEDBConnection
    
    try
        Conn=new OleDbConnection(strConn)
        dim Cmd as new OleDbCommand(strSQL,Conn)
        Conn.Open()
        Cmd.ExecuteNonQuery()
    catch exc1 as Exception
        Trace.Write("bad things happened","wow",exc1)
    finally
         If not(conn is Nothing)
                IF Conn.State=System.Data.Connectionstate.Open THEN Conn.Close()
         End If
    end try

    D1.EditItemIndex=-1
    FillUpGrid()
end sub

sub DataCancel(S as Object, E as DataGridCommandEventArgs)
    D1.EditItemIndex=-1
    FillUpGrid()
end sub
</script>
<html><head><title>Publishers Edit Sample</title></head>
<body bgcolor="#FFFFFF">
<form runat="server">
<font face ="Verdana"<h3>Publishers Edit Sample</h3></font>
<asp:literal id="litExc" runat="server" />

<ASP:datagrid id="D1" autogeneratecolumns="false" runat="server"
	OnEditCommand="DataEdit" 
	OnCancelCommand="DataCancel" 
	OnUpdateCommand="DataUpdate" 
	DataKeyField="PubID"
	>

    <columns>
		<asp:EditCommandColumn ButtonType="LinkButton" 
			EditText="Edit&nbsp;Record" UpdateText="Save&nbsp;Record" CancelText="Cancel&nbsp;Edit" />
    
        <asp:BoundColumn HeaderText="ID" ReadOnly="True" DataField="PubID"/>
        <asp:BoundColumn HeaderText="Company Name" DataField="Company Name"/>
        <asp:BoundColumn HeaderText="Name" DataField="Name"/>
        
        <asp:BoundColumn HeaderText="Phone#" DataField="Telephone">
            <ItemStyle Width="300px" />
        </asp:boundcolumn>
        
        <asp:BoundColumn HeaderText="Fax#" DataField="Fax">
            <ItemStyle Width="300px" />
        </asp:boundcolumn>
        
        <asp:BoundColumn HeaderText="Street Address" DataField="Address"/>
        <asp:BoundColumn HeaderText="City" DataField="City"/>
        <asp:BoundColumn HeaderText="St." DataField="State"/>
        <asp:BoundColumn HeaderText="Zip Code" DataField="Zip"/>
        <asp:BoundColumn HeaderText="Notes About This Publisher" DataField="Comments"/>
    
    </columns>

</ASP:datagrid>



</form>
</body></html>
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.