Category Archives: VB.NET

ASP.NET: Delete file from server after TransmitFile command

It’s a common scenario: user needs to download a file from the server by clicking link or a button in your ASPX page. The server-side code for this is pretty straightforward and looks something like this:

Sub DownloadFile(ByVal i_sServerPath As String, ByVal i_sDisplayName As String)
   Dim oFile As FileInfo = New FileInfo(i_sServerPath)

   With Response
      .Clear()
      .ClearHeaders()
      .AddHeader("Content-Length", oFile.Length.ToString())
      .ContentType = ReturnContentType(oFile.Extension.ToLower())
      .AddHeader("Content-Disposition", "attachment; filename=" & i_sDisplayName)
      .TransmitFile(oFile.FullName)
      .End()
   End With

End Sub

The code accepts 2 parameters: i_sServerPath – full path to the file on the server and i_sDisplayName – file name that will be displayed to the user in the “Save As” dialog. Code also populates response headers based on file information. I use this handy function to populate ContentType based on file extention:

Function ReturnContentType(ByVal i_sfileExtension As String) As String
   Select Case i_sfileExtension
      Case ".htm", ".html", ".log" : Return "text/HTML"
      Case ".txt" : Return "text/plain"
      Case ".doc" : Return "application/ms-word"
      Case ".tiff", ".tif" : Return "image/tiff"
      Case ".asf" : Return "video/x-ms-asf"
      Case ".avi" : Return "video/avi"
      Case ".zip" : Return "application/zip"
      Case ".xls", ".csv" : Return "application/vnd.ms-excel"
      Case ".gif" : Return "image/gif"
      Case ".jpg", "jpeg" : Return "image/jpeg"
      Case ".bmp" : Return "image/bmp"
      Case ".wav" : Return "audio/wav"
      Case ".mp3" : Return "audio/mpeg3"
      Case ".mpg", "mpeg" : Return "video/mpeg"
      Case ".rtf" : Return "application/rtf"
      Case ".asp" : Return "text/asp"
      Case ".pdf" : Return "application/pdf"
      Case ".fdf" : Return "application/vnd.fdf"
      Case ".ppt" : Return "application/mspowerpoint"
      Case ".dwg" : Return "image/vnd.dwg"
      Case ".msg" : Return "application/msoutlook"
      Case ".xml", ".sdxl" : Return "application/xml"
      Case ".xdp" : Return "application/vnd.adobe.xdp+xml"
      Case Else : Return "application/octet-stream"
   End Select
End Function

It’s all good, but what if you need to delete the file from the server immediately after user downloaded it? Continue reading →

UltraWebMenu: When background doesn’t change on hover in IE9

If you tried to use Infragistics classic UltraWebMenu control in IE9 you may experience issue (even in the latest version, 11.2 at the time of this post) whereby menu items don’t change background on mouse hover even though background is specified in menu’s HoverItemStyle property.

The solution is specify BorderStyle in HoverItemStyle. It can be any value besides NotSet, but the actual attribute has to be there. So for example if you want your hover style to have no borders and your original style looks like:

<HoverItemStyle
   ForeColor="White" 
   BackColor="#81C0E9"
   Height="18px"
   BorderWidth="0px"> 
</HoverItemStyle>

change it to

<HoverItemStyle
   ForeColor="White"
   BackColor="#81C0E9"
   Height="18px"
   BorderWidth="0px" 
   BorderStyle="None">
</HoverItemStyle>

I don’t know why border style affects showing of the background, but there you have it. Adding BorderStyle to HoverItemStyle will enable displaying of background color on hover.

VB for Android

Found this gem today:

Basic 4 Android

It’s a Visual Basic IDE environment for developing Android apps. But unlike other similar solutions it does not require bloated runtime running on the device, Basic4Android easily compiles native APK app.

Don’t learn Java, utilize your existing Visual Basic skills instead. And the community of thousands of developers can be a huge help as well.

Also you’re in luck today. Download the trial, play around with it and if you like it – use discount code “bvqbet” to get 50% off any version! Here’s how:

  1. Visit purchase link: http://www.basic4ppc.com/android/purchase.html
  2. Select Plimus as your checkout option
  3. Enter coupon code bvqbet in the coupon code field
  4. Profit! You get a 50% discound off a regular price

Happy coding!

UltraWebGrid bug: Row is selected on mouse move

Submitted for your approval an UltraWebGrid with CellClickActionDefault=”RowSelect” and SelectTypeRowDefault=”Single” – an ordinary down-to-earth grid. It also posses event handler AfterSelectChangeHandler, also nothing out of the ordinary. But in a minute the aforementioned grid will exhibit properties most unusual. As the alert message ahead reads: Infragistics Bug Continue reading →

