Thursday, January 9, 2014

ASP.NET 4 WEB FORM EXAMPLE TO CONNECT TO MDB DATABASE


STEPS

1) Create New ASP.Net Web Site in the path c:\projects\Campaign.
2) Download a sample mdb file, e.g http://www.arialsoftware.com/uploads/9/1/8/3/9183466/campaign_template1.zip ,unzip and save the file to c:\projects\Campaign\App_Data.
3) Add New Web Form, Default.aspx
4) Edit as follows:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
dim dbconn,sql,dbcomm,dbread
        dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("App_Data/Campaign_Template.mdb"))
        dbconn.Open()
        sql = "SELECT * FROM Campaign_Table"
        dbcomm = New OleDbCommand(sql, dbconn)
        dbread = dbcomm.ExecuteReader()
        customers.DataSource = dbread
        customers.DataBind()
        dbread.Close()
        dbconn.Close()
  End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:Repeater id="customers" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>ID</th>
<th>Last_Name</th>
<th>First_Name</th>
<th>Email_Address</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("ID")%></td>
<td><%#Container.DataItem("Last_Name")%></td>
<td><%#Container.DataItem("First_Name")%></td>
<td><%#Container.DataItem("Email_Address")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
5) Run.

Sunday, January 5, 2014

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 6 (Variables and Functions)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 6 (Variables and Functions)

PRE-REQUISITE

Or

STEPS

1) Open Web Site vbsite201
2) Edit Default.aspx
Add a property OnClick with value “Click”
Add a label Object Label5
Add a script for “Click”
3) Run.
1. Enter…
Name: John
DOB: 1/1/2000
Age Now: 14
2. Click Submit.
3. There is a possibility that the user calculates his age incorrectly. We need to assist the user to do this.
4) Modify the script.
1. Add a Function FindAge
2. Add function call to FindAge from within Sub Click
Take note that this server-side script may not be the most efficient way to compute user age.
This example is just to illustrate the use of variables and functions in ASP.NET Web Site.
5) Run.

C# EXERCISE

string FindAge(string strDate)
{
    DateTime dob = Convert.ToDateTime(strDate)
    textBox1.Text = dob.ToString();
    TimeSpan tm = (DateTime.Now - dob);
    int age = (tm.Days/365) ;  
    return age.ToString();
}
void Click(Object s, EventArgs e)
{
    Label5.Text = "You have entered...<br/>"
    Label5.Text += "Name: " + TextBox1.Text + "<br/>"
    Label5.Text += "DOB: " + TextBox2.Text + "<br/>"
    Label5.Text += "Age Now: " + FindAge(TextBox2.Text) + "<br/>"
}

FURTHER EXERCISE

How to add values e.g. “BMW”, “Toyota”, “Honda” to DropDown object during Page_Load event?
Clue: DropDownList.Items.Add(…)

DISCUSSION

Variables.
Variable Data Types.
Data Type Conversion.
Function.
Function Call.

DOWNLOAD

---

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 5 (Form Objects)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 5 (Form Objects)

STEPS

1) Create New Web Site.
2) New Web Site options.
1. Template Language: Visual Basic.
2. .Net Framework: 4
3. Template Types: ASP.NET Empty Web Site
4. File Location: File System
5. Path: C:\projects\vs12vb\sitevb201
2) Add a Web Form.
1. Language: Visual Basic.
2. Type: Web Form
3. Name: Default.aspx
4. Place code in separate file (No), Select Master page (No)
3) Edit Default.aspx
Open Default.aspx.
Switch to Source mode.
Add <p> elements for 6 times.
4) Add form objects.
Switch to Design mode.
Drag and drop Label, TextBox, DropDown and Button objects as follows:
Switch to Source mode.
Your form codes should look like below:
5) Modify the form texts to make them more meaningful.
6) Run.
7) Add JavaScript for Reset Button.
We can add a client-side scripting such as JavaScript to reset form values.
Modify the Button2 properties as follows:
8) Run.
Perform the following steps:
1. Enter…
Name: John
DOB: 1/1/2000
Age Now: 14
2. Click Reset.
3. Re-enter the same data.
4. Click Submit. Observe the “View State” effect.
5. Click Reset.The values do not reset to blank. Instead, they remain as entered earlier.
6. Click Refresh button (in Web Browser’s tool bar). Observe the “View State” effect.
7. Click the URL in the Address Box and press Enter. The TextBox objects now reset to blank values.
---

Saturday, January 4, 2014

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 4 (More Page Events)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 4 (More Page Events)

PRE-REQUISITE

Or

STEPS

