Friday, January 11, 2008

C# @ string meaning.

And now for a little string manipulation:

string initXML = @"<?xml version=""1.0"" ?><request company=""gnc"" auth=""{0}"" unit=""{1}"" code=""{2}"" />";

initXML = String.Format(initXML, authCode, location, passCode);

The difference between a regular and verbatim string expression:
























Regular literal

Verbatim literal

Result

"Unplug1dot6"

@"Unplug1dot6"

Unplug1dot6

"Backslash: \\"

@"Backslash: \"

Backslash: \

"Quote: \""

@"Quote: """

Quote: "

"CRLF:\r\nPost CRLF"

@"CRLF:
Post CRLF"

CRLF:
Post CRLF

Monday, January 7, 2008

Use BCP within ASP.NET, C#

The Bulk Copy Program (BCP) is a command-line utility that ships with SQL Server. You can import / export large amounts of data in / out of SQL.

The BCP utility is accessed from the command prompt. Syntax below:
bcp {dbtable | query} {in | out | queryout | format} datafile
[-n native type] [-c character type]
[-S server name] [-U username]
[-P password] [-T trusted connection]

Remember: When using BCP, the terms are case-sensitive.
-n term specifies native SQL Server format.
-S term enables you to add the server/instance name.
-U term allows you to add the name of the login used to connect to SQL Server.
-P term lets you add the password of the -U switch.
-T term is for establishing a trusted connection to your SQL Server
-c term performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator
-t field_term specifies the field terminator. The default is \t (tab character). Use this parameter to override the default field terminator.
-r row_term specifies the row terminator. The default is \n (newline character). Use this parameter to override the default row terminator.

Example - C# in .cs file
Process bcp = new Process();
bcp.StartInfo.WorkingDirectory = WorkPath;
bcp.StartInfo.FileName = "bcp";
bcp.StartInfo.Arguments = "datatableName..storedprocedureToRun"
  + " in " //out
  + file.Name
  + @" -c -t ~ -r \n -DATABASE_NAME -Ucredential -Pcredential";
bcp.Start();
bcp.WaitForExit();

Wednesday, January 2, 2008

Verify a condition is true before posting asp:checkbox

If you want to verify a condition before the AutoPostBack event is fired for an asp:checkbox.

Set up the javascript function of what you want to verify:
<script language="javascript">
  function Reviewed_Click(o, e)
  {
    var oResponse = document.getElementById("Response");
    if (oResponse != null)
    {
      var reg = /\s+/g;
      var value = oResponse.innerText.toLowerCase().replace(reg, "");
      if (value == "yes"
      && !confirm("A question to ask?\nIf \"Yes\" click OK, otherwise click CANCEL."))
      {
        event.returnValue = false;
        return false;
      }
    }
  return true;
}
</script>

--------------------------------------------------------------
Now add the asp:checkbox...

<asp:checkbox id="Reviewed" Runat="server" AutoPostBack="True" Text="Information Reviewed" onclick="if (!Reviewed_Click(this, event)) return false;"></asp:checkbox>