Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK



Articles

1.Windows API reference
2.HTML to WML Converter
3.Webcam streaming in VB.NET
4.Remoting with firewalls
5.RSA from first principles
6.Key & MouseLogger in .NET
7.Networking Resource Kit for .NET
8.Automatic Reboot with .NET
9.XAML Schema
10.Migrating VB6 Winsock to VB.NET
11.Migrating C++ sockets to C#
12.RFC Reference guide
13.Lingua - Localization webservice
14.COM Reference guide
15.WMI Reference guide
16.SQL stored procedures
17.TCP & UDP port reference
18..NET Framework reference
19.Ethernet Type codes
20.IP to country webservice
21.MAC address assignments
22.DLL entry point reference
23.WHOIS server list
24. Turing Numbers
25. Boost SQL performance
26. Progress Bar in ASP.NET
27. OleDb WebService
27. Internet Explorer

Contact us

Developing a website with ASP.NET 2.0, Visual Web Developer 2005

This article is a brief synopsis of the development of a website developed in .NET 2.0, using Visual Web Developer. The idea behind the website, was to develop a software download site. Yes, there are hundreds of these types of site about, but since I recently bought an expired domain name, www.downloadsoft-ware.co.uk, I thought I put it to good use.

But why use .NET 2.0 (formerly Whidbey)?, well, .NET 2.0 has a tree view control, which is excellent for showing XML, and as people familiar with software publishing, the de-facto standard for publishing information about new software releases is the PAD format, which is XML, under the hood. And of course, I wanted to try out .NET 2.0, and, my ultimate aim is to provide .NET 2.0 hosting.

So first step was to install Visual Web Developer 2005 on my webserver, which is a dedicated Win 2003 machine. Which took alot of time, and three reboots, but everything went smoothly. The only side effect, I saw was that every folder in my wwwroot that was mapped as a web application, had two new folders, aspnet_client and aspnet_webadmin.

I then set about writing a web page that bound a remote pad xml file to a treeview in .NET 2.0. My first approach was to drop a treeview onto the form, Click "Choose data source > New Data Source" then "XML File". On the XmlDataSource, I clicked "Configure Data Source", and entered the PAD url into the "DataFile" box. I ran the web application, and hey presto, I got a treeview of the XML file. I also found out that I could change the style of the treeview by pressing "AutoFormat". Zero lines of code, but with one major flaw, only the tag names were showing. I tried to fix this by researching into TreeNodeBindings, but drew a blank.

So, I removed the XmlDataSource, and delved into the .cs file. Without going into detail, here's the code

void Page_Load(object sender, EventArgs e)
    {
        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = "http://www.audio4fun.com/padfiles/vcsgold_pad.xml";
        XmlDocument root = xds.GetXmlDocument();
        TreeNode trunk = recurseXML(root.DocumentElement);
        this.TreeView1.Nodes.Add(trunk);
    }

    public static string stripHTML(string HTML)
    {
        string cleaner = HTML;
        cleaner = Regex.Replace(cleaner, @"<script(.|\n)*</script>", string.Empty, RegexOptions.IgnoreCase);
        cleaner = Regex.Replace(cleaner, @"<(.|\n)*?>", " ");
        cleaner = Regex.Replace(cleaner, @"&(.|\n){4};", " ");
        cleaner = cleaner.Replace("\n", " ");
        cleaner = cleaner.Replace("\t", " ");
        cleaner = Regex.Replace(cleaner, "\\s+", " ");
        return cleaner;

    }
    public TreeNode recurseXML(XmlNode parent)
    {
        if (!parent.HasChildNodes)
        {
            return null;
        }
        else
        {
            TreeNode branch = new TreeNode(parent.Name);
            foreach (XmlNode childElement in parent.ChildNodes)
            {                
                TreeNode childNode = recurseXML(childElement);
                if (childNode != null)
                {
                    branch.ChildNodes.Add(childNode);
                    if (childNode.ChildNodes.Count==0)
                    {
                        childNode.Text = stripHTML(childElement.Name + ": " + childElement.InnerText);
                    }
                }                
            }
            return branch;
        }
    }


