Microsoft Visual Studio – How to remove last character(s) from a string

Let’s say I am concatenating a string with values from a collection. Every time I add a value, I want to append with a comma (,) and a space to separate. I don’t know at run time how many objects are in my collection, so when I am done, I need to remove the last comma (,) and space. The following code shows how the string is  built, what it may look like when completed, and how to remove the last two characters – comma (,) and a space.

Dim mHobbies As String = String.Empty
Dim pObj as Object
For each pObj in myHobbyCollection
 With pObj
      mHobbies= mHobbies & .hobby & ", "
 End With
Next

mHobbies may contain the following:

Music, Reading, Swimming, Kite Flying,  

The following code will remove the last two characters:

mHobbies = mHobbies.Remove(mHobbies.Length – 2)

mHobbies will now look like this:

Music, Reading, Swimming, Kite Flying

Microsoft Visual Studio – ASP.Net Dynamic and Static Hover Style Conflict in IE-9 Browser

When working with a menu control, I found that Internet Explorer 9 can’t handle setting both the static and dynamic hover style properties of the menu control. It works fine in Chrome and Firefox, but not IE-9.

To make IE-9 compatible, add the following meta tag within your <head> tag of your aspx page that contains the menu control.

<meta http-equiv=”X-UA-Compatible” content=”IE=9″/>

<%@ Page Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
 <title>Menu Server Control</title>
 <meta http-equiv="X-UA-Compatible" content="IE=9"/>
</head>
<body>
 <form id="form1" runat="server">
 <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
 <asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1">
 <StaticHoverStyle BackColor="Green" BorderColor="Black" BorderStyle="Solid" 
 BorderWidth="5"></StaticHoverStyle>
 <DynamicHoverStyle BackColor="DarkGray" BorderColor="Black" BorderStyle="Solid" 
 BorderWidth="1"></DynamicHoverStyle> 
 </asp:Menu>
 </form>
</body>
</html>

 

 

 

Google Chrome – RealPlayer plugin does not work

Ask RealPlayer: Download This Video in Google’s Chrome Browser

by REAL TEAM on AUGUST 10, 2012

UPDATED OCTOBER 11, 2012: Chrome 22 (released on Sept. 26, 2012) does not currently support our Download This Video feature. At this time, the only solution is to use Internet Explorer or Firefox for downloading videos with RealPlayer.

We love hearing from users of our products. When features aren’t working the way you think they should, we want to help. That’s the idea behind Ask RealPlayer. We’re going to be providing answers to those questions straight from our product experts. Think of Ask RealPlayer as your buddy that works at RealPlayer. Please leave your questions below and we’ll address them in the comments or in our next installment of Ask RealPlayer.

Q: Why doesn’t “Download This Video” work in Google’s Chrome broswer?

A: Chrome has recently been updated. Among the few changes that were rolled out in the update, Pepper Flash has been implemented to handle Flash video. You can learn more about the decision to implement Pepper Flash in this post from Google’s Chromium blog.

As a result of the update, you may have recently started having problems with RealPlayer’s Download This Video button in the browser such as it  doesn’t appear when viewing videos in Chrome 21, or maybe you can see the button, but nothing happens when you click it, the following steps should resolve the problem. In order to fix the issue, please do the steps in both sections and close and reopen Chrome before trying the download again.

First: Disable Pepper Flash

1. Open a Google Chrome browser window.

2. In the address bar enter chrome://plugins/

3. Click the Details link in the top right corner.

4. The top entry should show three versions of Flash. Click Disable for the first one (you should see the Location listed as C:UsersuserAppDataLocalGoogleChromeApplication21.0.1180.60Pe perFlashpepflashplayer.dll).

5. Close, restart Chrome, and try downloading a video from your favorite video site.

SQL Server – Alternative to using ‘Case When’ in Where clause of SQL Statement

I came across a scenario where I needed to write conditional statements in my WHERE clause of my SQL that was more complex than what a ‘CASE WHEN’ statement could provide.

I have two parameters, @FieldA and @FieldB. Both parameters can have a value of either ‘A’, ‘Y’, or ‘N’.

If the value = ‘A’, I want to basically bring back all records as if I am not using a filter at all as if to say where the column on the table >=0.

If the value = ‘Y’, I want to bring back rows where the column on the table > 0.

If the value = ‘N’, I want to bring back rows where the column on the table = 0.

Here is my table:

Running the following SELECT statements (without using the parameters mentioned above) brings back these result counts.

Now, I need to build one SQL statement using the the two parameters mentioned above to yield the same results.

Instead of using a ‘CASE WHEN’ statement, you simply examine your parameter in your WHERE clause, and depending on parameter value, you follow the evaluation with the ‘AND’ operand and the fully qualified filtered statement that includes the database column, the operator, and the value to compare.

SELECT FieldA, FieldB
FROM test
WHERE
(((@FieldA = 'Y' and Fielda > 0)
 OR (@FieldA = 'N' and Fielda = 0)
 OR (@FieldA = 'A' and Fielda >= 0))
 AND
 ((@FieldB = 'Y' and FieldB > 0)
 OR (@FieldB = 'N' and FieldB = 0)
 OR (@FieldB = 'A' and FieldB >= 0)))

As you can see from the above statement, the outcome of the WHERE clause would look something like this:

SELECT FieldA, FieldB
FROM test
WHERE Fielda ? 0 AND FieldB ? 0

The ? would be translated to be either ‘>’,   ‘=’,   or   ‘>=’   depending on the value of your parameters.

So as you can see, ‘CASE WHEN’ can be very limited where the above solution allows you to fully qualify your conditional statements.

SQL Server Reporting Services (SSRS) – Set CacheDataForPreview = False

Have you ever witnessed running a report in the Business Intelligence Development Studio only to see data that you know does not exist anymore in your SQL Database?

In certain circumstances, when previewing a report in BIDS, the environment bypasses getting data from SQL Server and instead retrieves the data previously cached.

You can read more about caching at Microsoft:

http://msdn.microsoft.com/en-us/library/ms155927.aspx

If you don’t want to see the cached data, you can do 1 or 2 things:

1) Click the refresh icon on the toolbar when previewing your report.
2) Change the ‘CacheDataForPreview’ value to FALSE in the RSReportDesigner.config file located at C:Program Files (x86)Microsoft Visual Studio 9.0Common7IDEPrivateAssemblies. (SQL Server 2008)

Microsoft Visual Studio – ASP.Net Using LINQ to Filter Collections

You have a collection of objects. You want a subset of that collection. Instead of making another SQL call to the database or using programming code to create a filtered subset of your original collection, you can accomplish this simply by using LINQ.

The following scenario has already a GridView full of address data. I placed a radiobuttonlist control below the grid that allows a user to select ‘ALL’ or ‘FL’ (Florida). When the user clicks ‘FL’, I want to display in the GridView a subset of the original collection that contains addresses that pertain to Florida only.

The following event for the RadioButtonList demonstrates creating the filtered subset by using LINQ.

    
Protected Sub RadioButtonList1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
        mNameAddressCollection = ViewState("DataSource")
        If IsNothing(mNameAddressCollection) = True Then
            Exit Sub
        End If

        Try
            If RadioButtonList1.SelectedValue.ToUpper = "ALL" Then
                GridView1.DataSource = mNameAddressCollection
                GridView1.DataBind()
            Else
                Dim newCollection = (From m As clsNameAddress In mNameAddressCollection Where m.State.ToUpper = "FL" Select m)
                GridView1.DataSource = newCollection.Cast(Of clsNameAddress).ToList
            End If

            GridView1.DataBind()
            txtRecordCount.Text = GridView1.DataSource.count

        Catch ex As Exception
            mLogger.Error(ex.Message)
        End Try
End Sub

The actual line of LINQ code from the event above:

Dim newCollection = (From m As clsNameAddress In mNameAddressCollection Where m.State.ToUpper = “FL” Select m)

You then need to cast the new collection and assign it to the datasource of the GridView:

GridView1.DataSource = newCollection.Cast(Of clsNameAddress).ToList

Microsoft Visual Studio – ASP.Net How to Pass a Wildcard Parameter to a Table Adapter Query

If you are not using a stored procedure for you table adapter, then the answer is DON’T!!

I have done a lot of research and have found no perfect answer. Trying to pass ‘%’ to the fill method or embedding ‘%’ in your custom SELECT SQL statement of your table adapter with or without LTRIM’s and RTRIM’s just doesn’t work right.

The answer I found was to embed  the ‘%’ in a stored procedure instead. Here is how:

1) Create a stored procedure. Here is an example:

CREATE PROCEDURE GetCustomers
 @Customer VARCHAR(50)=null
AS
BEGIN
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers 
WHERE (CustomerID LIKE '%' + @Customer + '%')
END

Note that I embed the ‘%’ in the WHERE clause.

2) When building your table adapter, use your stored procedure to retrieve your data instead of customizing your own SQL statement. Here is an example:

Finally, in your code behind, you just pass the value you are filtering on and let the LIKE ‘%’ wild card in the stored procedure take care of the rest.

