Dot Net Fluke: Getting by on C# for iSeries RPG Developers

Useful tutorials on C# and .NET for RPG iSeries AS/400 developers. Brought to you from the folks at AranRock Consulting

9/1/10

Do I really need to know that?

An afternoon reviewing another C# book felt, yet again, like running in snowdrift. Typical with these kind of books, the first few chapters smother you in infrastructure pedantry, acronyms and background. If I needed to build an average house, would I really, really need to know the history of the lumber industry? The molecule structure of concrete? No. I need to learn how to build a house quickly, efficiently and inexpensively. Similarly, detailed information on the CTS, CLR and FCL are not needed to get a grounding in C#. This is what appendii were invented. Stick all the stuff that people don't need up front into the back of the book where they might need it once they finish reading the book.
This front-loading poses a problem because newbies tend to read the first few chapters very carefully. Each section is carefully scrutinized and digested. But then they discover that the information they spent so much brain power absorbing, isn't actually useful. There is no way to determine the quality or usefulness of information presented in the early stages. By the end of the third chapter, the reader thinks 'Ok...I think I got that, the Common Language Runtime sits on top of the OS, the Framework Class Library is above that for IO and streaming, then there's ADO.Net with XML and whatnot and then my application runs on top of that again' But NONE of this will definitely help you write a program. It's information and often it is interesting but it is not helpful information that leads a developer to realizing their own educational goal . I DON'T CARE how Microsoft layers its development environment because I can't do anything about it - it's out of my sphere of control. What I can control is an application. Show me how to develop quickly, efficiently and show me how C# solves common programming problems.
The volume of information is already overwhelming so why do authors make life difficult for newbies by being overly academic? Why do programming books, in the main, focus on the complexities of the language infrastructure rather than on adaptive, simple application development?
If you know of good development books that break the pedantic paradigm, post 'em here.

Labels: , , ,

8/10/10

How to Create Stored Procedures

Stored procedures are programs written in SQL.  They are their own object, stored on the iSeries db and can accept parameters. They are programs with their own variables, flow control and can be called by any other application. Their benefit is in being able to stack a bunch of statements together and so improve performance, reduce network traffic and make your life simple! Why have multiple db statements executing when you can issue one that does it all! It encourages separation of the data from logic and forces you to be modular. Begone Spaghetti!
They also help security; especially useful in distributed  apps (client/server)  so that the code that updates the db is hidden in the stored procedure and not visible at the front end. Finally they are a super simple way to do a remote call.

You create a stored procedure using iSeries Navigator or directly through your 5250 screen. I'll go over the 5250 way since it is more verbose than the GUI.
To create our procedure we will use  the
CREATE PROCEDURE command in SQL -called an SQL stored procedure. You can also create them in RPG - an external procedure. 

There are 4 main parts to the creation:

1. The NAME of the procedure

 CREATE PROCEDURE  MyProcedureName
2. Parameters I have an input parm of 10 chars and an I/O parm of type integer

( IN REQKEY CHARACTER(10), INOUT RETVAL INTEGER )
 3. Procedure properties The language will always be SQL

 LANGUAGE SQL 
 3. The procedure code

BEGIN  DECLARE CURS1 CURSOR FOR     SELECT CUSTNAME + 1 FROM MYLIB. MYTABLE      WHERE TELNO = REQKEY FOR UPDATE OF CUSTNAME;
Here's an example which creates a stored procedure in SQL for updating a table called MYTABLE.
This has two fields; CustName and TelNo.  When called with a
customer name,  it returns the phone number.

 CREATE PROCEDURE MYLIB.STOREDPROC         
(IN REQKEY CHARACTER(10),     INOUT RETVAL INTEGER )
RESULT SETS  0
LANGUAGE SQL  
NOT DETERMINISTIC
/*Start of Procedure Code*/
  BEGIN  /* Code always begins with 'Begin' */
  DECLARE CURS1 CURSOR FOR
    SELECT CUSTNAME + 1 FROM MYLIB. MYTABLE
    WHERE TELNO = REQKEY
