Image Image Image Image Image Image Image Image Image


June 12, 2012

Online Passphrase Generator.

June 12, 2012 | By | No Comments">No Comments

Note: This is a (fairly) long post where I go through my entire project. If you plan on reading this in one sitting I advise putting aside 20+ minutes. Consider yourself warned. =)

So after doing a little bit of research I’ve found that there really isn’t much of an online passphrase generator available. So being the type of person I am I decided I would make one.

In my previous role we were transitioning from Lotus notes to an online platform and the??back-end??was more than likely going to be C#. I’m the first to admit that I’m no coder. The main reason is that I just can’t get motivated to learn the code properly, I need a project that I can work through that will have some purpose and allow me to learn things along the way. So I thought to myself that this would be a good time to look around and see what I could use and this is how I stumbled across ASP.NET 2.0 with Razor.

For someone like me that isn’t very much of a programmer this seemed to be right up my alley. WebMatrix has a lot of helpers to, well, help get things done. Although there have been some minor hiccups along the way, like for example the .dll files for some helpers not being in the right place to be used, I will outline this in more detail a bit further along.

So my theory going into this project was that it should be fairly easy just create a couple of variables calling values from a database at random and display them along with a random number. Turns out it wasn’t that easy.

My first hurdle was opening the database. I had done this before when I coded a dummy site to use in my last role so I just wrote the same code which was:

var db = Database.Open("words");

But for some reason I would come back with an error that told me it didn’t know what Database was.

Compiler Error Message:??CS0103: The name ‘Database’ does not exist in the current context

I started doing some research for this error feeling very confused as to why this wasn’t working when it had only a couple of months ago. All the results I was getting was saying that I should be prefixing with the correct namespace which a couple of sites mentioned as WebMatrix.Data.dll. Well that seemed simple enough so I put in the code @Using WebMatrix.Data but when I did that I was given an error message stating that it didn’t know what WebMatrix.Data was. Well now I was really stumped, I did a bit more research and came up with nothing. So I just did a search on my entire C: for the file and came up with a few results, I copied one of those ??over to my project bin directory and tested the site again and it worked! Somehow Microsoft have fudged this up so that when WebMatrix makes a site it doesn’t give you this default helper. Anyway first hurdle completed I can now open the database I created called words.

The next thing I have to do is randomly call the data from the database using SQL. That seemed easy enough so I used the following code:

var word1 = db.Query("SELECT word1 FROM words ORDER BY RAND() LIMIT(1)");

So I thought I had it, seems logical enough but no, LIMIT() isn’t recognised by SQL Compact Edition so I had to find another way of getting this to work so back to the search engine I went to try to find the answer. Everywhere I looked it told me that I should use the LIMIT() function, until finally I found that I can use the TOP() function and it will do the same thing. In my travels through cyberspace I also found that rather than using the RAND() function I should use NEWID() because you can actually predict the RAND() function. So with all that newly acquired knowledge my new query looked something like this:

var word1 = db.Query("SELECT TOP(1) word1 FROM words ORDER BY NEWID()");

So I run my page with the new code but while I don’t get an error message I don’t get what I’m after either. I get some gibberish that looked like this:


Great, what is the issue now? From what I could tell I was pulling an internal command of SQL. The question was how to get the data I so??desperately??wanted. After searching in vain for a while I decided to ask for some help in the ASP.NET forums. I got a reply after a while telling me that I had forgotten to add one crucial bit of code to the query. Instead of:

var word1 = db.Query("SELECT TOP(1) word1 FROM words ORDER BY NEWID()");

What I needed was the??Database.QueryValue()??as shown below:

varword1=db.QueryValue(“SELECT TOP(1) word1 FROM words ORDER BY NEWID()”);

Makes sense now that I look at it but funnily enough I couldn’t find any documentation on this which may be because it’s a rookie mistake and everyone just presumes that you should know this.

Anyway with those hurdles out of the way I was now getting random results from the database! All I did now was clone my query and define it as a seperate variable then call in the body of my page.

The last thing I needed was to generate a random number to put in with my passphrase and that was a synch. First I set a variable for a call to the Random() function then defined a new variable that ran the previous variable and I limited it to 100:

var r = new Random();
  var RandNum100 = r.Next(100);

I now had everything I needed for my random passphrase generator… once I fully populated the database. You can find the code??HERE.

I will host this some time in the near future and update this post with the link.