Monday, October 1, 2012

On to C# and MVC

I have finally gotten rid of the ghost that is VB.Net now.  Got a new job as a C# and MVC developer, and I love it!  The new gig is all working on internal apps for a very large company.  Actually some of the older apps (2003-ish) are still in vb.net, converted up to use VS 2010.  I can't wait to tear them apart.

Also I'm surprised at how much I'm enjoying living in C# land full time.  So far it is seriously cool.  No major translation problems or anything.  All good in the hood!

Friday, March 23, 2012

Don't call it a thread-off

I had to do something incredibly cool the other day so I thought I would share.

A web site I was building calls two WCF services per data request.  It's not my architecture.  But in this case, the data call was to take a text file and dump the contents into the SQL database.  Since those files can get longer than a typical HTTP request can stay alive, I started getting timeout errors while waiting for the WCF service to finish its processing.  My first thought was to make the processing asynchronous.

The fun thing about multithreading and asynchronous operations is that I was passing the file name and path to the saved text file into the WCF service.  I needed to spin off a new thread while passing in the parameter, which I didn't expect to be this easy.  Here's the code:

Public Function ImportThis (byval psFileName as string) as Boolean
     Dim MyAsync as New System.Threading.Thread(AddressOf AsyncImport)
     MyAsync.Start(psFileName)
     Return True
End Function

Private Sub AsyncImport(psFileName as String)
     'Put code to process text file here
End Sub

The end result is that my service returns true back to the calling page, but of course I wrapped it in a try/catch statement and had to do a couple of other things in there.  I watched the database just add records into the table, then clear them back out as the DB finished processing the import.  All while the web site thinks everything is ok.  Good practices also dictated that I put plenty of error trapping in the AsyncImport process, where it emails an administrator a list of bad records in the import, or anything else that goes wrong. 

Bottom line is that it was easy to spin off a new thread for some asynchronous processing, and pass a parameter value into the process.  Method #2 for doing the same thing is to create a private property for the service, set the property value in the public function, and call the property value from the async function.  But just passing the argument in is still the simpler approach.

Thursday, October 6, 2011

No More Steve

I started out as a PC user with DOS 2.16 or some really early version like that running on a 386 back in high school, and used the old 5.25" floppy disks for storage. As a sophomore in college my roommate had an Apple II that we used to write papers on. I used to take money (or beer) to type up papers for other students, because typing was the only thing I knew how to do on a computer, and that was before we had the internet publicly available. Senior year we finally started learning how to get online so I switched over to the PC. After college I went back to school to learn how to write code and fix pc's

That was the only time I ever used an Apple product. Still with Steve Job's passing away yesterday nobody can deny the influence he has given over technology as a whole. His vision will be missed by us all in the future.

Back in the mid-2000's when I had the business I did my fair share of Mac support and repair work, got to know the IOS pretty well. I could do all of your basic setup and admin work on mac's, getting them network connectivity. I remember when the first (maybe second) generation iMacs came out with intel processors and my business partner and I were some of the first people to roll out dual boot options with Windows 2000 and the Mac OS 9.

Now as developers we build web sites for use with mobile browsing, size and graphic constraints thanks to Steve Jobs influence. Designers alone are making everything with rounded corners now creating the same feel to products that Steve gave the Apple product lines.

I'm sure with a few more years we would have gotten more from him. He'll be greatly missed. But like I tell my kids today, you get what you get and you don't get upset. Thanks for everything Steve.

Monday, September 26, 2011

Slick Javascript Trim

I found a great Trim function for Javascript that I wanted to share. Got it from http://developer.loftdigital.com/blog/trim-a-string-in-javascript and it has the cleanest explanation of space removal I've seen in a while.

My task has some form validation for incomplete fields, and it was reported back to me that users were entering a space and it was passing the validation. Now this is also why you should use both server side and client side validation so that extra space crud doesn't go into your database. But that's another post.

