Skip to content

Instantly share code, notes, and snippets.

@dJani97
Last active December 10, 2018 01:42
Show Gist options
  • Save dJani97/d1941dc85e09d0df3e94efeade7de8d9 to your computer and use it in GitHub Desktop.
Save dJani97/d1941dc85e09d0df3e94efeade7de8d9 to your computer and use it in GitHub Desktop.
C# snippets for Software Engineering class
FORRÁSOK:
Witch:
ftp://witch.pmmik.pte.hu:2001/Tanszeki_anyagok/Rendszer-_es_Szoftvertechnologiai_Tanszek/Szendroi_Etelka/
Peti repo:
https://github.com/izzual/karacsonyfazh
Jani repo:
https://github.com/dJani97/asp.net-bike-webshop
EGYÉB:
Szavazás:
<a href="/VoteForObject.aspx?id=<%#: Item.ID %>">VOTE!</a>
Megjelenítés:
GridView, egyszerű datasource-al
using System.Collections.Generic;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Web.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace BikeStore.Models
{
public class Bicycle
{
public int BicycleID { get; set; }
[Required, StringLength(60)]
public string ModelName { get; set; }
public int Price { get; set; }
public Nullable<int> CategoryID { get; set; }
public virtual Category Category { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace BikeStore.Models
{
public class BicycleContext : DbContext
{
public BicycleContext() : base("WebShop") { }
public DbSet<Bicycle> Bicycles { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.IO;
namespace BikeStore.Models
{
public class BicycleDbUpload : DropCreateDatabaseIfModelChanges<BicycleContext>
{
protected override void Seed(BicycleContext context)
{
foreach(Category item in ReadCategories())
{
context.Categories.Add(item);
}
foreach (Bicycle item in ReadBicycles())
{
context.Bicycles.Add(item);
}
}
private static List<Bicycle> ReadBicycles()
{
StreamReader reader = File.OpenText(HttpContext.Current.Server.MapPath("~/App_Data/bicikli.txt"));
var bikes = new List<Bicycle>();
Bicycle bike;
string[] line;
while (!reader.EndOfStream)
{
line = reader.ReadLine().Split(';');
bike = new Bicycle()
{
BicycleID = int.Parse(line[0]),
ModelName = line[1],
Manufacturer = line[2],
Type = line[3],
Price = int.Parse(line[4]),
ImageFile = line[5],
CategoryID = int.Parse(line[6])
};
bikes.Add(bike);
}
reader.Close();
return bikes;
}
private static List<Category> ReadCategories()
{
StreamReader reader = File.OpenText(HttpContext.Current.Server.MapPath("~/App_Data/katadat.txt"));
var categories = new List<Category>();
Category category;
string[] line;
while(!reader.EndOfStream)
{
line = reader.ReadLine().Split(';');
category = new Category()
{
CategoryID = int.Parse(line[0]),
CatName = line[1]
};
categories.Add(category);
}
reader.Close();
return categories;
}
}
}
<%@ Page Title="Bikes" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="BikeList.aspx.cs" Inherits="BikeStore.BikeList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<section>
<div>
<hgroup>
<h2> <%: Page.Title %> </h2>
</hgroup>
</div>
<asp:ListView runat="server" ID="lstViewBikes" GroupItemCount="4" ItemType="BikeStore.Models.Bicycle" DataKeyNames="BicycleID" SelectMethod="GetBikes">
<EmptyDataTemplate>
<table>
<tr><td>No data!</td></tr>
</table>
</EmptyDataTemplate>
<EmptyItemTemplate>
<td> </td>
</EmptyItemTemplate>
<GroupTemplate>
<tr id="itemPlaceholderContainer" runat="server">
<td runat="server" id="itemPlaceholder"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td runat="server">
<table>
<tr>
<td>
<a href="BikeDetails.aspx?BicycleID=<%#: Item.BicycleID %>">
<img src="/Catalog/Thumbs/<%#: Item.ImageFile %>" width="100" height="75" style="border:solid"/>
</a>
</td>
</tr>
<tr>
<td>
<a href="BikeDetails.aspx?BicycleID=<%#: Item.BicycleID %>">
<span> <%#: Item.ModelName %> </span>
</a>
<br />
<span> <b>Ár:<b/> <%#: String.Format("{0:c}", Item.Price) %> </span>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
</tr>
</table>
<p></p>
</td>
</ItemTemplate>
<LayoutTemplate>
<table style="width:100%">
<tbody>
<tr>
<td>
<table id="groupPlaceholderContainer" runat="server" style="width:100%">
<tr id="groupPlaceholder"></tr>
</table>
</td>
</tr>
<tr><td></td></tr><tr></tr>
</tbody>
</table>
</LayoutTemplate>
</asp:ListView>
</section>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
using BikeStore.Models;
namespace BikeStore
{
public partial class BikeList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) { }
public IQueryable<Bicycle> GetBikes([QueryString("id")] int? categoryID) {
var db = new BicycleContext();
IQueryable<Bicycle> query = db.Bicycles;
if(categoryID != null && categoryID.HasValue) {
query = from line in query
where line.CategoryID == categoryID
select line;
}
return query;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BikeStore.Models;
namespace BikeStore {
public partial class VoteForBike : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
string requestedID = Request.QueryString["id"];
if (!String.IsNullOrEmpty(requestedID) && int.TryParse(requestedID, out int bikeID)) {
var db = new BicycleContext();
Bicycle bicycle = (from bike in db.Bicycles
where bike.BicycleID == bikeID
select bike).FirstOrDefault();
bicycle.Votes++;
db.SaveChanges();
}
else {
Response.Redirect("404.aspx");
}
Uri previousUri = Request.UrlReferrer;
if (previousUri != null)
Response.Redirect(previousUri.ToString());
else {
Response.Redirect("BikeList.aspx");
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-BikeStore-20181209105656.mdf;Initial Catalog=aspnet-BikeStore-20181209105656;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="WebShop" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BikeStore.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment