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.

Tuesday, December 23, 2008

Get the Current Time for Database Storage

To get the current datetime in UTC format in .net use following:

DateTime dateUTC = DateTime.Now.ToUniversalTime();
You can then store this date in your database.
If you want to display it back to local time use:


DateTime dateLocal = dateUTC.ToLocalTime();


Reference: http://forums.asp.net/t/1217716.aspx

The Difference Between NVARCHAR and VARCHAR

The main difference between NVARCHAR and VARCHAR is that NVARCHAR extra space allows for easier multi-language support. Therefore, I switched my database's VARCHARs to NVARCHARS.

References: http://july-code.blogspot.com/2008/03/differences-between-varchar-and.html

Accessing ConnectionString from CodeBehind

Accessing a ConnectionString from CodeBehind requires your connection string to be defined in the web.config file.

Then use this source code:

ConnectionString = System.Configuration.ConfigurationManager.AppSettings["MySetting"].ToString();

I customized my BasePage with a utility function as follows:

public string getConnectionString(string vConnectionString)
{
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
System.Configuration.ConnectionStringSettings connString = null;
string connectionString = string.Empty;
if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
{
connString =
rootWebConfig.ConnectionStrings.ConnectionStrings[vConnectionString];
if (connString != null)
connectionString = connString.ConnectionString;
}
return connectionString;
}

References: http://forums.asp.net/p/1353594/2770309.aspx
http://msdn.microsoft.com/en-us/library/ms178411(VS.80).aspx

Use ConnectionString Web.config

I setup the ConnectionString in my Web.config file and encountered a "'ConnectionStringSettings' is not defined error. The solution was to add the 'system.configuration' library to the project! (Project, Add reference.., then find the library then add it!).

References: http://forums.asp.net/p/1047730/1475561.aspx#1475561

Keeping a DropDownList's DataSource Updated

The DropDownList does not manage selected state like other controls. Therefore, I use a Session variable to store an index to keep track of the currently selected item:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["listId"] = ddlList.SelectedValue;
GridViewStocksInLists.DataBind();
}
}

Now, I add the SelectedIndexChanged event with the following code:

protected void ddlList_SelectedIndexChanged(object sender, EventArgs e)
{
Session["listId"] = ddlList.SelectedValue;
GridViewStocksInLists.DataBind();
}

Reference: http://www.velocityreviews.com/forums/t123449-problem-with-formview-and-dropdownlists.html

Use a CustomControl to Set UserId and Use as a DataBindable Property

Use a CustomControl (named MembershipUser) to Set UserId and Use as a DataBindable Property. Create a MembershipUser CustomControl that has a property named Value that can be DataBound. For example MembershipUser1.Value would fetch the userId:


[Browsable (true)]
public string Value
{
get
{
MembershipUser currentUser;
currentUser = Membership.GetUser();
if (currentUser == null)
return string.Empty;
return currentUser.ProviderUserKey.ToString();
}
}
Reference: This is from http://aspnet.4guysfromrolla.com/articles/110106-1.aspx

Be Careful About uniqueId Parameter in Stored Procedure

Here is an error I received when my UniqueId parameter in a stored procedure was not valid:



Server Error in '/stockmonger.com' Application.
Conversion failed when converting from a character string to uniqueidentifier.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting from a character string to uniqueidentifier.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


I corrected to make sure the parameter was a valid uniqueId paramater and not null.