Since I had installed SQL server 2005, I decided to take advantage of it, to store the data for this website, for no better reason, than I wanted to learn how to use it. Now, by default, the only access you get to SQL server 2005 is via windows authentication, If you want to change this to mixed mode, you have to make this change in the registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer

LoginMode =2

(restart SQL server) Then you can add a new user, in using the command line: SQLCMD -S.\SQLEXPRESS -E

sp_addLogin 'user', 'password'
GO
sp_addsrvrolemember 'user', 'sysadmin'
GO
Being honest, this still didn't help me use the database, since there's no GUI, and I couldn't get Enterprise manager to log into it. (ok version 2000 vs 2005, so nothing unexpected). I then created a new Database called GeneralPurpose, and created a table within it called PADUrls, and manually inserted a new row. I personally hate command line interfaces to databases, since it reminds me of MySQL.

The first step was to create the connection string in the web.config file, which I initially entered as an OleDb connection, (Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=GeneralPurpose;Data Source=.\SQLEXPRESS) however, as I was to find out later, this crashes with "No error information available: DB_E_ERRORSOCCURRED(0x80040E21).", so I replaced it with a SQL server connection string. thus:
    <appSettings>
        <add key="dsn" value="Server=.\SQLEXPRESS;Integrated Security=True;Database=GeneralPurpose"/>
    </appSettings>
As I was to find out later, the IIS worker process didn't have access to the database, since it wasn't running under the administrator account. This resulted int the following error:
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
Which I fixed by logging in under SQLCMD (formerly osql), and entering:
sp_addsrvrolemember 'nt authority\network service', 'sysadmin'

The next step was to flesh out the ASPX code, here, I used a repeater, thus:
<asp:repeater id="rptSubmissions" runat="server">
            <itemtemplate>
               <span class="blackText"><%#DataBinder.Eval(Container.DataItem, "programTitle")%></span><br />
             </itemtemplate>
           </asp:repeater>
Which required the following back-end code:
void Page_Load(object sender, EventArgs e)
    {
     string sql = "select * from generalpurpose..padurls";
     DataSet ds = ExecuteDataSet(sql);
     rptSubmissions.DataSource = new DataView(ds.Tables["sql"]);
     rptSubmissions.DataBind();
    }

    public DataSet ExecuteDataSet(string sql)
    {
        DataSet ds = new DataSet();
        SqlConnection DSN = new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
        DSN.Open();
        SqlCommand Database = new SqlCommand(sql, DSN);
        SqlDataAdapter Adapter = new SqlDataAdapter(Database);
        Adapter.Fill(ds, "sql");
        DSN.Close();
        return ds;
    }

    public void ExecuteNonQuery(string sql)
    {
        // flesh out method here
        SqlConnection DSN = new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
        DSN.Open();
        SqlCommand Database = new SqlCommand(sql, DSN);
        Database.ExecuteNonQuery();
        DSN.Close();
    }


To design the HTML layout, I decided to avail of the masterPage functionalilty built into asp.net 2.0. Really quite straight forward. You create a master page, then right click on it in solution explorer to create a content page. In the content page, you swap to design to design view , right click, select edit content, and type away. What I did find however, that it reverts to the inline code model, rather than the code-behind model. Which is still workable, but reduces coupling between the logic and design, and intellisense doesn't work as well.

To see the whole thing in action, go to downloadsoft-ware.co.uk.

Free ASP.NET 2.0 Hosting offer

If you would like to experiment with ASP.NET 2.0 on a live server, put a comment on this page, and I'll get back to you. The offer of free space would be subject to terms such as, you give me credit (i.e. a backlink from your main website), and don't blow the CPU useage, or deadlock the web server.




Google

Free SMS UK Free SMS Ireland SMS Gratis Norway SMS Gratis Sverige Ilmainen SMS Suomi SMS Gratis Danmark SMS Tasuta Eestisse SMS Nemokamai Lietuva SMS Bezmaksas Latviju Darmowe smsy Polska SMS Zdarma Ceské SMS Zdarma Slovensko SMS Gratis Deutschland SMS Gratis Schweiz SMS Gratis Österreich SMS Gratuit Belgique SMS Gratis Nederland SMS Gratuit France SMS Gratis Espańa SMS Gratis Portugal Free SMS South Africa Free SMS USA SMS Percuma Malaysia Free SMS Hong Kong