Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Mar 19, 2010

Simple Method Invoker Thread Safe Code

                string ip = string.Empty;
                MethodInvoker method = delegate
                    ip = txtOutgoingIP.Text;

                if (InvokeRequired)

Bookmark and Share

Mar 14, 2009

Error the calling thred must be STA, because many UI components require this


Thread thread = new Thread(new ThreadStart(CreateUIInAnotherThread));

private void CreateUIInAnotherThread()
Window wd = new Window();

Feb 21, 2008

Validate Input Values With Message List

Private Function ValidateInputValues(ByVal control As Control, ByVal pageType As PageType) As ArrayList
Dim messageList As ArrayList = New ArrayList()
If (pageType <> PageType.Search) Then ' Add/Edit Validation
Dim fullName As TextBox = TryCast(control.FindControl("fullName"), TextBox)
If (fullName IsNot Nothing) Then
If (Not DataFormat.CheckString(fullName.Text)) Then
messageList.Add("Invalid Value (String): fullName")
End If
End If
Dim phoneNumber As TextBox = TryCast(control.FindControl("phoneNumber"), TextBox)
If (phoneNumber IsNot Nothing) Then
If (Not DataFormat.CheckInt32(phoneNumber.Text)) Then
messageList.Add("Invalid Value (Int32): Phone Number")
End If
End If
End If
Return IIf(messageList.Count = 0, Nothing, messageList)
End Function

' Powerful TIPs !
Dim source As WebControl = TryCast(sender, WebControl)

Page Load Function Tips

Get some idea or tips from others work in their page load():

Protected Sub Page_Load(ByVal s As Object, ByVal e As System.EventArgs)

' Registering external javascript file, so that we can have more neater js file management.
Page.ClientScript.RegisterClientScriptInclude("ethan", "ethan.js")

' Cache Setting, I just know about
' ASP Caching can be set manually on the virtual directory property

' Auto-find control and set focus to it;
' I just know got such a syntax, which really near to human language syntax "IsNot"
' and about ClientID property which is very useful in some case.
If (tblpwdDetailsView.FindControl("fullName") IsNot Nothing) Then
Page.Form.DefaultFocus = tblpwdDetailsView.FindControl("fullName").ClientID
End If

' The way they check for QueryString exist or not
If (Not Page.IsPostBack) Then
If (Request.QueryString.Count > 0) Then

Jan 2, 2008

Component cannot be created and AspCompact need to be TRUE

Usually in a vb6 to ASP.NET migration process, you will get this error.
Error: The component 'myCOM.COMClass' cannot be created. Apartment threaded components can only be created on pages with an <%@ Page aspcompat=true %> page directive.System.Web

The error above noted that we need to add page directive aspcompat = true because you are using COM interop with ASP.NET pages.

Just simply add the page directive "aspcompat=true" and it solved! :)

Exception : thread was being aborted / mscorlib

I got this message while debugging in 2.0 at this

I was thinking what's wrong with this line and you will get this exception if you throw the exception out to the display only.

Surfing around and I got the answer.
We have to understand how response.write or server.transfer work!

In ASP.NET, response.write and server.transfer will end the threading with response.end.
Thus, if we have a TRY...CATCH...END TRY within it, the TRY Thread will be aborted and ended suddenly and the exception will be caught.

We should make the response.redirect before or outside the catch at this point.

Usually, we will have a checking for session lost at the beginning of the page.

If String.IsNullOrEmpty(Session("user_id")) Then
End if

This simple validation portion might not need to be inside Try Catch loop and you will be safe!

In addition, sometimes programming like to declare variable inside Try Catch loop too, and it is NOT necessary to do so.

A simple statement likes
Dim myString as string
is NOT necessary to be in Try Catch loop. :)

Nov 8, 2007

Xceed Datagrid For WPF In XBAP

I did a XBAP Demo in my company sharing session, and wish to see more interactive and creative design with Microsoft WPF , .net 3.0 Framework.

Wooow, just surf around for free datagrid in WPF, I got this Xceed.

Xceed Component Demo

Doesn't it look pro ?

I am not Xceed stuff, anyway feel amazed with the little visual effects and design in this rich media web application.

Take a look! Wish to explore it more and hope I can apply my macromedia flash skill into this WPF too!

Simple JavaScript API For Blog Editor

Blog This - simple editor using the Blogger JavaScript API

You have to get your Google API Key first!

