WRITING STUDENTS

And now lets go to our final system that has two applications. The first is the registering of students:

What we will do now will be the creation of an SQL database (name: Croquet101) having a table (name:students) with the columns: name - 40 digits - and country - 20 digits.

Created the MS Access database (a .MDB file) we need to upload it to the server - in our case: www.dmu.com - in the folder data.

In the Cobalt front,all is similar we did in the first lesson. Here we have a Button and three TextBorder.

Glued to the Button we will have two scripts. The first starts by a click and the second starts by a "mouse-over". Look the first (remember the "problem" of the versions of the controls:

| page parsedPage thebody clean nameCust countryCust|
 
Msg6  setCharacters:'Data sended.Wait...'.
nameCust:=(Name  getCharacters) copyReplaceAll: ' ' with:'_'. 
countryCust:=(Country getCharacters) copyReplaceAll: ' ' with:'_'. 
Name  setCharacters:' '.
Country  setCharacters:' '.
page:=HTTPSocket httpGet:'http://www.dmu.com/croquet/writestudents.aspx?name=',nameCust,'&country=',countryCust.
parsedPage := HtmlParser parse: page. 
thebody := parsedPage body.
clean := thebody   contents at:1 .  
clean := clean  asString .
clean := clean copyReplaceAll: '[' with:''. 
clean := clean copyReplaceAll: ']' with:''. 
Msg6 setCharacters: clean.

Look the code of the ASPX application (writestudents.aspx) in JavaScript .NET, uploaded to our Windows server:

<script language="JavaScript" runat="server">
function Page_Load(sender:Object, e:EventArgs) : void {
 var  name=this.Request.Params["name"];
 var  country=this.Request.Params["country"];
 
 var msg;
 msg = insertStu(name ,country); 
 Response.Write("DB MESSAGE="+msg); 
}
 
function insertStu(name,country){
 var oConn:System.Data.OleDb.OleDbConnection = new System.Data.OleDb.OleDbConnection(); 
 oConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/data/croquet101.mdb"); 
 try{ 
  oConn.Open();
  var oCmd:System.Data.OleDb.OleDbCommand  = new System.Data.OleDb.OleDbCommand();
  oCmd = oConn.CreateCommand(); 
  oCmd.Connection = oConn;
   //Inserting the data
  oCmd.CommandText = "INSERT INTO students (name,country) VALUES('"+ name+ "','" + country + "')";
  oCmd.ExecuteNonQuery(); 
  return "Values registered!" ;
 }
 catch(e:Exception){
  //Write the error, if we have one
  return "Error: "+  e.ToString();
 }
 finally{
  //Having error or not we close the connection, a good pratice.
  oConn.Close();
 }
}
</script> 

To teach ASPX is out of the scope of this course but, for your information:

The second script glued to the Button clear the field of messages (Msg) when the user goes to click for a new transmission.

.

If you like, you can do the exercice of this lesson and use our server for the access to the ASPX script. Our server is free available for tests.

In the video we will see the registering of two new students. In the next lesson we will see them included in the list of students.




PREVIOUS LESSON NEXT LESSON
TABLE OF CONTENTS HOMEPAGE