Skip to main content

Posts

Sudoku Puzzle Generator

Sudoku puzzles can be generated by a switching the rows and columns of a valid puzzle. The switch has to be done between the 123, 456 and 789 rows/columns. ie; 1 cannot be switched with 7 for example. A random pair can be generated and if a loop is run say 50 times, we get a new puzzle. Then the cells can be hidden again randomly. The following code generates the puzzle from a base character string which is converted to a 81 length Char array. The output is the solution as well as the puzzle with blank (or 0s)               Dim stdArray As String = "317849265245736891869512473456398712732164958981257634174925386693481527528673149"         Dim charArray() As Char = stdArray.ToCharArray         Dim rng As New Random         Dim m As Integer         Dim row1 As Integer         Dim row2 As Integer         Dim col1...

BitArray 0 and 1s to Hex and back

This code will let you convert an arbitrary string with 0s and 1s only into a Hex value, and back. Why? Maybe you have a Javascript component that renders based on a long array of bits. Lets say var b = "101010101100101001000101011101010010011100000010100110000010001010010011000000000001000010101001110000000001100010100000" The string can be stored in the DB as nvarchar(max) field, but then if you want to reduce the length by 1/8th, you can convert it into a Hexadecimal representation The above would be equivalent to 27029822 930010A9C018A0 Ok, this is the 'bit packing' concept implemented in a very crude way. Anyhow, the output is human readable and probably easier to pass in a JSON The functions are as below     Private Function bitArrayStrtoHex(b As String) As String         Dim sb As New System.Text.StringBuilder         For i As Integer = 0 To b.Length - 1 Step 8             Dim cu...

HTML Sidebar menu without jQuery

Most examples I found on the net for a responsive sidebar had a reference to jQuery. For two lines of code, I find it an unnecessary overhead. So this is a basic page with no dependencies that has a toggle-able sidebar as well as a media query javascript that hides the sidebar below a breakpoint screen width. Modify as necessary. Maybe tricks could be added such as the one to change the hamburger to X when the sidebar is visible. https://jsbin.com/romiqov/2/edit?html,output <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>SideBar test</title>     <style>         * {             margin: 0;             padding: 0;             font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;         }         #sidebar { ...

SQL Server - Get columns and data types from all tables

Useful script USE SmartHire GO  SELECT TABLE_NAME, COLUMN_NAME, COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), COLUMN_NAME, 'ColumnID') AS COLUMN_ID, DATA_TYPE  FROM SmartHire.INFORMATION_SCHEMA.COLUMNS  GO   Output TABLE_NAME COLUMN_NAME COLUMN_ID DATA_TYPE TblBankDetails BankID 1 int TblBankDetails BankName 2 varchar TblContactPreferences ContactPreferenceID 1 int

Modal dialog window using Javascript and CSS