Try it!

Nov 1, 2007

ASP Get Browser URL

There are times in your scripts when you are going to want to get the current page URL that is shown the browser URL window. For example maybe a page URL has Querystring info appended to it and you need to send an email off to someone with that same exact URL and Querystring information. There are plenty of other reasons as well.

Here is some code to do it.

Lets say the current page is simply "http://www.mysite,com/view_user.asp"

This is all you need to get you the current page URL
Thispage ="http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")

Now, if your page has Querystring info and variables you want as well.
Like so "http://www.mysite,com/view_user.asp?ID=1&Name=Fred"

you would use code like this.

Thispage ="http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring

If your page had Form info that might have been posted to it you would use code like this.

Thispage = "http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Form

If your page had both Querystring and Form info you could try code like this.

Thispage = "http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") & "?" & Request.Querystring & Request.Form

Update DB Data With Leading Zero Format

SET PIN = '0' + PIN
WHERE ({ fn LENGTH(PIN) } = 7)

Oct 30, 2007

Get Physical File Path, Virtual Directory

oFSO = Server.CreateObject("Scripting.FileSystemObject")
sPath = Server.MapPath("Moozik\")
fRoot = oFSO.GetFolder(sPath)

(web paths, not full physical)

My file: /Test/Myfile.aspx
Virtual directory: /Test/VirtualDir/
Code: Server.MapPath("VirtualDir/")

or if you are in a higher directory:

MyFile: /Test/Something/MyFile.aspx
Virutal: /VirtualDir/
Code: Server.MapPath("../../VirtualDir/")

You are probably just getting the virtual path (the one that gets sent into
MapPath) wrong.

Win App Local Application Path

LocalReport.ReportPath = _

My.Application.Info.DirectoryPath & "\Reports\" & "myReport.rdlc"

Where myReport.rdlc is located under Reports folder in my win app program.

Oct 29, 2007

Using Microsoft Report Viewer

Create a Microsoft report:

1. add in a list.
2. add in table
3. drag data from data source to table

Dim voucher As New Voucher
DataGridView1.DataSource = voucher.FillVoucherHeader

DataGridView2.DataSource = voucher.FillVoucherItems

DataGridView3.DataSource = voucher.FillVoucherTotal



Using VBC Command Line Compiler

Do you ever compile with command line ?

Just recall back Java Learning during university lab.

Let's try:
  1. From the Start menu, click on the Accessories folder, and then open the Windows Command Prompt.

  2. At the command line, type vbc.exe /imports:Microsoft.VisualBasic,System sourceFileName and then press ENTER.

    For example, if you stored your source code in a directory called SourceFiles, you would open the Command Prompt and type cd SourceFiles to change to that directory. If the directory contained a source file named Source.vb, you could compile it by typing vbc.exe /imports:Microsoft.VisualBasic,System Source.vb.

  3. To compile with reference file:
    vbc /reference:metad1.dll,metad2.dll /out:out.exe input.

Oct 18, 2007

Runtime Web.config / App.config Editing

Web.config configuration files and app.config project item files, which get converted to "ExecutableName.exe.config" at build time, both support the convenient appSettings section with its own read method in the System.Configuration.ConfigurationSettngs class. The appSettings section stores element name / value pairs in the format:

You can store as many of these elements as you want, read them out at runtime, and use the values in the application. If you have an item that contains multiple values and you would like to keep them together, you can store them as a single string, delimited with a pipe | or other symbol, read them out at runtime, and call the String.Split() method to parse them into a useable string array.

I often read out my appSetting values into a NameValueCollection at runtime, which provides one-shot acess to the entire collection in memory:

NameValueCollection mySettings = System.Configuration.ConfigurationSettings.AppSettings;
string connStr = mySettings["connString"];

But what about being able to change, add, and save appSettings items while that app is running in response to user input or other actions, instead of just reading them out? Nada, Zippo, Efes! You have to open the config file manually and add them by "hand". Well that kinda stinks, don't you think? So here's my take on a convenient little class that allows you to either modify, add or delete any appSettings element, in either your Executable, Console or ASP.NET web application at runtime, on the fly. Bear in mind of course, that if you modify a web.config on a running ASP.NET app, the ASP.NET worker process will recycle. Users currently using your app aren't exactly guaranteed to have a fun experience when this happens...

Original Source:
By Peter A. Bromberg, Ph.D.