The ConnectionString property has not been initialized
Nogle der kan hjælpe og fortælle hvorfor dette ikke virker, jeg får fejl om: The ConnectionString property has not been initialized. (linie 19)Her er al min kode:
<%@ Register TagPrefix="JKData" TagName="Menu" Src="menu.ascx" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
<script runat="server" language="c#">
OleDbConnection objConn = new OleDbConnection(ConfigurationSettings.AppSettings["DSN"]);
OleDbDataAdapter objDA;
DataSet objDS = new DataSet();
DataRow objDR;
DataTable objCartDT;
void Page_Load() {
if (!IsPostBack) {
pnlShowCart.Visible = false;
objDA = new OleDbDataAdapter("SELECT * FROM Produkter", objConn);
objDA.Fill(objDS, "Store");
dlItems.DataSource = objDS;
dlItems.DataBind();
}
objCartDT = (DataTable)Session["Cart"];
if (objCartDT == null) {
objCartDT = new DataTable("Cart");
objCartDT.Columns.Add("CartID", typeof(Int32));
objCartDT.Columns["CartID"].AutoIncrement = true;
objCartDT.Columns["CartID"].AutoIncrementSeed = 1;
objCartDT.Columns.Add("ItemID", typeof(Int32));
objCartDT.Columns.Add("Quantity", typeof(Int32));
objCartDT.Columns.Add("Item", typeof(string));
objCartDT.Columns.Add("Cost", typeof(Decimal));
Session["Cart"] = objCartDT;
}
}
void AddToCart(Object s, DataListCommandEventArgs e) {
pnlShowItems.Visible = true;
pnlShowCart.Visible = false;
int ItemID = Convert.ToInt32(dlItems.DataKeys[e.Item.ItemIndex]);
DataSet objItemInfo = FindItem(ItemID);
bool blnMatch = false;
foreach (DataRow objDR in objCartDT.Rows) {
if (Convert.ToInt32(objDR["ItemID"]) == ItemID) {
int Quantity = Convert.ToInt32(objDR["Quantity"]);
Quantity += 1;
objDR["Quantity"] = Quantity;
blnMatch = true;
break;
}
}
if (!blnMatch) {
objDR = objCartDT.NewRow();
objDR["ItemID"] = ItemID;
objDR["Item"] = objItemInfo.Tables["ItemInfo"].Rows[0]["ItemName"];
objDR["Quantity"] = 1;
objDR["Cost"] = objItemInfo.Tables["ItemInfo"].Rows[0]["Cost"];
objCartDT.Rows.Add(objDR);
}
dgCart.DataSource = objCartDT;
dgCart.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
DataSet FindItem(int ItemID) {
DataSet objItemInfo = new DataSet();
objDA = new OleDbDataAdapter("SELECT * FROM EmployeeStore WHERE ItemID=" + ItemID, objConn);
objDA.Fill(objItemInfo, "ItemInfo");
return objItemInfo;
}
Decimal GetItemTotal() {
decimal decRunningTotal = 0;
foreach (DataRow objDR in objCartDT.Rows) {
decRunningTotal += Decimal.Round(Convert.ToDecimal(objDR["Cost"]) * Convert.ToInt32(objDR["Quantity"]), 2);
}
return decRunningTotal;
}
void dgCart_Cancel(Object s, DataGridCommandEventArgs e) {
dgCart.EditItemIndex = -1;
dgCart.DataSource = objCartDT;
dgCart.DataBind();
}
void dgCart_Update(object s, DataGridCommandEventArgs e) {
TextBox txtQuantity;
int intCartID;
intCartID = Convert.ToInt32(dgCart.DataKeys[e.Item.ItemIndex]);
txtQuantity = (TextBox)e.Item.FindControl("txtQuantity");
foreach (DataRow objDR in objCartDT.Rows) {
if (intCartID == (int)objDR["CartID"]) {
objDR["Quantity"] = Convert.ToInt32(txtQuantity.Text);
break;
}
}
lblTotal.Text = "$" + GetItemTotal();
dgCart.EditItemIndex = -1;
dgCart.DataSource = objCartDT;
dgCart.DataBind();
}
void dgCart_Delete(Object s, DataGridCommandEventArgs e) {
objCartDT.Rows[e.Item.ItemIndex].Delete();
dgCart.DataSource = objCartDT;
dgCart.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
void dgCart_Next () {
Response.Redirect("default.aspx");
}
</script>
<title>JK-Data</title>
<link rel="stylesheet" type="text/css" href="../includes/shop.css">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div id="top"></div>
<div id="image"></div>
<div id="center">
<form runat="server">
<asp:Panel id="pnlShowItems" runat="server">
<h1>Employee Store</h1>
<asp:DataList id="dlItems" runat="server" DataKeyField="ItemID" OnEditCommand="AddToCart">
<ItemTemplate>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="200" valign="top" align="center">
<img src="<%# DataBinder.Eval(Container.DataItem, "ImageURL") %>" />
</td>
<td width="200">
<strong><%# DataBinder.Eval(Container.DataItem, "ItemName") %></strong><br />
Cost: $<%# DataBinder.Eval(Container.DataItem, "Cost") %><br /><br />
<asp:Button id="btnAddToCart" runat="server" Text="Add To Cart" CommandName="Edit" />
</td>
</tr>
<tr>
<td colspan="2" width="400"><hr noshade height="1" /></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</asp:Panel>
<asp:Panel id="pnlShowCart" runat="server">
<h1>View Cart</h1>
<asp:DataGrid id="dgCart" runat="server" AutoGenerateColumns="false" GridLines="None" CellPadding="3" Width="100%"
DataKeyField="CartID" OnDeleteCommand="dgCart_Delete" OnUpdateCommand="dgCart_Update">
<HeaderStyle Font-Name="arial" BackColor="#003366" ForeColor="#FFFFFF" Font-Bold="true" Font-Size="10" />
<ItemStyle Font-Name="arial" Font-Size="10" />
<Columns>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Remove" />
<asp:BoundColumn DataField="Item" HeaderText="Product Name" ReadOnly="true" />
<asp:TemplateColumn HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox id="txtQuantity" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' runat="server" Width="50" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Cost" HeaderText="Cost" ReadOnly="true" />
<asp:ButtonColumn ButtonType="PushButton" CommandName="Update" Text="Opdater" />
<asp:ButtonColumn ButtonType="PushButton" CommandName="Next" Text="Næste Trin" />
</Columns>
</asp:DataGrid>
<p align="right"><strong>Total: <asp:Label id="lblTotal" runat="server" /></strong></p>
</asp:Panel>
</td>
</tr>
</table>
</form>
</div>
<div id="left">
<div id="menu"><JKData:Menu id="Menu" runat="server" /></div>
</div>
<div id="footer"></div>
</body>
</html>
På forhånd tak for hjælpen
AK