This post has been imported from the old blog and has not yet been converted to the new syntax yet.
Something not .NET related, regarding VB6.

Here is some strange behaviour:

Create a new .EXE project with the following References and Components:

References : Microsoft DAO 3.51 Object Library
Components : Microsoft Data Bound Grid Control 5.0 (SP3)

Add a DBGrid to your form with it's DataSource set to datQuery (Data).

Add a textbox and a button as well, and put this code in it:

[vb]
Option Explicit

Private Sub cmdExecute_Click()
Call LoadTable(txtquery.Text)
End Sub

Private Sub LoadTable(strQuery As String)
Dim Db As Database, Rs As Recordset

Set Db = DBEngine.Workspaces(0).OpenDatabase(dbPath)
Set Rs = Db.OpenRecordset(strQuery)
Rs.MoveLast
Rs.MoveFirst
Set datQuery.Recordset = Rs

Me.Caption = "Query - Colums: " & Rs.Fields.Count & " - # Records: " & Rs.RecordCount
End Sub
[/vb]

Run your code, and this is what it should look like:



Now, go to the Custom properties of the DBGrid, to the Layout tab and uncheck 'AllowSizing'.



Run your program again, and now your DBGrid will indicate there are results, but there will be no text, like this:



Why is it doing this? I really have no idea. But the best part is, when you check the AllowSizing property again, it isn't back to normal. It stays broken.

This 'bug' (don't know if it's a bug) was encountered some days ago, I reproduced it this afternoon, and now I installed VS6 SP6, but the bug remains.

If anyone wants a project which has the bug in it, I uploaded something small (30Kb) that got destroyed by this.

Anyone who has encountered this as well? And perhaps has an explanation of why it's doing this.

Update: Just when I posted this and looked at my post again, I noticed something, the second screenshot only had 2 columns, so I realized it did fetch the records, but didn't knew where to display the data. And when you right click and choose 'Clear Fields' it works again. But the AllowSizing is automatically checked on again. To add some value to this post, maybe anyone knows how to uncheck AllowSizing without the grid resetting it's own columns?
 
This post has been imported from the old blog and has not yet been converted to the new syntax yet.
I'm proud. I've just been included in the MSDN list of Belux bloggers!

And I also got linked in the March issue of MSDN Flash! :)

I'm happy to be a part of such a big community, really makes you feel appreciated once. Thanks to everyone out there!
 
This post has been imported from the old blog and has not yet been converted to the new syntax yet.
In response to a thread on the ASP.NET Forums I decided to publish the answers in an overview on my blog.

Someone asked on what the difference were between PHP and ASP.NET and how he did certain things.

My answers are based on using C# as the language for your ASP.NET application.

__________________________________

1. Is there any difference between '' and "" in ASP.NET?

Yes, ' ' is used for characters while " " is for strings.

Take this for example:
'a' = a in memory
"a" = a and \0 in memory

Something useful about escaping in ASP.NET is the following:
@"bla\bla" == "bla\\bla"

A string with an @ in front of it is seen as a literal, where you don't have to escape special characters.

2. How would I do something like: print $variable.'string'; in ASP.NET?

You would use the following: Response.Write(variable + "string");
But it isn't very recommended to use Response.Write, take a look at all the server controls you have, like a Label for example.

3. Within an if language construct, what would be the equivalent of "and" and "or?"

Actually PHP supports the same being used in ASP.NET:

and --> &&
or --> ||

Example:
[csharp]
if (((number == 5) && (number2 != 8)) || (admin == true)) {

// do stuff

}
[/csharp]

Would correspond to:
if (number = 5 AND number2 NOT equal to 8 ) OR we're an admin., do stuff.

4. Would someone be able to tell me how I could make an ASP.NET equivalent of this PHP Function:

[php]
function num($number){
return '#' . str_pad($number, 3, '0', STR_PAD_LEFT);
}
[/php]

You need a function which returns a string, and which formats a string:

[csharp]
public string num(int number) {
return String.Format("#{0}", number.ToString("000"));
}
[/csharp]

5. How do I create a mutidimensional array?

Try this:

[csharp]
// 2 dimensional arrays
int [,] a1;
a1 = new int[3,2];
int [,] a2 = {{1,2}, {3,4}, {5,6}};

// jagged arrays
int[][] a3;
a3 = new int[3][];
a3[0] = new int[5]{1, 2, 3, 4 ,5};
a3[1] = new int[3];
a3[2] = new int[4]{21, 22, 23, 24};
[/csharp]

Loop over them to see the elements.

__________________________________

Got a question yourself? Ask it, and I'll try to help :)
 
This post has been imported from the old blog and has not yet been converted to the new syntax yet.
Here I am again, with the last article for my birthday gift to you guys :)

This time we create our own WordWrapper method, inspired by an ASP.NET Forums question.

The last one for today: Custom WordWrapper.
 
