Monday, June 23, 2008

myString.Replace("Blah", "Blah Blah") - DONT TRY THIS AT HOME! Strings are immutable!

I hope I can save someone some time from this little mind boggler....

I had a string, I wanted to change one particular part of the string... NO PROBLEM -- use .Replace right? well, sort of.

Strings are immutable objects and cannot be changed -- so saying

msgText.Replace ("5 of 6", "3 of 5")

may look good and will not even give you an error (unfortunately) BUT it also WILL NOT WORK!

You have two options... create a different string to house the Replaced string OR create a stringBuilder.

I chose to use a new string and the .Replace worked great.

myNewText = msgText.Replace ("5 of 6", "3 of 5")

all is well again.

Monday, June 16, 2008

SENDKEYS -- Copying to clipboard - open Wordpad - Paste clipboard all in code!

I think this is cool and I learned several useful tools in the process. I have plans on creating a sort of graph in a form.

But I wanted to give the user's a chance to print it --- without getting to detailed, I figured that I would grab a screen shot of the form, open Wordpad (which everyone should have), and then paste it within wordpad.

I figured they could take it from there....

So how did I do it?



First off, grab the active part of the screen using SENDKEYS with the sendwait method. I put this in a FUNCTION called screenCapture:

Public Function screenCapture() As Image

'CALL THE SENDKEYS WITH THE PRINT SCREEN METHOD -- '%' = ALT
SendKeys.SendWait("%{PRTSC}")

'CREATE AN OBJECT TO HOLD THE IMAGE - IN CASE YOU WANT TO USE IT SOMEWHERE ELSE IN YOUR CODE
Dim captImage As IDataObject = Clipboard.GetDataObject

'RETURN THAT IMAGE AND FORMAT IT AS A BITMAP - AGAIN, TO USE SOMEWHERE ELSE IN CODE (you dont need this to paste -- its ALREADY ON the clipboard)
Return captImage.GetData(DataFormats.Bitmap)

End Function
------

Now from within the button click that called the screen capture function we send a Shell command to open the WORDPAD and then use SENDKEYS again to call Control and V, thus pasting the clipboard straight into Wordpad.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Call the function
screenCapture()

'Open WordPad
Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe", vbNormalFocus)

'Use SENDKEYS AGAIN TO CTRL+V ("^" = CTRL)
SendKeys.Send("^+{V}")

End Sub

Thats about it!!! In about 2 minutes you can drag a button to your form, include the above code and you have your very own screen capture functionality!!!!!

A couple other cool things you could do is SAVE the capture -- since the function call is an image you could write this:
screenCapture().Save("c:\screenCap.bmp", System.Drawing.Imaging.ImageFormat.Bmp)

You could also assign the captured image to a Picture Box on your form:
PictureBox1.BackgroundImage = screenCapture()

*I used Background image rather than Image because I can set the Layout of background to Stretch thus assuring that we see the entire image....

Pretty cool stuff and very quick and simple!!!

** GO SENDKEYS!

Thursday, June 12, 2008

Merging two dataTables

In this method I create a TEMP Table because I have two tables of information that I need to merge together (some names and their matching data) ---so I create a new table and clone it -- Clone copies the data structure of one table to another.
Dim tempTable As DataTable

'I clone the normal table that we need so I can insert the proper users later.
tempTable = dataset1.Tables(1).Clone

THEN we start going through our two tables and merging the users with their data-- INTO our temp table.
For iRowCount = 0 To dsReports.Tables(0).Rows.Count - 1

'Create the teachername for the rows
If Not dataset1.Tables(0).Rows(iRowCount)("fname") Is DBNull.Value Then teachername = dataset1.Tables(0).Rows(iRowCount)("fname")

If Not dataset1.Tables(0).Rows(iRowCount)("lname") Is DBNull.Value Then teachername = teachername + " " + dataset1.Tables(0).Rows(iRowCount)("lname")

'Now iterate through the other table and match the names via the matching ID
For iRowCount2 = 0 To dataset1.Tables(1).Rows.Count - 1

If dataset1.Tables(0).Rows(iRowCount)("sessid") = dataset1.Tables(1).Rows(iRowCount2)("session") Then

tempTable.ImportRow(dataset1.Tables(1).Rows(iRowCount2))
tempTable.Rows(tempTable.Rows.Count - 1)("teachername") = teachername

End If

Next

Next

FINALLY, we remove the two seperate tables that are no longer needed and we insert our finished temp table that will work with the rest of the code.

