The Starting Point: The Excel Spreadsheet: Visual Web Developer 2005 Express Edition Beta Matrix
The Starting Point: The Excel Spreadsheet: Visual Web Developer 2005 Express Edition Beta Matrix
NET-form to a database
Connecting an ASP.NET form created with ExcelEverywhere to a database is very easy.
We will do it in 3 steps:
1. Calculate and save the form contents into a database.
2. Retrieve previous entered data from the database, show it in the form and let the
user edit it and recalculated and save it again
3. Show all submitted entries so that we can click on them to edit them.
Step 1 can be implemented in several other ways. You do not even need an ASP.NET-
page for that, for example ExcelEverywhere for HTML + our advanced service or an
external tool like Frontpage extension can be used for that.
We are using Visual Studio 2003. If you do not have it, there are free or cheap alternative
IDEs out there: Visual Web Developer 2005 Express Edition Beta, and ASP.NET Web
Matrix. Actually, you do not even need an IDE, a text editor like notepad plus DOT.NET
version 1.1 is enough.
Make sure you both hide the column with 8-22 and the rows, otherwise you will get
blank white space at the bottom of the form.
All cells in the spreadsheet are locked by default, and we have unlocked the 4 input cells
so that ExcelEverywhere can identify them.
We only keep the update button, which is the button that triggers a recalculation, and the
submit button. The submit button will trigger the saving into the database.
We can test the form directly. Pressing Update will recalculate the values. Submit is not
yet implemented and will not do anything.
These are the files that ExcelEverywhere generated:
where serialno is the primary key. In this first part, serialno will have no purpose.We call
the table arrival
We start Access. You can skip this part if you do not have access to MS Access, you can
use the empty database in the zip-file.
Drivers for Access is included in Windows.
Creating the ASP.NET-application
Todo: Create an ASP.NET application and import the ASP.NET-page we created with
ExcelEverywhere.
Start Visual Studio 2003 and create a new C# ASP.NET application project called
FillFromDB.
Import the ASP.NET-page into Visual Studio 2003
We import the generated files into our Visual Studio project using Add Existing Item.
Delete the Src= tag
Click on time_report.aspx to open it and you will get
You have to delete the SRC-attribute. Visual Studio uses CodeBehind instead. A little
article describing the difference between Src and CodeBehind:
http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?ar ticle=22
Delete Src=”time_report.cs”
into
protected System.Web.UI.HtmlControls.HtmlInputHidden serialno;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace time_report
{
if (Request.Params["xl_submit_top"] != null ||
Request.Params["xl_submit_bottom"] != null)
{
// submit was pressed: save and redirect to confirmation page
If not VS2003 has created these attributes automatically, remove the comments before
the field names in the form. Skip p1B5, which is the error message. For me VS2003
created them all except for the today2-field.
protected HtmlInputText name;
protected HtmlInputText today2;
protected HtmlSelect arrival;
protected HtmlSelect departure;
// protected Label p1B5;
protected Label hours;
protected HtmlInputHidden serialno;
We have to adjust the code slightly. Move toForm() directly after docalc() since we need
the calculated values during save. The new code looks like:
docalc();
if (Request.Params["xl_submit_top"] != null ||
Request.Params["xl_submit_bottom"] != null)
The value of a form fields like name are accessed with name.Value. The SQL-statement
that insert the row into the database looks like
“INSERT INTO arrival(arrival, departure, hours, name, today2) VALUES (“ + arrival.Value +
“,“ + departure.Value +, “ + hours.Value +, “ + name.Value +, “ + today2.Value +)”
However, you should never build SQL-statements in this way. It opens up the application
for SQL injection attacks, which enables malicious user to access and delete your
database.
Instead, you should always use SQL parameters, preferable typed parameters. So the
simple line above becomes
OleDbConnection oleDbConnection1 = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\Inetpub\wwwroot\FillFromDB\bin\FillFromDB.mdb");
try
{
oleDbConnection1.Open();
oleDbInsertCommand1.Parameters["arrival"].Value =
Convert.ToSingle(arrival.Value);
oleDbInsertCommand1.Parameters["departure"].Value =
Convert.ToSingle(departure.Value);
oleDbInsertCommand1.Parameters["hours"].Value =
Convert.ToSingle(hours.Text);
oleDbInsertCommand1.Parameters["name"].Value = name.Value;
oleDbInsertCommand1.Parameters["today2"].Value = today2.Value;
try
{
int val = oleDbInsertCommand1.ExecuteNonQuery();
Response.Write("<p>Created</p>");
}
catch (OleDbException /* exc */)
{
Response.Write("<p><strong>Failed to insert data into
database</strong></p>");
}
}
finally
{
oleDbConnection1.Close();
}
You can also view the database from inside Visual Studio 2003. Right-click on the Server
Explorer, Add connection, select Microsoft Jet 4.0 OLE DB Provider, and select the file
MDB-file.
Tip: Close the database in Access after viewing it, otherwise the database might be
locked.
SQL injection
You can read more about SQL injection and why we have to build parameterized SQL-
statements here:
• http://www.developer.com/db/article.php/2243461
• http://dotnetjunkies.com/WebLog/richard.dudley/articles/13706.aspx
• http://blogs.wdevs.com/ColinAngusMackay/archive/2004/09/25/652.aspx
• http://weblogs.asp.net/bleroy/archive/2004/08/18/216861.aspx
Conclusion
Adding code to an ASP.NET-page that stores the contents into a database is easy. By
placing the database code into the codebehind file, it will not interfere with the generated
code, and you and update formulas and formatting in the spreadsheet later, and still keep
you backend integration code intact.
In the next step, we will add more database code that fills the form with data from the
database and lets the user update it.