This post has been imported from the old blog and has not yet been converted to the new syntax yet.
This article is about a question from chopps on the ASP.NET Forums. He wanted to wordwrap a text to a certain width.

It seemed something simple and easy to answer, but I decided to check on Google if anyone else had a good resource on this. Without any luck, I found remarkably little info on creating your own wordwrap method, so I decided to write it myself in C#. He was happy with the answer, I cleaned the code up a bit, and here's the article about it :)


We begin with creating two projects. One Console App (WordWrapDriver) and one Class Library (WordWrap). Reference the library in the console app.

[csharp]
using System;
using WordWrapper = WordWrap.WordWrap;

namespace WordWrapDriver {
class WordWrapDriver {
static void Main(string[] args) {
// TODO
} /* Main */
} /* WordWrapDriver */
} /* WordWrapDriver */
[/csharp]

Our wordwrapper will be a static method called Wrap. We're going to support a given width and a prefix (for example "> " to prefix like a reply mail).

[csharp]
using System;
using System.Collections;

namespace WordWrap {
public class WordWrap {
#region Public Methods
public static string Wrap(string originalText, int maxWidth) {
return Wrap(originalText, maxWidth, "");
} /* Wrap */

public static string Wrap(string originalText, int maxWidth, string preFix) {
// TODO
} /* Wrap */
#endregion
} /* WordWrap */
} /* WordWrap */
[/csharp]

Let's begin. In the Wrap method we pass our text as a string to a Wrapper method, which gives us a string array that we loop through and prefix.

[csharp]
public static string Wrap(string originalText, int maxWidth, string preFix) {
string[] wrappedText = Wrapper(originalText, maxWidth);

string wrappedBlock = "";
foreach(string textLine in wrappedText) {
wrappedBlock = String.Format("{0}\n{1}{2}", wrappedBlock, preFix, textLine);
}
wrappedBlock = wrappedBlock.Substring(1);
return wrappedBlock;
} /* Wrap */
[/csharp]

So what does Wrapper do? First we filter out \r\n and \r, after we split our text on \n to create 'blocks'.

[csharp]
originalText = originalText.Replace("\r\n", "\n");
originalText = originalText.Replace("\r", "\n");
originalText = originalText.Replace("\t", " ");
string[] textParagraphs = originalText.Split('\n');
[/csharp]

Now we take each paragraph and check if it's smaller then our maxWidth or not, if it isn't we just add the line. If it is longer we'll have to break it up in separate lines.

[csharp]
for (int i = 0; i < textParagraphs.Length; i++) {
if (textParagraphs[i].Length <= maxWidth) {
// Block of text is smaller then width, add it
textLines.Add(textParagraphs[i]);
} else {
// Block of text is longer, break it up in seperate lines
string[] pLines = BreakLines(textParagraphs[i], maxWidth);
for (int j = 0; j < pLines.Length; j++) {
textLines.Add(pLines[j]);
}
}
}
[/csharp]

And lastly we return our lines.

[csharp]
string[] wrappedText = new string[textLines.Count];
textLines.CopyTo(wrappedText, 0);
return wrappedText;
[/csharp]

The only method remaining is the BreakLines method. Here we split up our string into seperate words. With those words we re-create the text, but with the correct width. Therefore we loop through every word and add it to a temporary string, which we check against the given maxWidth. If it's smaller, we continue, if it's not we add the temporary string we had just before adding the word, and we continue, the word causing the 'overflow' will be the first word of the new line. If we have a line that is inside our required length, we check if it's the last word and if that's the case we add our temporary string.

[csharp]
while (wordIndex < textWords.Length) {
if (textWords[wordIndex] == "") {
wordIndex++;
} else {
string backupLine = tmpLine;
if (tmpLine == "") {
tmpLine = textWords[wordIndex];
} else {
tmpLine = tmpLine + " " + textWords[wordIndex];
}

if (tmpLine.Length <= maxWidth) {
wordIndex++;
// If our line is still small enough, but we don't have anymore words
// add the line to the collection
if (wordIndex == textWords.Length) {
textLines.Add(tmpLine);
}
} else {
// Our line is too long, add the previous line to the collection
// and reset the line, the word causing the 'overflow' will be
// the first word of the new line
textLines.Add(backupLine);
tmpLine = "";
}
}
}
[/csharp]

And here as well, we return our lines.

[csharp]
string[] textLinesStr = new string[textLines.Count];
textLines.CopyTo(textLinesStr, 0);
return textLinesStr;
[/csharp]

In the WordWrapDriver I added some test strings to test out the WordWrapper.

[csharp]
string strText = "The\t\tquick brown fox jumped over the lazy dog bla.";
Console.WriteLine("20 chars, prefixed example.");
Console.WriteLine(strText);
Console.WriteLine();
Console.WriteLine("> #################### (20)");
Console.WriteLine(WordWrapper.Wrap(strText, 20, "> "));
Console.ReadLine();
[/csharp]

And here are the sources as usual. Hope you find it usefull.