dataset1.Tables.Remove(dataset1.Tables(0))

dataset1.Tables.Remove(dataset1.Tables(1))

dataset1.Tables.Add(tempTable)

Wednesday, June 11, 2008

STRINGS and their stuff.

GET RID OF TRAILING COMMA:
(you have built something that leaves you with a string with a comma at the end and you want it gone -- use Left minus 1 of the length of string!)

dim myString as String = "1,2,3,4,5,"
myString = Left(myString, myString.Length - 1)
RESULT: myString = "1,2,3,4,5"


more to come...

Random (yet useful) SQL Commands

CHECKING IF A FIELD IS NULL:
SELECT lastName, ID
FROM tbl_customers
WHERE lastName IS NULL OR ID IS NULL
** THIS statement pulls all rows that either the lastName OR the ID is null. **

You could also do this if you wanted to replace NULLs with something:
WHERE ISNULL(lastName, 'test')
*This replaces all lastName 's that are null with the word 'test'



USING LIKE AND WILDCARDS:
There's a million uses for the LIKE keyword.

Use LIKE for incomplete words
Use % for anything after or before
Use _ for a single character

ex:
SELECT * FROM tbl_people WHERE lastname LIKE 'A%' - everyone who's last name starts with an A
SELECT * FROM tbl_people WHERE lastname LIKE '%A%' - use this to choose anything before and after.
SELECT * FROM tbl_people WHERE lastname LIKE 'Anders_n' - uses the _ wildcard which will ONLY give ONE char - so here you would ONLY get Anderson's or Andersen's but never Andershen's (because we all know how many Andershen's there are out there!)

Finally - dont forget that we can use the NOT keyword in conjuction with LIKE -- so if you DIDNT want to see anyone whose names started with Ander (like those dreaded Andershen's!) - we could write.
SELECT * FROM tbl_people WHERE lastname NOT LIKE 'Ander%'
more to come...

Nullable Types - and their methods.

The Nullable type allows us to hold Value types which are not allowed to hold nulls(as reference types are) to be able to have the state of NULL.

You declare a nullable like this:
Dim intNullable As Nullable(Of Integer)

Now this Integer type can be Null -- this is handy for several reasons such as if one were reading in values from a database where the particular integer column MAY hold a null value.
However, the confusion with this object comes when the poor unsuspecting sap tries to test the value of this variable.

The most obvious method .Value (intNullable.Value) will give you the value UNLESS it is currently NULL and then it will throw an InvalidOperationException!

OK, so what we have to do is call the .HasValue method first. This returns true or false based upon value or NULL.

So you COULD call .HasValue and then if True call the .Value.

HOWEVER, their is another method called .GetValueOrDefault. This method combines both .HasValue and .Value. This handy method will give you a '0' if the value is Null or the value of the variable.

Which begs the question in my mind... WHY EVEN HAVE THE FIRST TWO METHODS?
**I'm sure there is an explanation and would love to hear it if you know.

Anyway -- that's the Nullable nugget of the day.

Make a Database Diagram in SQL2005.

If you have a good sized database like I do and you need to see a visual snapshot of how the tables relate to each other as well as all the columns, then use the Database Diagrams option under the database!

This might seem like an OBVIOUS statement but believe it or not, I recently forgot how in the world I got this nice little printed out diagram of my database which hangs near my desk! (so sad)

Under your database, there will be a folder named 'Database Diagrams' - you can right click on this folder and add a new diagram. This is nice because you can arrange the tables of your database just how you like them (expanding or contracting) -- change the size, etc.

For me, I made my own diagram...enlarged so that it fit on four pages of paper. I printed off the those pages then was able to go to a larger printer and print a larger poster style (we have a very large database) .

Again, this was extremely handy because I am a visual learner and I absolutely needed this diagram to understand how our data related.

Tuesday, June 10, 2008

Formatting a date --- putting a zero in front of the single months and days...ARGH!!

This burns me... when something that should be so easy become soooo hard... .and even worse, knowing you found the answer once, then losing it and having to search ALL OVER AGAIN!!!!

So I'm blogging it -- hope I dont forget how to find this stupid blog!

** So the answer is that you have to turn your DATE into a STRING if you want to keep leading zero's. So you have a date - you then need create a string variable (from what I can gather DATES will always get rid of the zero's) - THEN you can format that date as such:

Dim DateString As String = Date.ToString("MM/dd/yyyy")

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

MM - adds the zero to the front of month (lower case is for minutes)
dd - does the same to the day (no uppercase)