Archive for the 'Coding' Category

Center datagrid pager style in IE8


<PagerStyle HorizontalAlign="Center" />

stopped working in IE8. Updated the css to the following and everything centered fine.


.dgrGridpager table
{
margin: 0 auto 2px auto;
}

sp_configure and Ole Automation Procedures

I needed to allow access to file create and delete to a specific user in sql server 2008. This advanced option is now disabled by default.

To enable:

sp_configure ‘show advanced options’, 1;
GO
RECONFIGURE;
GO
sp_configure ‘Ole Automation Procedures’, 1;
GO
RECONFIGURE;
GO

Hide horizontal scrollbar in tablelayoutpanel

When populating a tablelayoutpanel dynamically, the horizontal scrollbar kept appearing, even though there was no need for it.

It looks like there is no property to disable only the horizontal scrollbar, but I did find this workaround.

Pad the layoutpanel to the width of the horizontal scroll bar and the scroll bar will disappear.


int vertScrollWidth = SystemInformation.VerticalScrollBarWidth;

tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0);

MySql with .Net SqlDataSource on hosted server

I was getting an error saying the .Net provider could not be found when attempting to connect to a MySql db in hosted environment. If the MySql.Data.MySqlClient is not loaded in the GAC on the hosted server, you will need to add a reference in your web.config, if you want to use the SqlDataSource control.

  1.  
  2. web.config - make sure the version matches your MySqlClient version
  3.  
  4. <system.data>
  5. <DbProviderFactories>
  6. <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=5.2.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  7. </DbProviderFactories>
  8. </system.data>
  9.  
  10. SqlDataSource reference
  11.  
  12. <asp:SqlDataSource ProviderName="MySql.Data.MySqlClient" ID="dsData" runat="server" ></asp:SqlDataSource>
  13.  

Change column order in MySql

Too bad the MySql GUI can’t handle this, but it is what it is:

ALTER TABLE `clientpackage` MODIFY COLUMN `ParentPackageId` mediumint AFTER `PackageId`

MySql import with crlf

If your text file lines end with carriage return / line feed, you will need \r in addition to \n to have clean data import:


LOAD DATA LOCAL INFILE 'c:\\www\\package.txt'
INTO TABLE businesscategory
LINES TERMINATED BY '\r\n'
(businesscategory);

YYYYMMDD for derived column in SSIS

I think SSIS is great, but date formatting still remains a bit tricky. Would be nice to have a straight format function, but this is what I needed to use in the expression portion of the Derived Column Transformation Editor in order to get the date format YearMonthDay :

(DT_STR,4,1252) DatePart("yyyy",Trans_Dttm) +
Right("0" + (DT_STR,4,1252) DatePart("m",Trans_Dttm),2) +
Right("0" + (DT_STR,4,1252) DatePart("d",Trans_Dttm),2)

Delete WINDOWS when emptying Recycle Bin

Needless to say, this isn’t a very comforting message to get when you try to clear the Recycle Bin. What made it even weirder was there was nothing in the Recycle Bin.

To fix:

Start / Run / %systemdrive%\Recycler – this will give you the weird folder names for Recycle Bin – Something like : S-1-5-21-507921405-527237240-1801674531-1029

I had two folders under c:\recycler – went to CMD – cd \Recycler – then deleted all files / folders in Recycler and then deleted the c:\Recycler directory and rebooted.

That fixed the issue.

Reference ClickOnce publish version

If you are trying to keep your About window in sync with the published version in ClickOnce, rather than the assembly version, you can reference My.Application.Deployment.CurrentVersion. However, this only works on the installed application, not in Visual Studio.


If System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed Then
Me.LabelVersion.Text = String.Format("Version {0}", My.Application.Deployment.CurrentVersion)
Else
Me.LabelVersion.Text = String.Format("Version {0}", My.Application.Info.Version.ToString)
End If

How to move ToolStripMenuItems

Pretty basic stuff, but it can drive you crazy if you don’t remember how.

Move menu items

IE8 and HTTP 403.9 – Access Forbidden: Too many users are connected

I upgraded to IE8 and started getting 403.9 on my local machine during testing on XP with IIS 5.1.

Turns out, you now have to disable HTTP Keep-Alives in the IIS Admin properties for the local site.

Here is more info: Disable Keep-Alive

Sort javascript array by content length

I needed to sort an array by the length of the string values in descending order. JS has a lot of nice features and building your own sort order logic is one of them. To get the list in ascending order, just switch the comparison operator.

function sortByLengthDesc(a, b) {
if (a.length > b.length)
return -1;
if (a.length < b.length) return 1; return 0; } arrTmp = tmpText.split('<br>'); arrTmp.sort(sortByLengthDesc);

Clear a databound datagridview

If you have bound a datagridview to a datasource, you need to clear the datasource before attempting to remove all the rows.


dgvFunds.DataSource = Nothing
dgvFunds.Rows.Clear()

Add a destination to the Send To menu

To add a destination to the Send To menu, you must add a shortcut to the SendTo folder. To do this, follow these steps:

1. Click Start, and then click Run.
2. In the Open box, type sendto, and then click OK.
3. Add a destination by doing one of the following:
* Use the drag-and-drop operation to move the item that you want to the SendTo folder; to do so, right-click, and then click Create Shortcuts Here.
-or-
* Point to New on the File menu, and then click Shortcut.

Follow the instructions in the Create Shortcut Wizard.
A new shortcut is created in the SendTo folder, and it is displayed on the Send To menu.

MS article

Check for null date value

I have a class with a property of type date. Since this is a value type, it can’t be set to null.

If the date value is not set, it is initialized with the smallest date possible: #12:00:00 AM#, which will always return false if you use isNothing.

Instead, compare the date to DateTime.MinValue


If clsDate.dateEquals = DateTime.MinValue Then

SSMS 2008 stops showing 2005 dbs

I started getting this error out of the blue when trying to access a hosted 2005 db through SSMS 2008.

The server principal “db1” is not able to access the database “db2” under the current security context. (Microsoft SQL Server, Error: 916)

No databases would display under the databases tab and every refresh would result in the same error. I could see the dbs in SSMS 2005.

The fix is pretty weird. Click on the Databases folder, then View / Object Explorer Details. Then right click in the column headings and un-select Collation. Refresh the folder and the dbs should be there.

force __doPostBack generation

Sometimes you want the __doPostBack function to be forced on a aspx page.

Add this line in your Page_Load event:

Page.ClientScript.GetPostBackClientHyperlink(New Control(), String.Empty)

onMouseOut for nested DIV

This was making me crazy for a bit. The onMouseOut event fires for the parent DIV when moving over the child DIV elements.

I had several custom menus nested in divs and I only wanted to hide the entire parent DIV block when the mouse left the parent.

Came across the handy script below which works like a charm.

js for mouse out

Fixed header for repeater control

Here is a simple solution to get an excel like fixed header at the top of a repeater control.

Fixed headers

e.item.DataItem causes invalid cast exception

I kept getting an exception when attempting to cast e.Item.DataItem to DataRowView in a repeater control ItemDataBound event.

Issue was I was binding a DataReader, which requires DBDataRecord in System.Data.Common. Dataset uses DataRowView.

Microsoft KB

If the data source is a DataReader object, you must cast e.Item.DataItem as type DBDataRecord (from System.Data.Common) in the event handler. If the data source is a DataTable, you must cast e.Item.DataItem as type DataRowView.

« Previous PageNext Page »