Asp.Net Required Field Validator for a DropDownList

A required field seems a bit odd for a dropdown list because it's got a value whether you like it or not. But when you feel that your dropdown list should have a required value it's usually because, you've given it an initial value of "--please select--" or somesuch and you want to validate that a 'real' value has been chosen.

Easy. Set the InitialValue property:

<asp:requiredfieldvalidator ControlToValidate="ddlMyPickList" 
    Text="*" InitialValue="-1"
    ErrorMessage="You must choose a value"
    runat="server" ID="rfvDdlMyPickList"/>

Specifically, set it to the Value (not the Text) of the 'please select' item you added.

Prevent an Asp.Net Button from triggering a Postback

Sometimes, you want an Asp.net Button to not trigger a postback.

One solution is, don't use an asp.net button, use an html input or link tag instead. However, you may want to use an asp:XXXButton for consistency with the rest of your page; or it may seem a simple way to make the text on the button localisable (although you can equally achieve that on a plain old html control if you give it an id, a runat="server" and probably a meta:resourcekey); or you may have other stuff you want to do with it serverside.

To stop an asp.net button causing a postback, do it like this:

<asp:LinkButton id="btnX" OnClientClick="return false;" runat="server" />

The more likely scenarios is, that you want to run clientside javascript. In that case, put ";return false;" after your javascript call:

<asp:LinkButton id="btnX" OnClientClick="functionToCall();return false;" runat="server"/>

Write to a text file from MS SQL Server

There are several things you need to know to be able to do this.

  1. How to enable ad hoc queries.
    This guy enable-Ad-Hoc-Distributed-Queries shows you how.
  2. How to format the driver string and specify your directory and filename. Like this:
    OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=c:\MyDirectoryName;','select * from MyTextFile.txt')
  3. How to create a text file in the right format. This format works:
    "ColumnName1","ColumnName2"
    "value11", "value21"
    "value21", "value22"

There. That was only slightly unreasonably difficult. Now you can

Select * from OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir=c:\MyDirectoryName;','select * from MyTextFile.txt')

and

Insert into OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
DefaultDir=c:\MyDirectoryName;','select * from MyTextFile.txt')
Values ('Col1', 'Col2')