FOR UPDATE OF CUSTNAME;   OPEN CURS1  ;
FETCH CURS1  INTO RETVAL ;
UPDATE MYLIB. MYTABLE SET CUSTNAME= RETVAL
    WHERE CURRENT OF CURS1 ;
 END



I'll cover calling these from .NET in more detail soon. 
In the meantime, create yourself a simple Stored 
procedure and try calling it. Then take one of  your
 existing programs and replace your inline SQL 
replacing it with a stored procedure.


Labels: ,

7/14/10

Learning to learn

I have had more than a few emails from readers who privately moan that they 'just don't get it'. They find the concepts of object oriented programming too complex and wieldy and are unable to apply what they have learned in practice.  Despite having read the books, downloaded the software, written a few programs, they just don't feel competent. Not enough to use it daily in their work or even ace a simple test. They begin to feel as if they are past it, are simply unable to learn, old dogs/tricks etc.
dumb-dog-crossing
Did the following happen to you?
You said you wanted to learn .NET so you went and bought the book. The first few chapters were simple. You grasped types and flow control. Foreach? no problem.  Hello World! I’m a C# programmer! Then you progressed into the depths of .Net.  Inheritance, interfaces, delegates, anonymous methods etc.  You understood this perhaps but you found it difficult to really do anything with them. You did the exercises and it just got worse. It took you a month to read one chapter. You found the whole thing hard, confusing, ridiculous, hard to apply to the real world and at the end you retained very little of what you learned. Four months later you retained NOTHING.
Then you gave up.
Learning moment: You will not learn C# by reading a book.
Don't despair!  Think about it - if you have been solving programming problems day in, day out throughout your career then  it stands to reason that you still have all the right amount of grey matter. Yes! You're smart!  The problem may not be your programming skills, the problem may be your learning skills. This article describes the learning process and what you can do to improve your learning skills.
Quiz: By the end of this article try and answer this question: What learning activity was I (the writer) involved in by writing this piece?

Reading is not Doing
You can't learn C# solely from a book.   Books are the abstract theory that form only part of the process. All the time you spend reading in earnest is just one of the activities required to really learn.  Simply reading a book on the subject (or this blog) is not enough. In fact there is a term for this approach- ‘surface learning’ and will likely end in failure. The reader makes the mistake that if they just read and do the exercises then it will all be absorbed like a sponge and magically they will become a .NET developer. Wrong.  Would you trust the novice captain of your transatlantic flight who spent hundreds of hours playing Microsoft Flight Simulator but never flew an actual plane?  