WHDG: RowIslandsPopulating event fires multiple times

I’ve been successfully using manual load on demand in WebHierarchicalDataGrid for a while now, but recently noticed strange thing. The deeper in grid’s hierarchy I expanded the children – the slower it went.

In my case every time user clicks [+] to expand a row, VB.NET code calls an SQL Server Stored procedure to bring in child rows. I grew suspicious and fired up SQL Profiler. What I saw surprised me. Number of calls to the stored procedure increased the deeper in grid’s hierarchy I expanded the children. When I clicked [+] on the root level it resulted in 1 SP call. Clicking [+] on the child to expand grandchild – 2 calls. Expanding grandchild to see grand-grandchild rows – 3 calls, etc. Continue reading →

WebHierarchicalDataGrid: Extra Row after Update

If after upgrading to a new version of Infragistics NetAdvantage you suddenly found your WHDG sprouting an extra blank row on top:

Extra Row in Aikido WHDG

most likely it’s because grid’s ItemCssClass property is used. In theory (at least according to ever so verbose documentation) it should define what grid’s cells look like. In practice it have no effect whatsoever. Or rather had no effect until upgrade (verified in version 2011.1, perhaps even earlier). Now if your CSS class used in this property contains HEIGHT attribute – a blank row of that height will be inserted on top of the grid.

Solution? Remove ItemCssClass property. It’s useless anyway.

WebDataMenu: Incorrect displaying after upgrade

This is the second post in saga titled Upgrading Infragistics Controls to a new version. Chances are that your WebDataMenu looks weird after upgrade to 2010+ version. In my case the menu had following options/features:

  • It was a context popup menu, called on right mouse click
  • Text of menu items was assigned dynamically at runtime in JavaScript Client code

After upgrading NetAdvantage from version 2009.2 to 2011.1 strange things started to happen. Text of the menu items was cut short, submenues appeared at wrong places it looked like something from a Dali’s painting.
Numerous experiments later I found out that the problem was with EnableScrolling property of the menu control. Setting it to False returned menu to realm of realism.

WebHierarchicalDataGrid: JavaScript errors after upgrade

Upgrading 3rd party library to a new version is bound to have problems and Infragistics is no exception. In my case I was upgrading NetAdvantage for ASP.NET from version 2009.2 to to 20011.1 and right away WebHierarchicalDataGrid started to crash client-side. If ScriptManager was in debug mode I’d get error:

Microsoft JScript runtime error: Sys.ArgumentUndefinedException: Value cannot be undefined.Parameter name: type

With ScriptManager in Release mode it’d be:

Microsoft JScript runtime error: Object expected

But always in Sys.Component.Create – it looked like grid’s client-side scripts weren’t loading at all. After A LOT of digging I found out that the culprit was grid’s server-side Bands.Clear() method. When called, it caused client-side WHDG JavaScript not to load. When that method was commented – JavaScript errors disappeared. So until Infragistics comes out with a bug fix – if you experience similar problem, try to avoid Bands.Clear() method.

Pass NULL value to SSRS Report from ReportViewer

When working with ReportViewer ASP.NET control to display SSRS report on an ASPX page you can use ReportParameter class to create and pass parameters to the server, e.g.

aParamList.Add(New ReportParameter("MyParam", sValue, False))

Here aParamList is a Generic List of ReportParameter objects that you use in ReportViewer’s ServerReport.SetParameters method, "MyParam" is a parameter name and sValue is a string variable with value for the parameter. Third parameter – visibility, when set to False makes the parameter hidden.

All is well, but what if you need to pass NULL parameter? Unfortunately ReportParameter‘s constructor accepts only string type for it’s value parameter, so you cannot pass something like DBNull.Value or Nothing. Fortunately there’s an easy solution: Just set you string variable representing value to Nothing:

sValue = Nothing
aParamList.Add(New ReportParameter("MyParam", sValue, False))

Voila! Desired NULL is passed as a parameter value (on SSRS side parameter has to be set to accept NULL values).

Thanks Tincy for the original idea.

Prevent Cross Site Scripting thru hidden fields

ASP.NET protects you pretty good when user tries to post a malicious content thru an entry form. With @page directive “ValidateRequest=true” (which is true by default) if anybody attempts to enter something like <script type=”text/javascript”>…nasty stuff here…</script> the page will throw HttpRequestValidationException exception: A potentially dangerous value was detected from the client…

But this does not work for some form elements, for example framework variables such as __EVENTVALIDATION or __VIEWSTATE over which user or developer do not have direct control. A maliciously crafted POST request can insert JavaScript into those elements which then can execute in user’s browser. Yes the page may crash because of invalid data, but JavaScript is still echoed to the client and gets executed. In this case ASP.NET needs a little help. Continue reading →