Posted By

JordanRowles on 05/01/14


Tagged


Versions (?)

Logic used in internal Licensing program


 / Published in: C#
 

Logic to make sure that the input through window controls (text boxes, drop down selections ...) were the correct length for the database table.

I.e. If the user entered 'abcdefghijk' into the 'HS Asset Tag' (hstag) text box, it will reject it because the table column hstag is set to VARCHAR(10) - a string with a length of ten.

======================================================

LINE | EXPLAINATION

01 Creates a public class (container object) called DictionaryToDB. This is because it is taking the contents of a dictionary (key-value

pair) and putting it into a database table.

02 Create the dictionary object (another container type). 'string' means that the key in the dictionary must be a string (a sentence or word encapsulated by " "). 'dynamic' means that the datatypes can be different, you can have integers, strings, lists and objects in the same dictionary. This is useful because database table rows have a

specific datatype attributed to them.

04 I create a method/function that's public and doesn't return a value (void) called AddToDictionary, because it will perform the logic and add the keys and values (which we get from the windows form). It takes two arguments. Which is called whenever we want to use that method on some data. It becomes an object (method) inside another object (Class) - encapsulation. Used like:

AddToDictionary("hs_tag", "02535")

05-13 05 - It starts as an if statement, it does something if the condition given is either true or false. For example: if (MyName == "Jordan") { 06-10 - This is the basic layout of what you are seeing == is equal to || is 'or' && is 'and' <= is less than or equal to

                if (
                          (columns with 10 or less characters)  or
                          (columns with mixed length) or
                          (columns with 25 or less characters) or
                          (columns with 50 or less characters) or
                          (columns with 255 or less characters)
                          = The users input   then
                                Add the key and value to the dictionary

That's the basics of it. Still needs a little more work and need to add the other columns, with different datatypes like DATETIME and INT.

  1. public class DictionaryToDB {
  2. public Dictionary<string, dynamic> MyDictionary = new Dictionary<string, dynamic>{ };
  3.  
  4. public void AddToDictionary(string key, dynamic value) {
  5. if (
  6. (key == "hs_tag" || key == "host_pc" && value.Length <= 10) ||
  7. (key == "mac" && value.Length <= 17 || key == "ip" && value.Length <= 25 || key == "wireless_mac" && value.Length <= 20) ||
  8. (key == "disks" || key == "cpu" || key == "memory" || key == "graphics" && value.Length <= 25) ||
  9. (key == "name" || key == "manufacturer" || key == "model" || key == "serial_no" || key == "oem_os_key" || key == "allocated_to" || key == "bios" && value.Length <= 50) ||
  10. (key == "comment" && value.Length <= 255)) {
  11. MyDictionary.Add(key, value);
  12. MessageBox.Show("It worked!", "Success!");
  13. }
  14. else {
  15. MessageBox.Show("It Failed!", ":(");
  16. }
  17.  
  18. }
  19. }

Report this snippet  

You need to login to post a comment.