|
It looks like you are trying to run a Console application inside a web page, which will never work. Create a simple Console application and things should work out for you. You can also step through your code with the debugger in order to examine the variables at each step.
|
|
|
|
|
Thanks.
I got the binary working.
With XML I get an error "cannot be serialized because it does not have a parameterless constructor" at this part:
XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
Not sure what to change/add.
This is how the code looks now after creating it inside a console application.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Keycard
{
class Class1
{
public static void Main(string[] args)
{
Keycard d1 = new Keycard("John", 102030);
Stream stream = File.Open("KeycardData.dat",
FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, d1);
stream.Close();
d1 = null;
stream = File.Open("KeycardData.dat", FileMode.Open);
bf = new BinaryFormatter();
d1 = (Keycard)bf.Deserialize(stream);
stream.Close();
Console.WriteLine(d1.ToString());
XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
using (TextWriter tw = new StreamWriter(@"C\Brugere\Marc8\source\repos\keycards.xml"))
{
serializer.Serialize(tw, d1);
}
d1 = null;
XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
TextReader reader = new StreamReader(@"C\Brugere\Marc8\source\repos\keycards.xml");
object obj = deserializer.Deserialize(reader);
d1 = (Keycard)obj;
reader.Close();
Console.WriteLine(d1.ToString());
}
}
}
|
|
|
|
|
You need to add the parameterless constructor to your Keycard class as directed by the error message:
public Keycard()
{
this.Name = null;
this.Mykey = null;
}
|
|
|
|
|
I'm looking for an article or sample project that secures a web site, both API and client using .net core 3 and razor pages. The example should NOT be using EntityFramework.
Almost every article/sample I have found is either using EF or is based on core 2.2, I have a folder full of samples where I have downloaded the code, checked the packages and abandoned the project because EF is in there.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
For the clarity...
If you've got .net core 2.2 sample project without EF, what's the problem to refer to .net core 3.0? There's not much changes for ASP.NET projects: What's new in .NET Core 3.0 | Microsoft Docs
Quote: One of the biggest enhancements is support for Windows desktop applications (Windows only). By using the .NET Core 3.0 SDK component Windows Desktop, you can port your Windows Forms and Windows Presentation Foundation (WPF) applications.
|
|
|
|
|
It is an ASP.NeT core razor pages solution. I have three projects in my solution "OdeToFood"
1. OdeToFood (Original Project)
2. OdeToFood.Data (.Net Core Class Library)
3. OdeToFood.Core (.Net Core Class Library)
In OdeToFood.Core, when I use the following using directive in a class in OdeToFood.Core:
using OdeToFood.Data
it works perfect.
Similarly, when I use the following in a class file in OdeToFood.Data
using OdeToFood.Core
it works perfect.
But if I go to the startup.cs file in the "OdeToFood" Project and use
using OdeToFood.Data
It gives an error: The Type or namespace 'Core' does not exist in the namespace "OdeToFood." (Are you missing an assembly reference?)
|
|
|
|
|
Please clarify the references for each project.
Technically, you should reference both Core and Data projects in your main project.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
It sounds like you have a circular reference - OTF.Core depends on OTF.Data , and OTF.Data depends on OTF.Core .
You should refactor your code to remove that cycle from your dependency graph.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I know html, css some fundamentals of js and C#, and I have decided to delve into the world of asp.net. I have learnt that there are two options to begin with, razor pages and mvc. But Razor pages seem to have very little future at present.
Which is better, Razor pages or MVC to begin with? I am only 19 and I want to devote my life to coding
|
|
|
|
|
It sort of depends on your level of experience. If you are fairly skilled at building websites then go straight to MVC. If you are still a little unsure of things then start with Razor (as am I).
|
|
|
|
|
Whichever one you pick, there'll be a new "best" option every 5 years or so.
Concentrate on the fundamentals, and you should be able to transfer your skills to the next "Shiny New Thing" relatively easily.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am very new to ASP.net and C#, so I am trying my best, but there are stille many things that confuse me, especially because when I am looking at various examples online they give me different answers to the same solution, which just makes it even more confusing.
I made a person class, with two subclasses (driver and admin), but I can not get the ArrayList to show up when I run my Index file. I only get a parse error.
What do I need to change to make this work?
My person class
public class Person
{
public Person(string firstName, string lastName, int age, string email)
{
FirstName = firstName;
LastName = lastName;
Age = age;
Email = email;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Email { get; }
public virtual bool ChangeEmail(string email)
{
Email = email;
return true;
}
public override string ToString()
{
return $"Name: {FirstName} {LastName}, Age: {Age}, E-mail: {Email}";
}
}
My subclasses
public class Driver : Person
{
private string v1;
private string v2;
private string v3;
private string v4;
private string v5;
public Driver(string firstName, string lastName, int age, string email, int
licenceNumber)
: base(firstName, lastName, age, email)
{
LicenceNumber = licenceNumber;
}
public Driver(string v1, string v2, string v3, string v4, string v5)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.v5 = v5;
}
public int LicenceNumber { get; set; }
public override string ToString()
{
return $"Role: Traindriver, LicenceNumber: {LicenceNumber}, " + base.ToString();
}
}
public class Admin : Person
{
public Admin(string firstName, string lastName, int age, string email)
: base(firstName, lastName, age, email)
{
}
public override bool ChangeEmail(string email)
{
if (!email.EndsWith("@pig.dk", StringComparison.InvariantCultureIgnoreCase))
return false;
return base.ChangeEmail(email);
}
public override string ToString()
{
return $"Role: Admin, " + base.ToString();
}
}
}
My ArrayList
namespace Pig
{
public partial class Index : System.Web.UI.Page
{
public object ListBoxResults { get; private set; }
public object DriverListBox { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
ArrayList person = new ArrayList();
ListBoxResults.Items.Add(d1.ToString());
ListBoxResults.Items.Add(d2.ToString());
ListBoxResults.Items.Add(a1.ToString());
}
}
}
My Index file
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs"
Inherits="ThePig.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBoxResults" runat="server" Height="800px"
Width="600px"></asp:ListBox>
</div>
</form>
</body>
</html>
|
|
|
|
|
Please indicate the exact error message, as well as the line which is triggering it. "Not working" is not precise enough.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
You have a control with the ID ListBoxResults , which will automatically generate a field in your class:
private ListBox ListBoxResults; You have also declared a property called ListBoxResults in your code-behind.
I'd be extremely surprised if that code compiled; if it did, your references to ListBoxResults in your Page_Load method would be referring to the property, not the control.
Remove the property and your code should start working:
namespace Pig
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
ListBoxResults.Items.Add(d1.ToString());
ListBoxResults.Items.Add(d2.ToString());
ListBoxResults.Items.Add(a1.ToString());
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Okay, I removed the property and also changed "ListBoxResults" to simply "ListBox".
There are some problems remaining.
I will list the errors here (sorry for not doing so at first)
ListBox.Items.Add(d1.ToString());
ListBox.Items.Add(d2.ToString());
ListBox.Items.Add(a1.ToString());
Here I get the error: "An object reference is required for the non-static field, method, or property."
If I remember correctly, a solution to this would be to make a static ArrayList, but I am not sure how I can do it for this code.
Error no. 2
<pre lang="c#">public virtual bool ChangeEmail(string email)
{
Email = email;
return true;
}
Error: "Property or indexer cannot be assigned to -- it is read only"
Error no. 3
public Driver(string v1, string v2, string v3, string v4, string v5)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.v5 = v5;
}
Error message: There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'
Thanks a ton for being patient with me, you guys.
|
|
|
|
|
Member 14671427 wrote: and also changed "ListBoxResults" to simply "ListBox"
Which is not what I said, and is the cause of the "object reference required" errors.
Remove the property; leave the Page_Load method as I showed you:
namespace Pig
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
ListBoxResults.Items.Add(d1.ToString());
ListBoxResults.Items.Add(d2.ToString());
ListBoxResults.Items.Add(a1.ToString());
}
}
}
Member 14671427 wrote: Error: "Property or indexer cannot be assigned to -- it is read only"
You haven't added a setter to the Email property, so you can't set it.
If you don't want to be able to change it from outside of the class, then make the setter private :
public string Email { get; private set; }
Member 14671427 wrote: There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'
Your Person constructor requires four arguments. Every constructor of a class derived from Person must pass those four arguments into the base constructor.
public Driver(string v1, string v2, string v3, string v4, string v5)
: base("??? FIRST NAME ???", "??? LAST NAME ???", 0, "??? EMAIL ???")
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi again and thanks once again.
I did do ask you said. The reason why I tried playing around with the ListBox line is because I am getting an CS0103 error: "The name ListBoxResults does not exist in the current context", which I don't get, because I looked in my index file.
It says
<asp:ListBox ID="ListBoxResults" runat="server" Height="800px" Width="600px"></asp:ListBox>
I checked if the namespace was the issue, but that was not that case.
However, the object reference error is gone, just like you said =)
|
|
|
|
|
That would suggest that your .aspx markup file isn't correctly tied to your .aspx.cs code-behind file.
Check the CodeFile / CodeBehind and Inherits attributes on the <%@ Page ... %> directive in your markup file.
@ Page | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ah, what an idiot I was. In Inhereits, I had written "index" instead of "Index".
Thanks a lot for helping me =)
|
|
|
|
|
Hi Friends,
I have migrated asp.net 2.0 VS 2005 application to VS 2015 (framework 4.5).
Site loading fine. But when i click on the menus available in the page, actually it is not adding localhost in the redirection url.
for Eg:
My Home Page : http://localhost:1087/CGP/Home/Home.aspx. This page is loading fine.
In this page I have side menus and each menu "Profile" will have hyperlink like : CGP/Emp/Profile.aspx
But when I click on the menu "Profile" it is redirecting to http://CGP/Home/Home.aspx.
The term localhost is missing. I am not sure how to handle this.
Please Suggest.
Thanks and Regards
|
|
|
|
|
We'll probably need to see the code you're using to generate the side menu. But at a guess, you should add "~/" to the start of the URL.
So instead of:
NavigateUrl="CGP/Emp/Profile.aspx" use:
NavigateUrl="~/CGP/Emp/Profile.aspx"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
.
modified 20-Nov-19 22:29pm.
|
|
|
|
|
Don't post your question in multiple forums. It's annoying, and it's not like people who look at this forum never come in the webdevelopment-forum.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hello Friends,
i have following code. I am getting Error "The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter[] objects."
in the line
cmd.Parameters.Add(HasPermissionParams).
Please help.
Thanks in Advance.
SqlCommand cmd = GetCommand(Sql_GetPermission);
SqlParameter[] HasPermissionParams = GetInsertParameters(Sql_GetPermission);
SetHasPermissionParams(HasPermissionParams, EmpID, Permission);
cmd.Parameters.Add(HasPermissionParams);
private static void SetHasPermissionParams(SqlParameter[] parms, string EmpID, string Permission)
{
parms[0].Value = EmpID;
parms[1].Value = Permission;
}
private const string Sql_GetPermission = "[sp_Permission]";
private static SqlParameter[] GetInsertParameters(string querytype)
{
SqlParameter[] parms = null;
switch (querytype)
{
case Sql_GetPermission:
parms = new SqlParameter[]
{
new SqlParameter("@EmpID", SqlDbType.VarChar),
new SqlParameter("@PermissionName", SqlDbType.VarChar)
};
break;
}
return parms;
}
|
|
|
|
|
|