Koden til ol.aspx:
<%@ Page language="c#" Inherits="cafecms.ol" CodeFile="ol.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>ol</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="
http://schemas.microsoft.com/intellisense/ie5"> </head>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" height="120" align="center" bgcolor="#6699cc" border="0">
<TR>
<td align="center"><font size="8" color="#ffffff" face="verdana"><b> Administrations område</b></font></td>
</TR>
</table>
<br />
<br />
<table style="width: 680px">
<tr valign="top">
<td style="width: 187px; height: 21px">
<table style="width: 447px">
<tr>
<td style="width: 73px">
Navn</td>
<td style="width: 46px">
<asp:TextBox ID="Navn" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 73px">
Pris</td>
<td style="width: 46px">
<asp:TextBox ID="Pris" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td style="width: 73px; height: 173px" valign="top">
Beskrivelse</td>
<td style="width: 46px; height: 173px" valign="top">
<asp:TextBox ID="Beskrivelse" runat="server" Height="169px" TextMode="MultiLine"></asp:TextBox></td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Gem" /></td>
<td style="width: 644px; height: 21px" valign="top">
<asp:DataGrid ID="DataGrid1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="false"
Width="349px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditItemStyle BackColor="#999999" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundColumn DataField="navn" HeaderText="Overskrift"></asp:BoundColumn>
<asp:BoundColumn DataField="pris" HeaderText="Indhold"></asp:BoundColumn>
<asp:BoundColumn DataField="beskrivelse" HeaderText="Indhold"></asp:BoundColumn>
<asp:HyperLinkColumn Text="Ret" Target="Top" DataNavigateUrlField="id" DataNavigateUrlFormatString="olDetaljer.aspx?id={0}"
HeaderText="Ret">
<FooterStyle ForeColor="#FFCC66"></FooterStyle>
</asp:HyperLinkColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid></td>
</tr>
</table>
<br />
</form>
</body>
</html>
Koden til ol.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace cafecms
{
/// <summary>
/// Summary description for ol.
/// </summary>
public partial class ol : System.Web.UI.Page
{
OleDbConnection myConn;
OleDbDataAdapter adapter;
protected void Page_Load(object sender, System.EventArgs e)
{
string strDSN = @"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=";
strDSN += Server.MapPath("data\\cafe.mdb");
myConn = new OleDbConnection(strDSN);
string sql = "select * From ol";
adapter = new OleDbDataAdapter(sql, myConn);
DataSet ds = new DataSet();
adapter.Fill(ds, "ol");
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
protected void Button1_Click(object src, EventArgs e )
{
{
string sql = "Insert into ol (navn, pris, beskrivelse) Values (@navn, @pris, @beskrivelse)";
OleDbCommand insertCommand = new OleDbCommand(sql, myConn);
insertCommand.Parameters.Add("@navn", OleDbType.VarChar).Value = Navn.Text;
insertCommand.Parameters.Add("@pris", OleDbType.VarChar).Value = Pris.Text;
insertCommand.Parameters.Add("@beskrivelse", OleDbType.VarChar).Value = Beskrivelse.Text;
try
{
myConn.Open();
insertCommand.ExecuteNonQuery();
}
catch
{
Response.Write("Der er sket en fejl");
}
finally
{
myConn.Close();
}
}
}
}
}