Add this function to your scripts. Call it from a click or other events like button.onlick= function() { showModal("some content to be shown as modal dialog"); function showModal(txt) {     var d = document.getElementById("xf_modal_window"); d && n.parentNode.removeChild(d); var n = document.createElement("div");     n.id = "xf_modal_window", n.className = "modal"; var o = document.createElement("div");     o.id = "er_div_modal_content", o.innerHTML = txt, n.appendChild(o); var t = document.createElement("input");     t.type = "BUTTON", t.value = "close", t.onclick = function () { t.parentNode.parentNode.removeChild(n) }, n.appendChild(t), document.body.appendChild(n) } This to your CSS  .modal {     position: absolute;     left: 0px;     top: 0px;     width: 100%;     height: 100%;     text-align: center;     z-index: 990;     backgro...

Getting ENUM values by Reflection

Recently I had this problem, where I had  to get the Items and Values of an Enum within a class. The class name and enum name (as strings) are known and the instance itself has to be created at runtime. i.e; via Reflection Namespace N1abcd .... Public Class TestClass     Public Enum TestEnum         val1 = 20         val2 = 30     End Enum End Class The syntax in VB.net is as below '   System.Type.GetType("Namespace Name" + "." + "Class Name" + "+" + "Enum Name") Dim fieldInfos() As System.Reflection.FieldInfo = System.Type.GetType(" N1abcd .TestClass+TestEnum").GetFields             For Each f As System.Reflection.FieldInfo In fieldInfos                 If f.IsLiteral Then            ...

.Net Serialize and Deserialize Objects to XML and database

Serialization Create an empty instance of your object Dim Obj as New MYOBJECT Dim cn As New SqlConnection(ConnectionString.Text_) Dim cmd As New SqlCommand Dim Trans As SqlTransaction   With cmd                 .Connection = cn                 .Transaction = Trans                 .CommandType = CommandType.StoredProcedure                 .CommandText = "somestoredprocedure"             Dim xMLSerialiser_ As New System.Xml.Serialization.XmlSerializer ( Obj.GetType )                 Dim sWriter As New System.IO.StringWrit...

Adding Item to Generic List using Reflection

Say the object pX has a property which is a IList of something public property SomeListProperty as List of (AnotherClass) To add an item to this List, use this technique Dim pInfo As System.Reflection.PropertyInfo            pInfo = pX.GetType().GetProperty(“SomeListProperty")            Dim Obj As Object = pInfo.GetValue(pX, Nothing)            Obj.GetType().GetMethod("Add").Invoke(Obj, New Object() {InstanceofAnotherClass})

ASP.Net–Creating Controls Dynamically and Adding Events to it

Create a form and add a panel to it. Say pnlW In the code behind Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load     Try         Dim tx As New TextBox         tx.Text = "Hello"         pnlW.Controls.Add(tx) ‘textbox added         Dim bx As New Button         bx.ID = "btnX"         bx.Text = "Do something"         bx.ToolTip = "Button does something"         bx.CssClass = "somecssclass"         bx. CommandArgument = "somethingunique" ‘key lines         AddHandler bx.Command, AddressOf ...

Also seen wearing Chaddis

Shoe size chart

Excel Macro – Copy Sheets to Another File

Can use this Macro to copy data in multiple sheets into a unified single sheet in another excel file. Open the source file. Open a blank workbook and save it with some name eg: ‘target’. Keep both source and target files open. In the macro, replace ‘source’ with name of source file. Replace ‘target’ with name of the blank file. Sub CopyLoop()         Dim Ws As Worksheet         Dim i As Integer         Dim K As Integer         i = 2  ‘ first row of copyselection         ' Loop through all of the worksheets in the active workbook.         Windows("source").Activate         For Each Ws In Worksheets         K = K + 1          Ws .R...

ASP.net GridView CheckBox

Ancient piece of code that I keep forgetting. <asp:GridView ID="grdR" runat="server" AutoGenerateColumns="False"             DataKeyNames="Id" style="margin-left: 6px; font-size: x-small;" Font-Size="Small">             <Columns>                 <asp:TemplateField>                     <ItemTemplate>                         <asp:CheckBox ID="CheckBox1" runat="server" />                  ...

Siemens Washing Machine : Kill the Buzzer

The washing machine we have has the habit of sending out an irritating beep after it finishes the wash cycle. Ok enough; but these intermittent beeps go on and on and on till it is sure everyone came back home after your attending your funeral. One awful engineer who programmed the chip. Anyway, been looking around the net if there is a work around. Found this written by some Russian. I didn’t have patience to correct the grammar entirely. If the below does  not work, there is always the option of an Axe and ‘hey Siemens,..Heeeere is Johnyyy!) -- text You can change the volume of the buzzer according to your requirement. The operation procedure: 1. Switch on the machine,turn the program selector to Off . 2. Turn the program selector to cold Easy-care , press the additional function button Intensive stains and dont let go. You can hear the volume of the buzzer from minimum to maximum to off cycled (I didnt hear this!). If you decide the volume that yo...

UML Use Case

Do not try to make sense of it as an English word. Use of Case, Case of Use.. no no. Use Case title is the statement of the actor's goal in a verb phrase. e.g. “Admit Patient” Actor : Someone who has a goal in using the system. “Nurse”

Regular Expression for Numerics (Integer and Decimal)

Sometimes you need to validate your ASP.net web form user input for numeric values.. The following works for me Any integer, positive or negative ^[-+]?[0-9]*$ Validates -9 9 12 1352345234 –3434134 +34 Does not validate -9.6 +89.9 Any decimal/float value Being: positive or negative, specified maximum length, limited number of decimals ^[-+]?\d{0,12}(\.\d{1,2})?$ Validates 123456789012.34 -123456789012.34 +123.45 D0es not validate 1234567890123 1234.123

Reversing Yakov Smirnoff

Truncate LOG files of SQL Server

USE tmssmall GO DBCC SHRINKFILE(tmssmall_log, 1) BACKUP LOG tmssmall WITH TRUNCATE_ONLY DBCC SHRINKFILE(tmssmall_log, 1) GO   tmssmall is name of database So… copy the above text entirely to Notepad Ctr+H Find what is ‘tmssmall’ Replace with is ‘yourdatabasename’ OK. Copy the new text and execute query Save space!!

Datatable Grouping in .Net 2.0

This functionality is missing in the GridView control. There are various ways to go about it. I thought I will do it without Ajax or other overheads. The following code takes in a Datatable as input and returns the grouped Datatable as output. Make the output datatable the source of any gridview! The parameters allow multi column grouping and sorting becomes -----------------------------------------------------------------------   Public Shared Function GroupDataTable(ByVal T1 As DataTable, ByVal GroupByColumns As String(), _     Optional ByVal SortOrder As Boolean() = Nothing, _     Optional ByVal SumColumns As String() = Nothing, _     Optional ByVal HeaderCell As Integer = 0 _     ) As DataTable         Dim T2 As New DataTable         Dim sortStr As String = ""       ...