![]() |
ASP.Net Tutorial #9
It is very useful to be able to use Windows stykle controls (buttons, textboxes etc) on an ASP page to make it look more like a real application. This is achieved by using a combination of HTML, C# code and specially created controls (such as the datagrid we used earlier). Here's some code:-
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>
<%@ OutputCache Duration="1" VaryByParam="none"%>
<html>
<head>
<title>Viewing data from an Access Database using Forms</title>
</head>
<body>
<H3>Viewing data from an Access Database using Forms</H3>
<form runat="server" name="Form1" id="Form1">
<asp:TextBox id="Text1" Text="Name Data" Width="200px" runat="server"/>
<asp:TextBox id="Text2" Text="Number Data" Width="200px" runat="server"/>
<asp:Button OnClick="SubmitBtn_Click" Text="Click here" Runat="server"/>
<asp:Label id="Label1" Text="Number of records" runat="server"/>
<asp:Button OnClick="ClearBtn_Click" Text="Clear" Runat="server"/>
</form>
</body>
</html>
<script language="c#" runat="server">
void ClearBtn_Click(Object Sender, EventArgs e)
{
Text1.Text="";
Text2.Text="";
Label1.Text="";
}
void SubmitBtn_Click(Object Sender, EventArgs e)
{
string answer = null;
string strSQL = null;
DataTable dataTable;
int totalRec=0;
strSQL = "SELECT * FROM fone";
int i = 0;
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" +
"data source = c:\\inetpub\\wwwroot\\newmdb.mdb";
OleDbConnection conn = new OleDbConnection(connectionString);
try
{
conn.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "fone");
dataTable = ds.Tables["fone"];
totalRec = dataTable.Rows.Count;
Label1.Text="Total recs= " + totalRec;
for(i=0; i<totalRec; i++)
{
Text1.Text=dataTable.Rows[i]["strName"].ToString();
Text2.Text=dataTable.Rows[i]["strNumb"].ToString();
}
}
catch (Exception ex)
{
Response.Write("Failed to connect to data source" + "<BR>");
Response.Write(ex.Message + "<BR>");
}
finally
{
conn.Close();
}
}
private void Page_Load(object sender, System.EventArgs e)
{
string answer = null;
string strSQL = null;
DataTable dataTable;
int totalRec=0;
strSQL = "SELECT * FROM fone";
int i = 0;
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" +
"data source = c:\\inetpub\\wwwroot\\newmdb.mdb";
OleDbConnection conn = new OleDbConnection(connectionString);
try
{
conn.Open();
Response.Write("Connected to data source" + "<BR>");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
dataAdapter.Fill(ds, "fone");
dataTable = ds.Tables["fone"];
totalRec = dataTable.Rows.Count;
Response.Write("Total recs= " + totalRec + "<BR>");
for(i=0; i<totalRec; i++)
{
Response.Write(dataTable.Rows[i]["strName"].ToString() + "<BR>");
Response.Write(dataTable.Rows[i]["strNumb"].ToString() + "<BR>");
}
}
catch (Exception ex)
{
Response.Write("Failed to connect to data source" + "<BR>");
Response.Write(ex.Message + "<BR>");
}
finally
{
conn.Close();
Response.Write("Data source closed"+ "<BR>");
}
}
</script>
Wow, quite a lot. I'll got through it a bit at a time. It is split into two sections with the HTML looking like this:-
<html>
<head>
<title>Viewing data from an Access Database using Forms</title>
</head>
<body>
<H3>Viewing data from an Access Database using Forms</H3>
<form runat="server" name="Form1" id="Form1">
<asp:TextBox id="Text1" Text="Name Data" Width="200px" runat="server"/>
<asp:TextBox id="Text2" Text="Number Data" Width="200px" runat="server"/>
<asp:Button OnClick="SubmitBtn_Click" Text="Click here" Runat="server"/>
<asp:Label id="Label1" Text="Number of records" runat="server"/>
<asp:Button OnClick="ClearBtn_Click" Text="Clear" Runat="server"/>
</form>
</body>
</html>
This is setting up all the controls we need with a textbox for each field in the database and a button to get the data. We also need a clear button because simply refreshing the browser won't clear the boxes. Each control is given a name and the buttons have events. The clear button event is here:-
void ClearBtn_Click(Object Sender, EventArgs e)
{
Text1.Text="";
Text2.Text="";
Label1.Text="";
}
and is within script tags as before. Page_load introduces a slightly different way of getting data out of the database and uses a dataadapter more in keeping with my other C# Tutorials. The SubmitBtn_Click event is handled to take data from the database and insert it into the text boxes although it is currently limited to displaying just one record - you need to add more buttons to get it to move through the records something I'll leave you to experiment with.
That's it for these tutorials. Thanks for looking and I hope they were useful. There may be some more in the pipeline as I'm interested in .Net controls on web pages and remoting. In the meantime, you can find my other tutorials for other languages on my main site here. Remember, if you get stuck or need help then email me at the address below and I'l try to help. See you around...