Programming is an experiential learning activity.You may not have considered the change required to develop a completely new skill since you were at college.  If you are self-taught you may have forgotten that you did make a dramatic shift in thinking at the time. Well, this shift is required again because C# is a not just a different language, it’s a radically different way of programming.
It’s true that you learn all the time. At work you continually develop and enhance your skills but usually you are adding to existing knowledge through active experience and the learning is incremental. What is required now is to learn in a monumental manner  i.e understand, retain, practice and implement a complex new skill on a daily basis that is quite different than any you have acquired to date.  
To accomplish this you need to develop your learning skills. Once you really change your learning methods to adapt to acquiring new systems of thinking then you will find learning .Net or any similar hefty new skill much easier. This all takes substantial investment in time – more than you have likely done already. But if you take the smart approach it will really pay off , you will retain what you have learned and importantly, be able to apply it to daily programming problems. You might have spent months reading C# books without being able to use it practically, so spending the extra time learning to learn will salvage precious time spent reading. So the hard part is not C#, the hard part is changing how you learn. Let’s look now at what learning really is.
Look at the diagram below by the experiential learning guru David Kolb - it shows the various stages of learning. Now tell me where your $50 C# book is in this cycle?  (I have to laugh at the SAM’S programming titles ‘Learn C# in 24 hours’ ).  Programming does not mean coding, SAM. Programming means being able to take a customer problem – like a business request for multi country order entry and turn it into a solution using the appropriate programming language. Knowing the definition of a Lambda expression is much different than knowing exactly how to apply it in the search function in the order history module and why. 
The stages of learning then are:
  1. Abstract Conceptualization & Theory (Books, blogs, articles, lectures)
  2. Active Experimentation (Applying what you have learned in different situations)
  3. Concrete Experience (Doing a real life project in C#)
  4. Reflection (Complete and go back over your experience documented in your learning journal to change your future behaviour.)
So yes, you need the book but you need to remember that learning C# is not just about reading a book and understanding the concepts. It is a major shift in thinking which requires monumental learning.  You read the book but because you weren’t successful and couldn’t apply it you thought .NET and C# was just beyond you. It’s not. You just didn’t complete the other 3 parts of learning. 
But Kolb doesn't say HOW you learn in a monumental way.  Let’s turn now to improving your learning skills.
One proven method for developing learning skills comes from clinical practice- 'Reflection ON Action and Reflection IN action' according to Price & Maier in their excellent book ‘Effective Study Skills’ (Pearson 2007).  This reflection process means to systematically monitor your learning while on a task and after the task.
You will need to add about 10-20% of your time for each task to monitor it through the process called 'Reflection'.
Don’t skip this! You probably think ‘I don’t need to be this explicit, I know all this stuff already’. I agree, you do know this stuff, but you know it implicitly. The point is to improve your learning and to do that, your knowledge needs to become more explicit.  Engineers are notorious for being black holes of knowledge. It all goes in but nobody , including themselves, sees clearly into that obscure mass of synaptic lightning. They scoff at others who are ‘all talk and no action’ they find articulation beneath them. They hide in their cubicle and weeks later magic stuff appears on screens. The reality is that we all need to be a little more ‘all talk’  to learn with skill, speed and be able to quickly put it into practice. If we want to keep making magic stuff, we need to articulate our learning, we need to learn how to be reflective.
Reflection is a process whereby you mirror the activities you are engaged in, think about them objectively and determine ways to change your behaviour and then actively apply the changes in your behaviour.
You can start using reflection today. Let’s take a typical project like order entry.
Here’s the three steps to reflection:
1) Recap: Go back over the requirements, how they changed during coding, what delayed the project. Go back over what happened. Articulate the project issues and how they were solved or not solved. Articulate doesn’t mean glossing it over in your head, it means writing it down, presenting it to others, chunk the information in meaningful ways, use mindmaps to look for patterns.
2) Reframe: Take what you have learned and apply it in a new situation.
If the requirements changed substantially during coding then the
A simple example comes from language learning. You take a word and then use it in a new sentence.
3) Reassess: This is a biggie. Here you need to determine how to change your behaviour and activity based on what you learned. You may have noticed that some problems go from project to project. Don’t blame it all on upper management, the users, budget etc. Figure out where you can specifically change your behaviour in the next project and then commit to change. e.g. Create minutes after every meeting so everyone is clear as to their responsibilities.
Remember, the process of reflection happens IN action and AFTER the action. Keep a journal to track your learning (see below).
I recommend a few things to retool your learning.
1) Read Study the book ‘Effective Study Skills' by Price & Maier (Pearson Press)
2) Take a study course at your local university or college. Learn to Learn!
3) Keep a notebook on learning. Track your learning like a kind of resume. State your current ability in the various areas of .NET, state your objectives VERY specifically (e.g. be able to write any subfile type program in ASP.NET in under 6 hours).  

