Contents of bupdate2.aspx:
<%@ Page Language="VB" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <%@ import Namespace="System.Text.RegularExpressions" %> <script runat="server"> Sub Page_Load(sender As System.Object, e As System.EventArgs) End Sub Sub FindAcct(ByVal sender As System.Object, ByVal e As System.EventArgs) ' ' This sub attempts to find the account to be updated ' ' The page is tested first for validity of the input data (Page.IsValid) before any read occurs if Page.IsValid then Dim strFind as String strFind = "Select * from glmaster Where " & _ " major=" + mjr.Text & " AND" & _ " minor=" + mnr.Text & " AND" & _ " sub1="+ s1.Text & " AND" & _ " sub2="+ s2.Text Try Dim c as Integer Dim strConnect as String = "server=AUCKLAND;uid=gl001;pwd=11111;database=gl001" Dim objConnect as new SqlConnection(strConnect) objConnect.Open() Dim objCommand as new SqlCommand(strFind, objConnect) Dim objDataReader as SqlDataReader objDataReader = objCommand.ExecuteReader() ' ' find the acct and load up the current values in the textboxes ' c=0 Do while objDataReader.Read() mjr.Text=objDataReader("major") mnr.Text=objDataReader("minor") s1.Text=objDataReader("sub1") s2.Text=objDataReader("sub2") ad.Text=objDataReader("acctdesc") c=c+1 Loop results.innerHTML="Found "+CStr(c)+" rows" if c=1 then adbox.visible=true findbox.visible=false upbox.visible=true clearbox.visible=false else adbox.visible=false findbox.visible=true upbox.visible=false clearbox.visible=false end if objConnect.Close() Catch exc as exception results.InnerHtml = "<b>*** SQL Error while Searching for the account</b>.<br />" _ + exc.Message + "<br />" + exc.Source End Try else ' ' This code returns nothing because the Page.IsValid is NOT true and the user ' must correct the data error ' results.InnerHtml="" end if End Sub Sub updateAcct(ByVal sender As System.Object, ByVal e As System.EventArgs) ' The page is tested for validity (Page.IsValid) before any update occurs results.InnerHtml="" if Page.IsValid then Dim strUpdate as String strUpdate = "Update glmaster set acctdesc= "+ Chr(39) & _ ad.Text & CHR(39) & " WHERE " & _ " major=" + mjr.Text & " AND" & _ " minor=" + mnr.Text & " AND" & _ " sub1="+ s1.Text & " AND" & _ " sub2="+ s2.Text Try Dim numa as Integer Dim strConnect as String = "server=AUCKLAND;uid=gl001;pwd=11111;database=gl001" Dim objConnect as new SqlConnection(strConnect) objConnect.Open() Dim objCommand as new SqlCommand(strUpdate, objConnect) numa=objCommand.ExecuteNonQuery() results.innerHTML="Updated "+CStr(numa)+" rows" clearbox.visible=true upbox.visible=false objConnect.Close() Catch exc as exception results.InnerHtml = "<b>* Error while Updating the Account</b>.<br />" _ + exc.Message + "<br />" + exc.Source End Try else results.InnerHtml="" end if end sub Sub ClearForm(ByVal sender As System.Object, ByVal e As System.EventArgs) mjr.Text="" mnr.Text="" s1.Text="" s2.Text="" ad.Text="" results.InnerHtml="" adbox.visible=false findbox.visible=true upbox.visible=false clearbox.visible=false End Sub </script> <html> <head> </head> <body> <center> <table border="1"> <tbody> <tr> <td> <img src="captsm.gif" /></td> <td valign="center" align="middle" bgcolor="#aaaaaa"> <font face="COMIC SANS MS"><font size="4"><b> Simple Update in VB.NET with Validation <hr /> Modifies an Account Description in <i>glmaster</i> </b></font></font></td> </tr> </tbody> </table> </center> <form id="Form1" method="post" runat="server"> <p> <asp:Label id="Label1" runat="server" Font-Bold="True">Enter the Account Number to Modify Description:</asp:Label> </p> <p> Major <asp:TextBox id="mjr" MaxLength="4" columns="4" Runat="server"></asp:TextBox> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" EnableClientScript="False" Width="208px" Display="Dynamic" ControlToValidate="mjr" ErrorMessage="Major Account Code is Required"></asp:RequiredFieldValidator> <asp:RangeValidator id="RangeValidator1" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="mjr" ErrorMessage="Major Account must be between 0 and 9999" MinimumValue="0" MaximumValue="9999" Type="Integer"></asp:RangeValidator> <asp:CompareValidator id="CompareValidator1" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="mjr" ErrorMessage="Must be digits 0-9 only" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator> <br /> Minor <asp:TextBox id="mnr" MaxLength="4" columns="4" Runat="server"></asp:TextBox> <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="mnr" ErrorMessage="Minor Account Code is Required"></asp:RequiredFieldValidator> <asp:RangeValidator id="RangeValidator2" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="mnr" ErrorMessage="Minor Account must be between 0 and 9999" MinimumValue="0" MaximumValue="9999" Type="Integer"></asp:RangeValidator> <asp:CompareValidator id="CompareValidator2" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="mnr" ErrorMessage="Must be digits 0-9 only" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator> <br /> Sub 1 <asp:TextBox id="s1" MaxLength="4" columns="4" Runat="server"></asp:TextBox> <asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="s1" ErrorMessage="Sub Account1 is required"></asp:RequiredFieldValidator> <asp:RangeValidator id="RangeValidator3" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="s1" ErrorMessage="Sub Account 1 must be between 0 and 9999" MinimumValue="0" MaximumValue="9999" Type="Integer"></asp:RangeValidator> <asp:CompareValidator id="CompareValidator3" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="s1" ErrorMessage="Must be digits 0-9 only" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator> <br /> Sub 2 <asp:TextBox id="s2" MaxLength="4" columns="4" Runat="server"></asp:TextBox> <asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="s2" ErrorMessage="Sub Account 2 is required"></asp:RequiredFieldValidator> <asp:RangeValidator id="RangeValidator4" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="s2" ErrorMessage="Sub Account2 must be between 0 and 9999" MinimumValue="0" MaximumValue="9999" Type="Integer"></asp:RangeValidator> <asp:CompareValidator id="CompareValidator4" runat="server" EnableClientScript="False" Display="Dynamic" ControlToValidate="s2" ErrorMessage="Must be digits 0-9 only" Operator="DataTypeCheck" Type="Integer"></asp:CompareValidator> <br /> <!-- there are four panels below "adbox" panel holds the account decsription textbox for the modify (this is visible on the 2nd and 3rdt passes) "upbox" panel holds the update button (this is visible on the second pass) "clearbox" panel holds the clear button (this is visible on the third pass) "findbox" panel holds the find account button (this is visible on the first pass) --> <asp:panel id="adbox" runat="server" visible="false"> Account Description <asp:TextBox id="ad" MaxLength="50" columns="40" Runat="server"></asp:TextBox> </asp:panel> </p> <p> <asp:panel id="findbox" runat="server" visible="true"> <asp:Button id="btnAdd" onclick="FindAcct" Runat="server" Text="Find Account"></asp:Button> </asp:panel> </p> <asp:panel id="upbox" runat="server" visible="false"> <asp:Button id="btnUpdate" onclick="updateAcct" Runat="server" Text="Update Account Decsription"></asp:Button> </asp:panel> <asp:panel id="clearbox" runat="server" visible="false"> <asp:Button id="btnClear" onclick="ClearForm" Runat="server" Text="Update Another"></asp:Button> </asp:panel> </form> <p> </p> <p> <!--- all the server reuslts are shown in the results div block below ---> <font color="#ff0000"> <div id="results" runat="server"> </div> </font> </p> </body> </html>
***** end of file *** printed: 232 lines on: 11/23/2009 6:05:12 AM