<!--This is how you write comments in html-->
<!--This is the aspx page declaration. It is usually generated by VS for you. It stores config options, and the path of the backend/ server side scripts.
In this case, your server page is defined by using the CodeFile Attr, and as we can see it is: AddDepartment.aspx.vb
Note. Don't be fooled into thinking that the codefile must be seperate to this html file. It can reside on the same page but for readablity sake, its much better to do it this way.
-->
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="AddDepartment.aspx.vb" Inherits="AddDepartment" %>
<!--These tags render as divs
What is important to note is the runat="server" attribute. This makes that location accessible from the serverside code, and allows vs to bring it up in the autocomplete.
-->
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table>
<tr>
<td>
<!--renders as span-->
<asp:Label ID="lblmessage" runat="server" Text=""></asp:Label></td>
</tr>
<tr>
<td>ID</td>
<td>
<!--Renders as input type='text'-->
<asp:TextBox ID="txtid" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<!--Renders as input type = 'button' (or) type= 'submit' if the page is a webform-->
<asp:Button ID="btnadd" runat="server" Text="Add" /></td>
</tr>
</table>
</asp:Content>
/*
This is C# comment syntax. VB.Net does not allow multiline comments so it makes it difficult to type this tutorial out.
*/
/*
This is your page backend
*/
Partial Class AddDepartment
Inherits System.Web.UI.Page
/*
This is the event handler for the click event on the "btnadd" field on your web form. When it is clicked, this method is fired!
*/
Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnadd.Click
'These are variable declarations
Dim id As String
Dim name As String
'Grab the Text value that exist in "txtid" field on the webform (aspx page)
id = txtid.Text
'Grab the Text value that exist in the "txtname" field on the webform (aspx page)
name = txtname.Text
'We now call a method to insert the data in the database
DBDepartmentController.Create(id, name)
/*
Note: From what i am seeing here, it is following a simple MVC pattern, where your database logic is stored in a seperate file known as a controller. DBDepartmentController is the name of the class which seems to contain the logic by which to create the database entry.
*/
End Sub
'This fuction will execute if you change the value in the 'txtid' field
Protected Sub txtid_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtid.TextChanged
End Sub
'This fuction will execute if you change the value in the 'txtname' field
Protected Sub txtname_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtname.TextChanged
End Sub
End Class
'Import the crucial Classes required for executing the page logic.
Imports Microsoft.VisualBasic
'The classes for handling data connections
Imports System.Data
Imports System.Data.OleDb
/*
As mentioned previously, this class handles the db logic
*/
Public Class DBDepartmentController
'create
'read
'update
'delete
/*
This method creates a new entry in the database! It takes 2 parameters
User Id (this is the txtid field from the webform) PS that syntax seems incorrect (missing an underscore perhaps USER_ID or simply ID based on the values in the method {I removed the USER ID and replaced it with just ID to match})
Name (this is the txtname field from the webform)
*/
Public Shared Function Create(ByVal ID As String, ByVal Name As String)
'Write the insert sql
Dim cmdText As String
/*
As mentioned previously, this should be ammended to allow the fieldnames to appear with its corresponding fieldvalue
cmdText = INSERT INTO USERS(departmentid, departmentname) VALUES('{0}','{1}')"!
*/
cmdText = "INSERT INTO USERS VALUES('{0}','{1}')"
'Make a new connection to an OleDB Data source (MS Access)
Dim myConnection As OleDbConnection
'Connect to the database using a connection string previously set by the application config!
myConnection = DBController.GetConnection()
'A command is required to execute a database query. All sql queries go through the class Command (OleDbCommand, SqlCommand etc)
Dim myCommand As New OleDbCommand
'Specify the connection to which the command will be routed (Defined above)
myCommand.Connection = myConnection
/*Specify the sql to execute. (Our sql is stored in the cmdText variable, and it is missing 2 parameters ({0} and {1}). These parameters are defined in the method definition ID and Name, which comes from the webform)
After the String.Fomat method runs, myCommand.CommandText = INSERT INTO USERS(departmentid, departmentname) VALUES(ID,Name)"
*/
myCommand.CommandText = String.Format(cmdText, ID, Name)
'Execute query (Does not return any values *I believe)
myCommand.ExecuteNonQuery()
End Function
Private Shared Function ID() As Object
Throw New NotImplementedException
'This is invalid, no use for this at this time
End Function
End Class
I surely hope this in addition with what we discussed earlier has cleared up any issues you may be having.
Peace