My javascript (inherited - I found it but didn't write it) was doing a

if (document.formname.elementname.value == "") ....

so if you enter a space, that's not equal to empty string. Validation passed, right? That syntax alone is enough reason for me to switch to JQuery. But that's out of my control

Javascript by default will use regular expressions for string comparison. If we change the if statement to read:

if (document.formname.elementname.value.replace(/^\s+|\s+$/g, '') == "")

then it will remove all spaces in the form element text no matter what. The regular expression breaks down like this:

/ start the regex
^ from the beginning
\s look for spaces
+ not just as the first character
| or (really more like also)
\s+$ look for spaces until the end of the form text
/ end regex
g = make it global

And since this is in an IF statement, it's just going to check and see what happens if you strip out all of the spaces. If you type two words separated by a space it will still go into the database as two words. and the conditional statement will still return false.

This is the cleanest and "most right" trim statement I've seen yet. Very cool!

Tuesday, September 20, 2011

Binary Fun

Today's XKCD is worth a quick read. Too funny!

Sunday, May 1, 2011

MVC?

Why is Microsoft pushing MVC so hard? All I hear about the new certification exams is that they are very heavy on MVC and JQuery. We've been using plenty of JQuery at work lately, I feel really comfortable in there.




But WHY all the MVC? I'm sure I can pick it up. But I'm going into it a bit begrudgingly. I know eventually I'm going to have to tell somebody who's currently in elementary school how "we used to do some really cool stuff in vb.net. But MVC is such a different framework I'm not looking forward to the change.

Monday, May 10, 2010

Making Scrum Fail

http://glenndejaeger.wordpress.com/2010/05/06/how-to-make-scrum-fail/

I saw this article today about how to make scrum fail. We use Agile Scrum and I love it. others are not so enthusiastic. You do have to have the right people in the right roles.

I am posting a link to this because I think it's funny, not because I'm implying that we are falling victim to any of the conditions listed.

Sunday, October 18, 2009

LINQ Rocks

I'm becoming a convert. The new job is awesome, I'm loving Raleigh. But this new gig has me focusing on a subset of VB.Net (ADO.Net) that I've never had to dive into before. and it rocks.

LINQ to Entities is a small but durable subset of ado.net introduced in framework 3.5. It gives you separated OOP layers to run between a DB (or multiple db's) and the rest of your framework. You can define an EDMX file to obfuscate your data, and it will generate the XML to connect your business layer to. These become your objects (entities) for the entity framework.

I've always been inherently opposed to OOP layers, they just add complexity when it isn't really needed. You end up creating spaghetti code with LINQ to Entity, but it's not as bad as it could be. and once it's built, it's built. I'm becoming a convert.

Once your data layer is built it's done. This makes the front end extraction considerably easier. You write linq queries instead of T-SQL queries, which are a completely different syntax.

The ease of front end development and flexibility of the LINQ syntax are what's really making me turn over the leaf. In T-sql, I'm hooked on aggregates. I can do anything in t-sql, been writing it for years. I'm still at the bottom of the learning curve from the LINQ syntax. But using .Include's to create joins has me intrigued. I'm actually looking forward to learning more. which hasn't happened in a long time with vb.net.

Friday, October 2, 2009

Last day on the job

Well, it's good to be changing jobs. Today is my last day on this work from home job, and next wednesday I start the new gig with a software company in Raleigh, NC. The new job is all VB.Net web sites over SQL Server databases, hosted in the same environment. It's going to be nice to have something stable that isn't a pressure cooker environment where anything can be thrown at me.

Here's to new beginnings!

Wednesday, September 23, 2009

New Digs

Well, it happened. I accepted a new job offered to me yesterday. Looks like all that whining about switching to C# was unfounded. The new digs are strictly VB.Net over SQL Server. Now I have to get a car, sell the house, work my way out of this job, buy a house.... geez. let the whining begin.

Thursday, September 17, 2009

Tech support is a good thing

I've been tagging support for this one company trying to get help with connecting to an XML web service that they provide. A solid month hitting up the two contacts I got with no response, actively refusing phone calls and email responses. I eventually had to go back to my client (who chose this discount provider) and get them involved from the sales side.

It is so nice to actually get tech support from someone who know's what they are talking about. As technicians, I think we all have to strive to be that guy. The one guy who actually can provide some help even if the rest of the organization is comprised of dumbasses.

Yesterday my client went back to the salesperson, who tagged "that guy" and we had the problem fixed in 2 emails. They didn't have the numbers I was submitting in their system, the same thing I asked the other guys to check.

So go be "that guy" today. Take care of the small easy problems, and knock out as many as you can. Be helpful to our clients. They will appreciate it much more than an unreturned phone call.

Thursday, September 10, 2009

ActivePDF

I have to profess love for ActivePDF. I think there are a bunch of PDF form filler services out there. But their ActiveToolkit software is top shelf. Today I am using it for a second process.

It's a DLL consumable with .Net, so you have to set a reference to the toolkit. But then you can open and manipulate pdf's inside of the toolkit in a bunch of ways. The first time I had to consume it was to prepopulate and/or extract data from a PDF. I got the data in an XML string, and looped through the XML nodes to shove them into the PDF. Open the input file first (source), then create the output file (destination), populate the field values in memory and call CopyForm to write the memory values to the output hard file. Very slick. I can also specify specific field names to populate, like a submit date.

Today I'm going the other direction. I am using ActivePDF to open the input (source) file, loop through each field on the PDF and inserting the field names (with some other identifying data and logging) into the database. Should be interesting and fun.

Wednesday, August 12, 2009

Jobs

OK, there are DEFINITELY more jobs out there for C# guys than VB.Net junkies. A few years ago, it was the other way around. Wow, I had no idea the landscape had changed that much in the last few years. I'm certainly going to start writing more C# code.

Every job listing I see now is for a C# position, but they will take VB folks and train them, I guess. Which is really just a cruel joke. There are so many people looking for work out there, if you have the choice to hire a VB developer and teach it C#, or just hire a C# developer, which one will you choose? Got to remarket myself as a C# guy.

Friday, August 7, 2009

Primary?

I started writing the .Net languages when they were initially released on valentines day in 2002. So I've been a .Net guy since day 1. Before that, I was a VB6 programmer, and wrote web sites in Notepad before moving into Homesite, then eventually Dreamweaver.

Would you believe I actually got tapped to teach a VB6 class just a couple of years ago? 2006 or 2007, I think. That old thing will never die. It's still a cool platform I guess. But I went right into VB.Net when I switched jobs and went to work for a local IT consulting firm in 2002. The first project was picking up after another programmer who initially wrote the site in C#. Then I did a small winforms program in VB.Net, then more C#, then web sites in VB. Since then, I've placed over 1,000,000 lines of VB.Net code in production, and maybe 20,000 lines of C# into production. So VB.Net has totally been my primary language for the last 7 years.

Now it seems the workforce is being dominated by C# jobs. I was talking with a trainer yesterday who had cut out all of their VB classes for lack of demand. So apparently people who want to become new or better .Net programmers want to go C#. If you do a job search for VB.Net on any of the major job boards, you find C# jobs where they will consider a VB programmer that can be retrained.

This leads me to give serious consideration to changing my primary language. I'm the only developer at the company I currently work for, so I can write any project in any language I choose. I hate a case sensitive language, and that's why I only use C# when I am forced to now. But I'm sure with time I will get used to all the nuances of the language. I recently had to translate one of my favorite scripts into C# and it was only mildly frustrating. Do you, gentle reader, have an opinion here? What do you think the programming landscape is doing?

Wednesday, August 5, 2009

DB Connection Strings

Here's how I connect to databases using vb.net:

In a config file, I typically use an AppSettings value to store the setting like:

<add key="dbconn" value="Data Source=127.0.0.1; User Id=sa;Password=; Initial Catalog=MediaDent; Persist Security Info=False" />



Pervasive:

Provider=PervasiveOLEDB;Data Source=C:\PWorks;

SQL Server:

Data Source=127.0.0.1; User Id=sa;Password=; Initial Catalog=dbname; Persist Security Info=False

MS Access:

Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\dbfolder\viewer.mdb; Persist Security Info=False;

ODBC:

DSN=myDsn;Uid=myUsername;Pwd=;

or

DSN=connectionname;

And that's all I've had to connect to recently.

Wednesday, July 22, 2009

Consume .Net DLL in Classic ASP Script

I've been fighting this one all day, and I think I've finally got it figured out. I use XML web services all the time. But without a lot of crazy SOAP calls you can't use them in a Classic ASP script. My solution is to make a DLL using VB.Net that will call the web service, pass the same parameter values, and return the results back to the calling app. Seems simple, right? Here's the breakdown:

1. Create the XML Web Service. Add your exposed web methods.

2. Create a new vb.net Class Library type project. This will create the DLL. I use VS.Net 2003 and 2008 on the same dev machine. The web server I am installing this one on is running a Classic ASP web site, and IIS is configured to use the .Net framework 1.1. Also, RegAsm and GACUtil only exist on this server for v1.1, so I chose VS 2003 to build the DLL

3. Set a web reference to the web service, set the URL Behavior to Dynamic, be sure the URL is accessible from the live web server whether it be IP address or www something.

4. Use this code to build your public exposed methods:

Public Function MyMethod(ByVal psPass As String) As String
Dim sReturn As String = ""
Dim oSend As New Webservice.webserviceClass
sReturn = oSend.Method1(psPass)
Return sReturn
End Function

And repeat as needed for each web method you need to expose from the web service.

5. Right click on the DLL project, and go to Properties. This should show you the assembly property pages. On the Configuration --> Build page, check the box to Register for Com Interop. In VS 2008 it's in the project properties --> Application tab --> Assembly Information button --> check box says "Make assembly COM visible".

6. Build your DLL

7. Open the VS.Net Command Prompt that came with your version of VS. Enter the command:

sn -k "C:\myDLL.snk"

This creates what's called a strongly typed name for your assembly. You'll need that snk file later when we go to deploy.

8. Add the strong name into your DLL. Open the AssemblyInfo.vb file and add the line:

Blogger doesn't want me to display this line, so i'm going to write it out vertically.

<
A
s
s
e
m
b
l
y
:

A
s
s
e
m
b
l
y
Key
File
("
c:\
MyDLL
.snk
")
>

Build your DLL again. Note the spaces in the vertical list. blogger doesn't make it very easy to post code in here that can be interepreted.

9. If VS.Net builds all of that without errors then we are ready to deploy to the web server. 2003 does not care about the config file. So minimally you will need to copy the DLL and the snk file up to the web server. My web server is Windows 2003 Server with all versions of the .Net framework installed. Everything else has to be done FROM THE SERVER CONSOLE. I put the DLL in a folder that is not behind the web server for security reasons. And I put the snk file on the root of C: just like it was on the dev workstation.

10. Open up a command prompt. Get into the C:\Windows\Microsoft.Net\v1.1.4322\ folder

11. Register the assembly using RegAsm:

regasm /tlb c:\dll\mydll.dll

This registers the dll with windows. This must be done without errors coming back before you can get any further. You must use the same version of regasm as you used to build the DLL.

12. Load the assembly into the Global Assembly Cache (GAC):

gacutil /i c:\dll\mydll.dll

If this comes back without an error you are good to go. Again, gacutil must be from the same .net version as you used to build the dll.

13. Call the DLL from the ASP Script

<
%
set oSend = createobject("MyDLL.Class1")
str = oSend.MyMethod(sPass)
set oSend = nothing
response.write(str)
%
>

Now everything has done it's job. Your asp script called the dll, which called the web service, which did it's thing. Then (in this example) we sent a string back to the DLL, sent it back to the script and wrote it out to the browser. that's all it takes!

Bond's nerdy contra-personality

Greetings fellow nerds. I am a full time software developer, and I am starting this blog to post code and hurdles that I encounter in daily programming. I seem to find myself solving the same problems again and again. Instead of re-inventing the wheel every time, I want to create posts about solving a specific problem. Blogger lets me search through the posts so I can easily find my solutions.

Originally, I started doing the same concept on my site for a company I used to own. www.flynntechnology.com has a "Tips" section, where I would put up tips for each language I regularly write. there's maybe 30 tips in there, but the navigation and depth of the whole thing made it take about an hour for each tip I wanted to put up. Here, I can just copy some code, provide a brief explanation about what I am trying to do, and publish the answer. Much easier.

I am a triathlete by hobby. My training blog is at http://trainingsmoker.blogspot.com and I post funny, inspiring, and sometimes strange stories about my running, cycling, and triathlon adventures. Plus I talk about the family there, and it's supposed to be very personable. This blog, by contrast, should have no family pictures or social drive. Just the code, ma'am.

So check over to the Smoke Training blog if you want to know more about me. Comment on any of these posts if you have programming questions or get stuck with something. I'll help out when I can. I will tell you this about myself:

I am a huge James Bond fan. As much as Bond is a general badass, men love him, ladies want to be with him... I consider myself the geek equivalent. Other geeks want to write as many languages as I do. My wife wants me to step away from the laptop every now and then. If you picked up the fact that 00111 was a Bond reference, you are as big of a geek as I am. 111 is the number 7 in binary. We are going to be good friends.