Archive for the 'Coding' Category

Mutiple random numbers in VB .Net

The .Net Random class works well on its own, if you just need one random number. By default it uses the system time for the seed value. Where problems occur is when you need multiple random numbers inside a loop.

When I called Random.Next within the loop, it would always return the same number, as the seed value was always the same, even using the millisecond property of Now().

My solution was to multiply the Random seed (milliseconds) by the loop value which resolved the issue.

  1. Public Shared Function GetRandomNumber( _
  2. ByVal iLowerBound As Integer, _
  3. ByVal iUpperBound As Integer, _
  4. Optional ByVal iSeedMultiplier As Integer = 1) As Integer
  5.  
  6. Dim Random As New Random(Now.Millisecond * iSeedMultiplier)
  7. Return Random.Next(iLowerBound, iUpperBound + 1)
  8. End Function
  9.  
  10. Example:
  11.  
  12. For i = 1 To 5
  13. strKey = i.ToString & "|" & GetRandomNumber(1, 10, i).ToString
  14. Next

Column headers in Studio Express 2005

One big hassle in Sql Query Analyzer was the inability to copy the column names from the result set. You could get the column names through Enterprise Manager.

This appears to have been resolved in Studio Express where you just need to set the option to copy the column header names:

Column names

Datagrid with fixed width columns

I created a dynamic calendar using a datagrid with a width of 700px with each of the seven days set to 100px. I thought this would keep days in a fixed width column, but that was not the case.

I had to add the following to my code and then the columns stayed fixed:


dgrCalendar.Style.Add("table-layout", "fixed")

Dynamically set datagrid HeaderText

I am creating a ASP .Net page to maintain all of my lookup tables, rather than creating a separate page for every 2-4 field table.

Initially I thought I would change the headertext property during the datagrids OnItemDataBound event, checking for ListItemType.Header, etc. This proved a bit convoluted with the resolution being much simpler:

dgrLookupTable.Columns(1).HeaderText = "Source"

The ASP code looks like this:

  1. <asp:TemplateColumn SortExpression="1" HeaderText="">
  2. <HeaderStyle Width="150px"></HeaderStyle>
  3. <ItemStyle Wrap="False"></ItemStyle>
  4. <ItemTemplate>
  5. <span class="dgrOverflow">
  6. <%#Container.DataItem("1")%>
  7. </span>
  8. </ItemTemplate>
  9. <EditItemTemplate>
  10. <ASP:TextBox Size="40" id="txt1" Text='<%# Container.DataItem("1")%>' runat="server" />
  11. </EditItemTemplate>
  12. </asp:TemplateColumn>

1.1. datagrid in 2.0 .Net environment

My ISP recently migrated all 1.1 accounts to 2.0. All the 1.1 code seems to work fine. One bizarre thing was that the gridlines in all of my datagrids turned to white, which makes them invisible against a white background.

I found a couple posts that said if you made bgcolor=white, you would then be able to see the gridlines. I tried that and it did work, but obviously not a viable solution.

Turns out to be very simple. You need to set the bordercolor attribute of the datagrid in code:

dgrCalendar.Attributes.Add("bordercolor", "Black")

Initial thread

Extracting ViewState and other ASP .Net params

This is a very simple function for extracting any ASP .Net page state value such as __VIEWSTATE or __EVENTVALIDATION. These values are necessary when scraping some websites.

  1. Public Shared Function ExtractDotNetData(ByVal strData As String, ByVal strStateName As String, ByVal strDelimiter As String) As String
  2.  
  3. Dim iStateNamePosition As Integer = strData.IndexOf(strStateName)
  4. If iStateNamePosition < 0 Then Return ""
  5.  
  6. Dim iStateValuePosition As Integer = strData.IndexOf(strDelimiter, iStateNamePosition)
  7. If iStateValuePosition < 0 Then Return ""
  8.  
  9. Dim iStateStartPosition As Integer = iStateValuePosition + strDelimiter.Length
  10. Dim iStateEndPosition As Integer = strData.IndexOf("""", iStateStartPosition)
  11.  
  12. Return HttpUtility.UrlEncodeUnicode(strData.Substring(iStateStartPosition, iStateEndPosition - iStateStartPosition))
  13. End Function
  14.  
  15. strViewState = ExtractDotNetData(strResponseData, "__VIEWSTATE", "value=""")
  16. strEventValidation = ExtractDotNetData(strResponseData, "__EVENTVALIDATION", "value=""")

File permission error in .Net 2.0

If you are moving a .Net 1.1 app to a server with 2.0, you may need to add the param

<trust level="Full" originUrl="" />

to your web.config if you are doing any sort of file upload.

There may be a more elegant way, but this was the quickest resolution I could find.

« Previous Page