I'm only lightly touching on the subject here but take comfort in the fact that you're not dumb, that you CAN learn, and you will be a great .NET developer because you are a great iSeries developer. However you can and you must change. Yes, dog/spots, you can change -  through improving your learning.  Most programmers are self-taught and when they come up with a learning block they stop right there because they have only learned one way. What they forget is that there are lots of ways to learn. You owe it to yourself to improve your learning, to adapt to what is changing around you.  Don’t bank on learning C'# just one way. Take a class. Get a junior position in a .NET team (even if you are 48) and get adopted by a guru (even if he’s 25). Have learning as a systematic process and core part of ALL your activities. Reflect on your learning.  OK, try and do it right now. Go through this article again to reflect i.e. Recap, reframe and reassess.
An efficient learning process is essential for your continued professionalism. Look at Dice.com and see where all the jobs are - in newer technologies like .NET.   In 10 years it will be completely transformed again and you will need to change again. Becoming a proficient learner is just another skill but is undoubtedly the greatest skill of all.
The information on learning in this article is partly based on Price & Maier’s book ‘Effective Study Skills' 


6/22/10

Books on writing C# apps for iSeries

One of the reasons I started this blog was to periodically build up enough material for a book on interfacing the iSeries with .NET
Someone has already done this - written a book, I mean. It's called "Develop Microsoft C# Applications for the iSeries" by Craig Pelke. They are priced towards IT departments at $445 not  for recession babies like you and me.
He's written  a bunch of books about .NET and the iSeries, so if you have got any of them - please let me know what they are like.
Here's a link to where you can buy them
http://www2.systeminetwork.com/str/books/UniqueAuthor.cfm?NextAuthor=150

Labels: , , ,

5/22/10

Fast Paging a .NET grid from an iSeries table

I recently wrote a simple grid view ASP.NET web application for a client displaying late paying customers. The SQL behind it though had to summarize and group across several different tables on the iSeries. The program worked well but the client complained that the app took too long to start up.
To speed things up, I put all the SQL into a stored procedure used a scrollable cursor and fetched 22 records at a time.  FETCH NEXT FROM C1 FOR 22 ROWS
I shouldn't have done a select All - it took too long and left the customer hanging even though there wasn't much data behind it. 

4/17/10

What is ASP.NET?

ASP.NET is the platform to create web pages in .NET. 
We say 'I'm writing this app in ASP.NET' to indicate the technologies used.
To create an ASP.NET page, open up Visual Studio and create it from there.
I would then use C# as the code with HTML to provide the controls to process the page.
That code is either embedded in the web page code itself or included in a separate file.
When embedded, it is called INLINE, when separate it is called CODE-BEHIND.
I prefer to use code behind since it keeps the application logic separate.
Visual Studio saves an ASP.NET file as .apx page type.
A web config file is part of the files in your application which contains your connection string to the iSeries.
ASP.NET may seem daunting and the mix of html and C# makes it especially so. The number of files generated by Visual Studio may seem excessive but the aspx and web config are the ones to focus on.

Labels:

3/19/10

Connection String Stinger - Phantom Library List

.Net can look for your tables in a library the same name as your user id  even if you supply a library list. This can cause your SQL 'selects' to fail.
Take a look:
Here's a basic connection string using the IBM supplied iDB2 Classes:
DataSource=192.168.0.1; UserID=colm;Password=xxxx;LibraryList= CusLib,ARlib, QGPL

At first glance everything looks good, right?
So now I want to get at my table MYCUST in CusLib. Should be a no brainer. However if I run the SQL command 'Select * from MYCUST' guess what I get?  - A big fat error like this:
SQL0204 - File MYCSUT in COLM not found
What??. CusLib is on the TOP - I don't even mention library Colm.  What's going on?
IBM says that if you are using the SQL naming convention that it sets the default collection or library to the library with the same name as the user id. I have no idea why they do this! Anyway if you have this issue the simple fix is to specify the default collection. i.e. defaultcollection=cuslib
Here's the new connection string
DataSource=192.168.0.1; UserID=colm;Password=xxxx;LibraryList= CusLib,ARlib, QGPL;DefaultCollection=CUSLIB

Labels: , , ,