1) Open the Web Site vbsite101.
Open Default.aspx.
2) Edit the <script> by replacing the existing codes for Page_Load with the following codes:
    Sub Page_Init(ByVal s As Object, ByVal e As EventArgs)
        myTimeLabel.Text = "<br/>1. Page_Init: " + DateTime.Now.ToString() + "<br/>"
    End Sub
    Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
        myTimeLabel.Text += "2. Page_Load :" + DateTime.Now.ToString() + "<br/>"
    End Sub
    Sub Page_PreRender(ByVal s As Object, ByVal e As EventArgs)
        myTimeLabel.Text += "3. Page_PreRender :" + DateTime.Now.ToString() + "<br/>"
    End Sub
    Sub Page_UnLoad(ByVal s As Object, ByVal e As EventArgs)
        myTimeLabel.Text += "4. Page_UnLoad :" + DateTime.Now.ToString() + "<br/>"
    End Sub
3) Run.
4) Type Hello and click the button.

C# EXERCISE:

void Page_Init(Object s, EventArgs e)
{
   myTimeLabel.Text = "<br/>1. Page_Init:" + DateTime.Now.ToString() +  "<br/>";
}
void Page_Load(Object s, EventArgs e)
{
   myTimeLabel.Text += "2. Page_Load:" + DateTime.Now.ToString() +  "<br/>";
}
   void Page_PreRender(Object s, EventArgs e)
{
   myTimeLabel.Text += "3. Page_PreRender:" + DateTime.Now.ToString() +  "<br/>";
}
void Page_UnLoad(Object s, EventArgs e)
{
   myTimeLabel.Text += "4. Page_UnLoad:" + DateTime.Now.ToString() +  "<br/>";
}
---

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 3 (View State)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 3 (View State)

PRE-REQUISITE

Or
Download startup file:

STEPS

1) Open the Web Site vbsite101.
Open Default.aspx.
2) In the Design Mode, create a new <p> element below the object myTimeLabel (click an area beside the object [myTimeLabel] and press ENTER key)
3) Add control objects; TextBox, Button and Label.
Click the object icon in the Toolbox, drag and drop them into the <p> element.
Design outcome:
Code outcome:
4) Add OnClick property to the Button.
OnClick=”Click”
Code outcome:
4) Add action codes that will respond to Click event.
Sub Click(ByVal s As Object, ByVal e As EventArgs)
    Label1.Text = TextBox1.Text
End Sub
Code outcome:
5) Run.
1. Type “Hello” into the Text Box.
2. Click the Button
3. The text “Hello” appears in the Label.

C# EXERCISE

Use the action codes below for the Click event (for C# language):
void Click(Object s, EventArgs e)
{
   Label1.Text = TextBox1.Text;
}

DISCUSSION

ASP.NET controls automatically retain their data when a page is sent to the server in response to an event (such as a user clicking a button). Microsoft calls this persistence
of data view state.
ASP.NET pages maintain view state by encrypting the data within a hidden form field.
This takes up additional resources. If you don’t intend to use view state, it is recommended that you disable it.

DOWNLOAD

---

ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 2 (Page_Load event)


---
ASP NET 4 BASICS USING VISUAL STUDIO 2012 Part 2 (Page_Load event)

PRE-REQUISITE

Or
Download startup file:

STEPS

1) Open the Web Site vbsite101.
Open Default.aspx.
2) Turn on the line number display (follow the steps here, http://visualstudio-steps.blogspot.com/2014/01/how-to-show-line-numbers-in-code-editor.html)
Refer the screenshot below. We will be editing the following elements:
<Script> (line no. 5)
<title> (line no.11)
<div> (line no.15)
3) We start with the easiest part, i.e. <title>:
<title>Welcome</title>
4) Next, edit the <div>:
    <div>
      <p>Hello there!</p>
      <p>
        The time is now:
        <%-- Display the current date and time --%>
      </p>
      <p>
        <%-- Declare the title as string and set it --%>
        <% Dim Info As String = "This is generated by a code render block."%>
        <%= Info %>
      </p>    
    </div>
5) Run.
6) We are done with a simple page displaying a value stored in a string variable. We will add action to respond to an event, i.e. page load. Close Internet Explorer first.
7) Switch to Design Mode.
Click the Toolbox sidebar.
Click the pin icon so that Toolbox is visible all the time.
8) Add a Label object to the form.
Click the Label icon in the Standard Group of the Toolbox and drag the icon to a location besides “The time is now:”.
9) Set the Label object properties.
In the Properties window, set…
ID: myTimeLabel
Text: <blank>
10) Switch to Source mode.
We have successfully added an object using GUI approach (we can also type the codes manually).
If you run the codes now, you won’t see the Label object as it doesn’t contain anything.
We will add an action that will put some texts into the label when the page load event takes place.
11) Add codes to the <script> element.
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    'set the label text to the current time
    myTimeLabel.Text = DateTime.Now.ToString()
  End Sub
12) Run.

C# EXERCISE:

Repeat the above steps to create the web site using C# language.
At Step 11, use the codes below (for C# language):
  protected void Page_Load(object sender, EventArgs e)
  {
    //set the label text to the current time
    myTimeLabel.Text = DateTime.Now.ToString();
  }

DISCUSSION

An ASP.NET page consists of the following elements:
 directives
 code declaration blocks
 code render blocks
 ASP.NET server controls
 server-side comments
 literal text and HTML tags
DOWNLOAD
---