When installing ASP.Net, the Framework, or for that matter, any new app, here are some things you need to check first, just to make sure you get a good installation:
1. Temporarily disable virus protection
2. Remove All Temp Files
3. Make sure no other app is running
4. Run chkdsk/scandisk/Norton’s WinDoctor, or some other fix-it app against your HD to make sure it’s in the best shape it can be.
5. THEN – try installing
Monthly Archives: May 2007
Displaying DataBase Information in ASP.NET
Have you ever asked this question:
“How can I connect to a database and display it in a web page?”
Unfortunately, that’s pretty much like asking : “What occupations are available for me in the world?” Well, it’s not quite that bad, but there are a lot of different ways to do it. First, let’s break it down into two sections – Connecting to and getting information from the database, and Displaying it in the web page. For each of those sections (which could easily have been broken down further), there are multiple possibilities, which this tutorial will at least partially address.
However, to make this merely a simple tutorial, instead of a book, hitting the highlights of the basic things necessary for Dispaying DataBase information on a web page, using ASP.Net. Also, the only databases covered here will be SQL Server and MS Access, the only language covered here will be VB.Net and we’re only going to address Select Queries, leaving out (updates, deletes and inserts).
- In connecting to a database, there are several items which are necessary:
- Namespaces
- Connection String
- Connection
- SQL Query (or non-query, in the case of an insert, update or delete)
- Opening the Connection
- Executing the Query
- Closing the connection
Namespaces
For both SQL Server and MS Access, we import the System.Data Namespace. Separately, for SQL Server, also:
Continues…
Fill 2nd ListBox based on Selection from First ListBox
Many times we want to choose an item in one listbox, in order to fill another listbox with items that match the criteria selected in the first listbox.
In this example, there are two database tables (Model and BodyStyle) where the actual data is being retrieved. The first listbox is filled on Page_load with the Car Models. Based on the selection from this first listbox, by clicking the button, we retrieve the Body Styles of that particular model.
Just copy this code sample to a new document, import the data (download cardata.zip – includes .sql scripts and data) and change the database/connection information to run it locally on your computer.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.0">
<title>Listboxes</title>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim strConn as string = "server=(local);uid=sa;pwd=password;database=yourdatabase"
Dim MySQL as string = "Select Model from model"
Dim MyConn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
List1.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
List1.DataBind()
list1.selectedindex=0
end if
End Sub
Sub FillList2(Source as Object, E as EventArgs)
DoFill
End Sub
Sub DoFill()
Dim strConn as string = "server=(local);uid=sa;pwd=password;database=yourdatabase"
Dim MySQL as string = "Select bodystyle from bodystyle where modelID =’" & list1.selecteditem.text & "’"
Dim MyConn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(MySQL, MyConn)
MyConn.Open()
List2.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
List2.DataBind()
End Sub
</script>
</head>
<body>
<div align="center"><Form id="form1" runat="server">
<table width="275" border="0">
<tr>
<td align="Left" valign="Top"><b>Choose Model:</b>
<asp:ListBox id="list1" width="125" height="150" datatextfield="Model" runat="server" />
</td>
<td Width="50" align="right" valign="Top"></td>
<td align="Left" valign="Top"><b>Body Styles</b>
<asp:ListBox id="list2" width="125" height="150" Datatextfield="BodyStyle" runat="server" />
</td>
</tr>
<tr>
<td align="center" valign="Top" Colspan="3">
<asp:Button id="button1" Text="Fill Second Listbox" onclick="FillList2" runat="server" />
</td>
</tr>
</table>
</Form>
</div>
</body>
</html>
Specify DataGrid Column Width
Sometimes you have a fixed width for certain columns in your DataGrid that you need to maintain. To do this, you can do this accordingly:
<asp:BoundColumn ItemStyle-width=”250px” />
or
<asp:TemplateColumn ItemStyle-Width=”50%”>
Displaying Sessions/Visitors Online on an ASP.NET Page
One of the more common questions I’ve found on the net is ‘How do you show the number of current users browsing my site?”, like ASPNet101.com has on the bottom left of each page. This tutorial will take the mystery out of this by breaking it down, piece by piece.
It all starts with the Global.asax file. For those of you who have come from the Classic ASP world, you will probably recognize the name of this file, since Classic ASP also had a Global.asa file. Here, we track the active Sessions for our application. There are three subroutines into which we’ll be looking to do this – Application_OnStart, Session_OnStart, and the Session_OnEnd.
First, in the Application_OnStart sub, we basically set the user count to 0, when the server starts
Sub Application_OnStart (Sender as Object, E as EventArgs)
' Set our user count to 0 when we start the server
Application("ActiveUsers") = 0
End Sub
Next, in the Session_OnStart subroutine, there are several things happening:
Sub Session_OnStart (Sender as Object, E as EventArgs)
Session.Timeout = 10
Session("Start") = Now
Application.Lock
Application("ActiveUsers") = Cint(Application("ActiveUsers")) + 1
Application.UnLock
End Sub
Continues…
Changing Page BackGround Color Programmatically
There are several things this sample shows, including creating an arraylist and binding it to a DropDownList, but the main thrust of the sample is to show how to easily change the background color of the existing page programmatically.
<html>
<head>
<meta name=”GENERATOR” Content=”ASP Express 2.0″>
<title>Change Body Color</title>
<script language=”VB” runat=”server”>
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim ColorList as ArrayList= new ArrayList()
ColorList.Add (“AliceBlue”)
ColorList.Add (“AntiqueWhite”)
ColorList.Add (“Aquamarine”)
ColorList.Add (“BlanchedAlmond”)
ColorList.Add (“CadetBlue”)
ColorList.Add (“Chocolate”)
dd1.datasource=ColorList
dd1.databind()
end if
End Sub
Sub changecolor(Source as Object, E as EventArgs)
bdy.attributes(“bgcolor”)=dd1.SelectedItem.Text
End Sub
</script>
</head>
<body runat=”server” id=”bdy”>
<Form id=”form1″ runat=”server”>
<asp:DropDownList id=”dd1″ runat=”server” /><asp:Button id=”button1″ Text=”Change Background Color” onclick=”changecolor” runat=”server” />
</Form>
</body>
</html>
Using Variables from Databound items
These things must be addressed:
1. The variable must be declared at the page level. (inside the script tags, but not inside a sub/method)
2. You must call DataBind() so the DotNet framework will evaluate any code within <%# %> syntax
3. You must use ASP.Net databinding syntax: <%# %>
Sending Bulk Emails in ASP.NET
Just like setting up emailing from a form, in general, is not difficult, neither is sending emails to multiple people at the same time. First, we need to dimension a variable called ‘MyVar’: to hold all the email addresses in a string:
Dim MyVar as String
In inline coding, this would be placed inside the script tag, but outside any Sub or Function.
Next we need to set up the BCC field, grabbing all the emails from a database field called (surprise!) ‘email’:
Dim MySQL as string = "Select email from yourTableName" Dim MyConn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("UrAppString")) Dim objDR as SQLDataReader Dim Cmd as New SQLCommand(MySQL, MyConn) MyConn.Open() objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) MyVar="" While objDR.Read() MyVar+=objDR("email")& ";" End While MyVar=MyVar.substring(0,(MyVar.Length-1))
Continues…
Listbox – Selecting Multiple Items
Sometimes, you will come across a need to select multiple items from a ListBox. This code example shows how to create a multi-select Listbox, and also how to iterate through the item collection to retrieve the text values of the selected items.
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.0">
<title>Listbox – Selecting Multiple Items</title>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
with listbox1.items
.add ("Cherry")
.add ("Vanilla")
.add ("Strawberry")
.add ("Chocolate")
.add ("Banana")
.add ("Apple")
end with
end if
End Sub
Sub doList(Source as Object, E as EventArgs)
Dim Stuff As String
Dim Item As ListItem
For Each Item In ListBox1.Items
If Item.Selected Then
Stuff = Stuff & Item.Text & "<br>"
End If
Next
If Stuff <> "" Then
Label1.Text = "<b>You chose :</b> <br>" & Stuff
Else
Label1.Text = "<b>Hey – Nothing was chosen!</b> "
End If
End Sub
</script>
</head>
<body>
<Form id="form1" runat="server">
<asp:ListBox id="Listbox1"
Height="125px"
width="100px" SelectionMode="Multiple"
runat="server" />
<asp:Button id="button1" Text="Get Items" onclick="doList" runat="server" /><br>
<asp:Label ID="label1" runat="server" />
</Form>
</body>
</html>
Error Using Trusted Connection in DB Connection String
If you are using a Trusted Connection in the connection string to a database and you get the following error:
Login failed for user (null).
Reason: Not associated with a trusted SQL Server connection.
Try adding this to your Web.Config File:
<configuration>
<system.web>
<identity impersonate="true" />
</system.web>
</configuration>