Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Friday, March 28, 2008

const and static declarations

const and static declarations cannot be declared together because they are always together when marked const.
Reference: http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88416.aspx

Thursday, March 27, 2008

Simulating a time spanned event with the sleep event

Here is how to simulate a time spanned event with the sleep event and a millisecond argument:

System.Threading.Thread.Sleep(2000);

Configure SQL Database for User Login

If you try and set up User Login without configuring the web.config file and try to login from a hosted site you might get an error similar to :" An attempt to attach an auto-named database for file C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\App_Data\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. "
To configure it for User Login, I first added the user database to the SQL server using this method.

Next, I changed the web.config file to include:

<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=my.host.net;Database=myDatabase;uid=myUserId;pwd=myPassword" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<authentication mode="Forms" />

<membership defaultProvider="AspNetSqlMembershipProvider">
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServersm" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" passwordFormat="Hashed" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="3" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" maxInvalidPasswordAttempts="3"/>
</providers>
</membership>

<roleManager enabled="true" />
Just change the myX fields to your fields. And now your configuration manager should work to add users and roles.

Monday, March 24, 2008

Running a client-side application using javascript in asp.net

Here is how to run a client-side application (.exe) using javascript in asp.net. I don't recommend it, because you probably have to lower your IE security (Tools -> Internet Options -> Security... and allow unsafe activeX).

<script language="javascript" type="text/javascript">
function btnSubmit_Click() {
var WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("calc.exe");
}
</script>
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="btnSubmit_Click();" />
</div>

reference: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_11275014.html

Thursday, March 20, 2008

Setting focus in a user control

Here is how to set focus in a user control. I used it in an update panel.

ScriptManager.GetCurrent(this.Page).SetFocus(txtTarget);

Saturday, March 15, 2008

Giving Stored Procedure Permissions

To give the required permissions in SQL Server Management Express SQL 2005 database, go to Security -> Schemas. Select the target schema. Right click for properties and Add the target user to the Execute Permission.

Tuesday, March 4, 2008

Prevent wrapping in table cell

Prevent wrapping in table cell. Since the old nowrap method is out of favor, the new method is to use:


<td style="white-space:nowrap"> foo </td>

reference: http://www.thescripts.com/forum/thread468471.html
Note: this also works with a Calendar Extender and TextBox even if the cell is too short.

Sunday, March 2, 2008

Previewing a TextBox that has HTML with a Popup Window

Here is a nifty way to preview HTML in a TextBox. I use the ScriptManager and $get the TextBox ID before popping the window and then filling it.

<script type="text/javascript" language="javascript" >
function preview() {
var txt= $get('txtContent');
var sHTML= txt.value;
win = window.open(", ", 'popup', 'toolbar = no, status = no');
win.document.write("" + sHTML + "");
}
</script>
Reference: http://javascript.internet.com/forms/html-preview.html

SQL Server Management Studio Express Review

SQL Server Management Studio Express is fine GUI for manipulating your database. Unfortunately, the problems I've been having are due to the complete lack of robustness. Try copying over 500 records to your table via the cut and paste Excel method and you should expect the program to freeze up and maybe have success. I would recommend using your own stored procedure (SPROC) for data uploading tasks. It is more cumbersome, but at least it works for large data handling.

Unable to cast object of type Foo to object of type Foo

I would get the error: Unable to cast object of type Foo to object of type Foo in my dynamic control. I read some problems about that kind of error and it said to make sure the Control was set to null before the cast. No error afterwards, at least so far.

Setting up form for HTML Email and Avoiding HttpRequestValidationException

In a previous post I showed you how to set up .NET for email. But what if I want to send it as HTML. I tried and received this error: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client ...

The validation problem is solved by setting the Page attribute Validation="false". But then when I sent the HTML it was received as text.

The solution is to use IsHTML=true In my example, I used a check box:

protected void btnSend_Click(object sender, EventArgs e)
{
string sTo = "jane@foo.com";
string sFrom = ddlFrom.SelectedItem.Text.Trim();
string sSubject = txtSubject.Text.Trim();
string sBody = txtContent.Text.Trim();
lblStatus.Text = "Sending... please wait";
MailMessage msg = new MailMessage(sFrom, sTo, sSubject, sBody);
msg.IsBodyHtml = chkIsHTML.Checked;
SmtpClient mailObj = new SmtpClient("localHost");
mailObj.Credentials = new NetworkCredential("username", "password");
mailObj.Send(msg);
clearTexts();
lblStatus.Text = "Success: Sent message to Jane at " + DateTime.Now.ToLongTimeString() + " Eastern.";

}



References: http://www.hintsandtips.com/ShowPost/131/hat.aspx
http://aspnet.4guysfromrolla.com/articles/080206-1.aspx