First Retouch into .Net Programming

Hi, it has been a while since my very last post under my Learning Bible title. Today, I spent my time to re-touch .Net programming. It has been two years not doing any web programming and I have this re-touch will bring me more new ideas. Ultimately, web programming is not my next main interest. It is just a warm-up session with a familiar platform.

Today I tried our the JQuery on setting a focus onto the textbox. Simple as a piece of cake for many programmers. I started with adding the below script into my master page,
<script type=”text/javascript” src=”/scripts/jquery-1.4.1.min.js”></script>. If you need guide, this website shows how to do it.
http://stackoverflow.com/questions/22664066/jquery-in-asp-net-web-form-with-a-master-page

I am sure there are newer versions of JQuery available. I found an article which is nicely explained which methods to be used to include jQuery into your project. http://www.codeproject.com/Tips/471799/jQuery-introduction-and-how-to-use-jQuery-with-ASP 

And, there is a demo available for setting focus on a textbox. http://jsfiddle.net/4fXLn/
This demo is based on onclick() function. If you’re doing page on load, then you can just remove the onclick() function.

<script type=”text/javascript” language=”javascript”>
$(document).ready(function() {
$(‘#MainContent_LoginUser_UserName’).focus();
});
</script>

Since I’m using master page, the id turned to be MainContent_LoginUser_UserName else you can just use the id from <input>.

And, you’re done!

[VS2005] Textbox Auto Expand

Here is an example of auto expand a textbox.

1. Control declaration:

<asp:TextBox ID=”txtMsg” runat=”server”  TextMode=”MultiLine” style=”overflow:hidden” onkeyup=”AutoExpand(this, event)” Rows=”2″ />

2. JavaScript function:

function AutoExpand(txtBox, event)
{
if (event.keyCode == “13” || event.keyCode == “8”) {
var therows = 0
var thetext = document.getElementById(txtBox.id).value;
var newtext = thetext.split(“\n”);
therows += newtext.length

document.getElementById(txtBox.id).rows = therows;
return false;
}
}

[VS2005] HEX to RGB

Changing the Hex value of a color to RGB format can be done using the following example.

if (HexColor.Replace(“#”, “”).Length == 6)
{
byte r, g, b;
HexColor = HexColor.Replace(“#”, “”);

r = Convert.ToByte(HexColor.Substring(0, 2), 16);
g = Convert.ToByte(HexColor.Substring(2, 2), 16);
b = Convert.ToByte(HexColor.Substring(4, 2), 16);

series.Color = Color.FromArgb(r, g, b);
}
else
throw new Exception(“Invalid HEX value”);

[VS2005] Image as Input Parameter

Using image as an input parameter, this is how the syntax should be:
public string strAddTemplate(string strGameCode, byte[] TemplateImage, string strImagemap, string strUserCode, string strClientIP)
{
SqlConnection con = null;
string strReturnCode = “”;

try
{
con = SqlHelper.CreateConnection(_connectionString);

SqlParameter prmGameCode = new SqlParameter(“@i_chvGameCode”, SqlDbType.VarChar, 10);
prmGameCode.Direction = ParameterDirection.Input;
prmGameCode.Value = strGameCode;

SqlParameter prmImage = new SqlParameter(“@i_chvImage”, SqlDbType.Image);
prmImage.Direction = ParameterDirection.Input;
prmImage.Value = TemplateImage;

SqlParameter prmImagemap = new SqlParameter(“@i_chvImagemap”, SqlDbType.VarChar);
prmImagemap.Direction = ParameterDirection.Input;
prmImagemap.Value = strImagemap;

SqlParameter prmUserCode = new SqlParameter(“@i_chvUsrCode”, SqlDbType.VarChar, 20);
prmUserCode.Direction = ParameterDirection.Input;
prmUserCode.Value = strUserCode;

SqlParameter prmClientIp = new SqlParameter(“@i_chvClientIp”, SqlDbType.VarChar, 15);
prmClientIp.Direction = ParameterDirection.Input;
prmClientIp.Value = strClientIP;

SqlParameter prmoResult = new SqlParameter(“@o_intResult”, SqlDbType.Int);
prmoResult.Direction = ParameterDirection.Output;

SqlParameter prmoRemark = new SqlParameter(“@o_chvResult”, SqlDbType.VarChar, 200);
prmoRemark.Direction = ParameterDirection.Output;

SqlHelper.ExecuteNonQuery(con,
CommandType.StoredProcedure,
“dbo.adm_sp_tb_template_add”,
prmGameCode,
prmImage,
prmImagemap,
prmUserCode,
prmClientIp,
prmoResult,
prmoRemark);

strReturnCode = (prmoResult.Value.ToString()) + “|” + (prmoRemark.Value.ToString());
}
catch (SqlException ex)
{
return ex.Message.ToString();
}
finally
{
SqlHelper.CloseConnection(con);
}
return strReturnCode;
}

[VB6] VB6 vs VB.net

VB6 vs VB.net
Data Type

VB6 VB.NET Comments
Integer Short 16 bits
Long Integer 32 bits
N/A Long 64 bits
Variant N/A Use the new ‘Object’ data type
Currency N/A Use Decimal in VB6 or Decimal or Long in VB.NET
N/A Decimal Available* in VB6. Native in VB.NET
String String VB.NET doesn’t support fixed length strings

– VB.net does not support Variant and Currency data type so use Object data type
– For currency, VB.net uses decimal data type and VB can also use the decimal data type. VB can’t declare a variable as decimal but can declare it as variant and then use cDec function to make its subtype decimal

Passing Parameters
– VB is using 2 methods: pass by reference and pass by value
– VB.net is using pass by value

Variable Declaration
– Always declare a variable in separate lines.
– Eg:
Dim lngOne As Long
Dim lngTwo As Long

If declares like this,
Dim lngOne, lngTwo As Long

declares lngOne as a Variant (which is not supported in .NET) and lngTwo as a Long.

New Commands and Keywords
– VB.NET introduces many new keywords

Array
– VB, array(5) gives index 0 to index 5
– VB.net, array(5) gives index 0 to index 4

Default Properties
– VB allows referencing default properties without specifying the name of property
– VB.net does not allow it

Let and Get Properties
– VB.Net no longer has Let properties

Data Access Method
– VB.net, all bound controls work with ADO only

Windows Forms
– VB.net is using windows forms replacing the VB6 Forms

More details, http://www.thescarms.com/vbasic/VB6vsVBNet.aspx

[VS2005] Language Selection In A Page

This is the way when a user click between English and Chinese language and how the page is being switched language.

It needs to have a resource file create. The English and Chinese texts for each label in a page will be created in 2 different files. Then, build the resource files and the ddl file is created. This file will be used in the website that I created. All, I have to do is to copy the latest ddl file into the project’s folder under the bin folder.

In VS 2005, check the bin folder add/remove the ddl file from the folder to ensure that the latest file is being called/used. To add, choose Add Reference to add the ddl file and browse the file. To remove, right click the reference and select remove.

Next, at the web config section, you have to add the following code:
<expressionBuilders>
<add expressionPrefix=”Translator” type=”gdLanguage.ResourceExpressionBuilder, gdLanguage”/>
</expressionBuilders>

Next, at the behind page of the master page, add the following code:

protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);

if (Session[“ssLanguageType”] == null)
{
Session[“ssLanguageType”] = “en-US”;
}

acafeLanguage.ResourceExpressionBuilder.sTranFile = GetCurrentPage();
acafeLanguage.ResourceExpressionBuilder.sCalture = (string)Session[“ssLanguageType”];

[VB6] Different Between Class and Module

As per title, I get this from the following source: http://en.allexperts.com/q/Visual-Basic-1048/class-module.htm

– A class requires you to create an instance of it before it’s use and module does not.
– Technically they are both classes.
– When you create a module, you are in essence creating a class. When your code runs, VB creates an instance of the module and makes it globally available. So, once your project is running, any public methods and/or variables in the module are available to your application.
– You can declare a variable as an instance of your class and then the public methods and variables of that class are available.

Examples:
Create a function that returns the date in a format custom for your application. Can be done in 2 methods:

1) Create a module and add a function.
Public Function GetSpecialDate() As String
GetSpecialDate = Format(Now, “yy-mm-dd”)
End Function

Call GetSpecialDate from anywhere in your application to get your date.
Private Sub Command1_Click()
MsgBox GetSpecialDate
End Sub

2) If you want to add this function into a class called cSpecialDate,
Private Sub Command1_Click()
Dim oSpecDate As cSpecialDate
Set oSpecDate = New cSpecialDate

MsgBox oSpecDate.GetSpecialDate
End Sub

where oSpecDate is an object for the cSpecialDate class and set the object.

A class allows you to create multiple instances of it. To show you how this may be used, let’s add property to our class that will affect the format of the date.

Option Explicit
Private msFormat As String

Private Sub Class_Initialize()
msFormat = “yy-mm-dd”
End Sub

Public Function GetSpecialDate() As String
GetSpecialDate = Format(Now, msFormat)
End Function

Public Property Let DateFormat(ByVal value As String) //write-only
msFormat = value
End Property

Private Sub Command1_Click()
Dim oSpecDate1 As cSpecialDate
Dim oSpecDate2 As cSpecialDate
Set oSpecDate1 = New cSpecialDate
Set oSpecDate2 = New cSpecialDate

MsgBox oSpecDate1.GetSpecialDate

//oSpecDate2.DateFormat = “yyyy-mmm-dd” //Don’t have DateFormat in VB6
MsgBox oSpecDate2.GetSpecialDate
End Sub

[VS2005] GridView with UpdatePanel

I’ve an update panel in which the gridview in place inside content template. I’ve a triggers for the button click and this button is placed outside the update panel.

I would like to know why the gridview is not reloaded/refreshed each time the button is clicked. The gridview is used to show the item details and status. So, i expect when a button is clicked, it’ll process behind code n the update panel will be triggered to reload new details and status.

The designer’s view,

<atk:ToolkitScriptManager runat=”Server” EnablePartialRendering=”true” ID=”tsmPage” />

<asp:UpdatePanel runat=”server” ID=”uppChild” UpdateMode=”Conditional”>
<ContentTemplate>
<b><asp:Label ID=”lblHeader” runat=”server” Text=”View Items Added Cart”></asp:Label></b><br />
<br />
<asp:GridView ID=”gvwItemAddCart” runat=”Server” AutoGenerateColumns=”false” ShowFooter=”false” HeaderStyle-Font-Names=”verdana”
Enableviewstate=”false” AllowPaging=”true” BorderStyle=”Solid” AllowSorting=”true” HeaderStyle-BackColor=”#F4901E”
DataKeyNames=”rdh_id, itm_id” PageSize=”15″ Width=”800″
OnPageIndexChanging=”gvwItemAddCart_PageIndexChanging” OnSorting=”gvwItemAddCart_Sorting”>
<Columns>
<asp:BoundField HeaderText=”Item Code” DataField=”itm_id” SortExpression=”itm_id” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
<asp:BoundField HeaderText=”Item Name” DataField=”itm_name” SortExpression=”itm_name” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
<asp:BoundField HeaderText=”Item Picture” DataField=”itm_imgsmall” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
<asp:BoundField HeaderText=”Quantity” DataField=”rdh_quantity” SortExpression=”rdh_quantity” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
<asp:BoundField HeaderText=”Unit Point” DataField=”rdh_unitpoint” SortExpression=”rdh_unitpoint” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
<asp:BoundField HeaderText=”Total Point” DataField=”rdh_totalpoint” SortExpression=”rdh_totalpoint” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
<asp:BoundField HeaderText=”Status” DataField=”rdh_status” SortExpression=”rdh_status” HeaderStyle-HorizontalAlign=”Left” ItemStyle-HorizontalAlign=”left” HeaderStyle-Width=”4%” />
</Columns>
</asp:GridView>

<asp:Label ID=”lblMessage” runat=”server” Text=”” ForeColor=”red”></asp:Label>

</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID=”btnConfirm” EventName=”Click” />
</Triggers>
</asp:UpdatePanel>
<br /><br />

<asp:Button ID=”btnConfirm” runat=”server” Text=”Confirm” OnClick=”btnConfirm_OnClick” /><br />

The above code is fine.

To be able to update the gridview after the button is clicked, I need to call a function that loads the data into the gridview. It’s needed to be called so that the gridview is rebind with new data.

Without the rebind, the gridview will show the old data even though the button is clicked.

[VS2005] Passing Variable Methods

There are few methods to pass a variable from one page to another page.

Using querystring:
From first page,
Eg: At back end code,
–> Response.Redirect(“location.aspx?search=”+search.Text); //value is taken from the textbox
–> Response.Redirect(“location.aspx?search=”+strSearch); //value is taken from a variable

At second page,
Eg: At back end code,
–> string searchLocation = Request.Querystring[“search”];

Using session:
From first page, add/create the session
Eg: At back end code,
–> session[“tempId”] = lblId.Text; //tempId is a variable name for session
–>session[“tempId”] = Request.Querystring[“id”]; //passing a request querystring to session

At second page, to use the session value

Eg: At back end code,
–> int Id = Convert.Int32(Session[“tempId”].ToString()); //to convert the session value into integer

Variable can be passed using the a href, hyperlink and linkbutton methods. The value also can be taken from the textbox, label or data from the database.
To clear session,
–> session.Clear(); // remove all keys & values from session
–> session.Abandon(); //cancel the current session