The basics
The first thing i needed to know is how to include files because in php the master-content pages is set up by including files. Because the server only has the .net 1.1 framework i couldn't work with the master-content pages. This is where i first noticed a big difference between php and asp.net/c#. Where php is an embedded programming language, asp.net is the templating language for c#. You can go the php way and embed the c# code into your html code but if you use the asp.net controls and ids for other html elements you can keep your html clean enough so that a designer can understand what you are doing. All the html ids maybe a torn in the eye of css architects and javascript coders but it's workable.
But now back to the include files syntax. It's a weird html comment but it's easier than in php
<!--# file="c:\httpdocs\site\\test.aspx" -->
<!--# virtual="/site/testpage" -->
The first example is the counterpart of the include function from php even with the same double quote behaviour. The second example is nicer because it's relative to the root directory. In php i had to provide that myself by using a constant.
The next thing was how to get the php $_POST and $_GET globals. That was easy enough Request.Form["test"] gets you the posted form values, Request.QueryString["test"] gets you the url parameters. The Request object is like the $_SERVER global in php but the $_GET and $_POST are a part of it too which is cleaner than in php.
Now i got the basics i can make things work, i thought. c# is a strong typed language so you have to know which sort of content your variable will hold. You can guess Request.QueryString["test"] is a string but to compare it with a numeric variable you need to convert it.
int test = Convert.ToInt32(Request.QueryString["test"]);
The database
Now for the database connection. In php code there is morethan one way to do this but most of the database manipulation code now is object oriented and so is the c# code. The difference in c# is you have to use parametrized queries.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>
<script language="c#" runat="server">
// variables
int number = 1;
string query = "SELECT name FROM testtable WHERE id = ?";
// prepare connection
OdbcConnection conn = new OdbcConnection("Driver={Mysql Odbc 3.15 Driver}; Server=localhost; Database=test; User=user; Password=password; Option=3;");
// prepare database connection
OdbcCommand com = new OdbcCommand(query, conn);
// add value to query string
com.Parameters.Add("",OdbcType.BigInt, 20).Value = number;
// start error handling
try
{
// open connection
conn.Open();
// get database result
OdbcDataReader reader = com.ExecuteReader();
// process database result
while(reader.Read())
{
textboxName.Text = reader.GetString(0);
}
// because it's a single result it can be shorter
textboxName2.Text = com.ExecuteScalar();
reader.Close();
}
// database error
catch (OdbcException ex)
{
labelMessage.Text = ex.Message;
}
// general errors
catch (Exception ex)
{
labelMessage.Text = ex.Message;
}
finally
{
conn.Close();
}
</script>
<html>
<head>
<title>test</title>
</head>
<body>
<p><asp:Label id="labelMessage" runat="server" /></p>
<p><input type="text" id="textboxName" runat="server" /></p>
<p><input type="text" id="textboxName2" runat="server" /></p>
</body>
</html>
To summarize the previous code.
- Define classes needed for database manipulation
- C# code
- Prepare database manipulation
- Display database result
- Html/asp.net code
I will give you some rest now and i hope i helped other php programmers to take their first steps in asp.net/c#.