Dim custID as String = ‘EA’

customer.Fill(myDataSet.Customers, custId)

Your result set should be all customers that contain the value ‘EA’.

i.e. ‘OCEAN’, ‘BEANS’, ‘JEANS’.

 

Microsoft Visual Studio – ASP.Net Error in IE 9 When Using ReportViewer : No Such Interface Supported

While I was debugging in Visual Studio 2010, I come across this error when working with an aspx page that has a report viewer control. This only happens with IE. My report displays, but as soon as I click a parameter drop down that has multiple selections, the debugger throws the following error:

Now this exception in no way prevents my report from running. I am not even sure what the error is all about. If I publish my web site and run it from the domain, I would never know the error was being thrown.

A workaround I found was to place the following javascript into the <head> tags of the aspx page that contains the report viewer control:

<script language="javascript" type="text/javascript"> 
     if (window.getComputedStyle != null) 
        { 
          var orginalGetComputedStyle = window.getComputedStyle; 
          window.getComputedStyle = function (element, parm) 
             { 
               try 
                  { 
                     return orginalGetComputedStyle(element, null); 
                  } 
               catch (err) 
                  { 
                     return orginalGetComputedStyle(document.getElementsByTagName("body")[0], null); 
                  } 
             } 
         } 
<script>

SQL Server Reporting Services (SSRS) – Dealing with multiple date ranges with the option of choosing NULL for the start or end date in SSRS Reports

select * from date_range_table
where

((@startdate1 IS NULL AND @enddate1 IS NULL)
OR (start_date1 >= @startdate1 AND start_date1 <= @enddate1)
OR (start_date1 >= @startdate1 AND @enddate1 IS NULL)
OR (@startdate1 IS NULL AND start_date1 <= @enddate1))

AND

((@startdate2 IS NULL AND @enddate2 IS NULL)
OR (start_date2 >= @startdate2 AND start_date2 <= @enddate2)
OR (start_date2 >= @startdate2 AND @enddate2 IS NULL)
OR (@startdate2 IS NULL AND start_date2 <= @enddate2))

AND

((@startdate3 IS NULL AND @enddate3 IS NULL)
OR (start_date3 >= @startdate3 AND start_date3 <= @enddate3)
OR (start_date3 >= @startdate3 AND @enddate3 IS NULL)
OR (@startdate3 IS NULL AND start_date3 <= @enddate3))

OR IF YOU NEED TO CAST THE TIME DO THIS:

WHERE

((@startdate1 IS NULL AND @enddate1 IS NULL)
OR (start_date1 >= (@startdate1 + cast('00:00:00.000' as smalldatetime)) AND start_date1 <= (@enddate1 + cast('23:59:59.999' as smalldatetime)))
OR (start_date1 >= (@startdate1 + cast('00:00:00.000' as smalldatetime)) AND @enddate1 IS NULL)
OR (@startdate1 IS NULL AND start_date1 <= (@enddate1 + cast('23:59:59.999' as smalldatetime))))

AND

((@startdate2 IS NULL AND @enddate2 IS NULL)
OR (start_date2 >= (@startdate2 + cast('00:00:00.000' as smalldatetime)) AND start_date2 <= (@enddate2 + cast('23:59:59.999' as smalldatetime)))
OR (start_date2 >= (@startdate2 + cast('00:00:00.000' as smalldatetime)) AND @enddate2 IS NULL)
OR (@startdate2 IS NULL AND start_date2 <= (@enddate2 + cast('23:59:59.999' as smalldatetime))))

AND

((@startdate3 IS NULL AND @enddate3 IS NULL)
OR (start_date3 >= (@startdate3 + cast('00:00:00.000' as smalldatetime)) AND start_date3 <= (@enddate3 + cast('23:59:59.999' as smalldatetime)))
OR (start_date3 >= (@startdate3 + cast('00:00:00.000' as smalldatetime)) AND @enddate3 IS NULL)
OR (@startdate3 IS NULL AND start_date3 <= (@enddate3 + cast('23:59:59.999' as smalldatetime))))


I have found that a 'CASE WHEN' http://www.jamesandchey.net/?p=119 will work fine when working with one date range, but with multiple date ranges, the results can be unpredictable. The above logic proves to be a more effective way to get the results your are looking for.

This SQL says this:

If the @start date and @end date are NULL, get all rows. Basically ignore this where clause.

If your @start date is defined and @end date is defined, get all rows within the start and end date range

If your @start date is defined and @end date is NULL, the date range has no end, so get all rows after the range's start.

If your @start date is NULL and @end date is defined, the date range has no start, so get all rows before the range's end.