|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using Components in ASP.NET (C#)Hi,
I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL into the bin directory but am not able to progress. Can anyone please guide me to an online tutorial on the subject. Thanks, ACB "acb" <chrib***@gmail.com> wrote in message What does that actually mean?news:1132828255.250694.112480@o13g2000cwo.googlegroups.com... > am not able to progress. acb wrote:
> Hi, I think you also need to add it to your project's references. You can hi there, find the "references" in your solution panel at the top right hand side. Right click the item and select add reference, then locate your dll file and click ok. Hope that helps Show quoteHide quote > I wrote a DLL Component (using Visual Studio 2005) and managed to > include it into a C# Console application. > > I am now trying to include this component into a Web project. I copy > the DLL into the bin directory but am not able to progress. Can anyone > please guide me to an online tutorial on the subject. > > Thanks, > ACB > Mark and Henry,
Sorry for not explaining myself properly. Thanks to you I have had a better look at the code and the problem was not web vs console, it was how to properly tell the compiler that it should use the newly created namespace. I now have functional code but I haven't yet figured one variation. This is my situation I have the following Class Library: using System; namespace SimpleComponent { public class SimpleComponent { public string SayWelcomeStatement() { return "Hello World"; } } } I compiled it into a release DLL. I then created a console application solution called TextSimpleComponent. I included a reference to the DLL created above and in Program.cs included the following. It works. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SimpleComponent.SimpleComponent scMessage = new SimpleComponent.SimpleComponent(); Console.WriteLine (scMessage.SayWelcomeStatement()); Console.ReadKey(); } } } On the other hand, the code below does not compile using System; using System.Collections.Generic; using System.Text; using SimpleComponent; // this is different namespace ConsoleApplication1 { class Program { static void Main(string[] args) { SimpleComponent scMessage = new SimpleComponent(); // This is different Console.WriteLine (scMessage.SayWelcomeStatement()); Console.ReadKey(); } } } It returns the error: "'SimpleComponent' is a 'namespace' but is used like a 'type' Again thank you for your help. Yo acb,
I am not exactly sure, but why not try a different name for your class name other than the same name of your namespace? That should at least clarify where the error is comming from, the namespace or the class :) Hope that helps acb wrote: Show quoteHide quote > Mark and Henry, > > Sorry for not explaining myself properly. Thanks to you I have had a > better look at the code and the problem was not web vs console, it was > how to properly tell the compiler that it should use the newly created > namespace. I now have functional code but I haven't yet figured one > variation. This is my situation > > I have the following Class Library: > > using System; > > namespace SimpleComponent > { > public class SimpleComponent > { > public string SayWelcomeStatement() > { > return "Hello World"; > } > } > } > > I compiled it into a release DLL. > > I then created a console application solution called > TextSimpleComponent. I included a reference to the DLL created above > and in Program.cs included the following. It works. > > using System; > using System.Collections.Generic; > using System.Text; > > namespace ConsoleApplication1 > { > class Program > { > static void Main(string[] args) > { > SimpleComponent.SimpleComponent scMessage = new > SimpleComponent.SimpleComponent(); > > Console.WriteLine (scMessage.SayWelcomeStatement()); > Console.ReadKey(); > } > } > } > > On the other hand, the code below does not compile > > using System; > using System.Collections.Generic; > using System.Text; > using SimpleComponent; // this is different > > namespace ConsoleApplication1 > { > class Program > { > static void Main(string[] args) > { > SimpleComponent scMessage = new SimpleComponent(); // This > is different > > Console.WriteLine (scMessage.SayWelcomeStatement()); > Console.ReadKey(); > } > } > } > > It returns the error: "'SimpleComponent' is a 'namespace' but is used > like a 'type' > > Again thank you for your help. > "Garfield" <party***@bigpond.net.au> wrote in message That's just common sense, surely...? I can't imagine naming a class the same news:eRb39oP8FHA.2132@TK2MSFTNGP10.phx.gbl... > I am not exactly sure, but why not try a different name for your class > name other than the same name of your namespace? That should at least > clarify where the error is comming from, the namespace or the class :) as the namespace containing it. "Mark Rae" <m***@mark-N-O-S-P-A-M-rae.co.uk> wrote I'm too new on the subject to comment on that :-) My only observation>That's just common sense, surely...? I can't imagine naming a class the same >as the namespace containing it. is that the book ASP.NET by Danny Ryan and Tommy Ryan (Hungry Minds publishers) has examples written in a form in which the namespace and the class have the same names. If this is such a no-no I would suggest that an enhancement would be say a warning by the compiler to that effect. I was impressed by the fact that the compiler tells me that a variable I am using might not have been properly initialised so I don't think that this should be difficult to implement. For the benefit of others who might encounter a problem similar to mine, the problem ***was*** the fact the namespace and the class had the same name. The following code should help demonstrate the problem and reproduce it: ------------------------------------------------------------------------------------------------ Class Library NsNeCl. Build release dll. using System; using System.Collections.Generic; using System.Text; /* In this example, the namespace has a different name to the class. In my tests * this combination works. * * ACB - Nov 2005 */ namespace NsNeCl { public class Class1 { public string SayHello() { return "Hello, World"; } } } ------------------------------------------------------------------------------------------------ Website CallNsNeCl. Include in this project the release dll produced in NsNeCl. Default.aspx code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMessage1" runat="server"></asp:Label> <br /> <asp:Label ID="lblMessage2" runat="server"></asp:Label></div> </form> </body> </html> - - - - - - - - - - - - - - - - - - - - - - File Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NsNeCl; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { /* Since the namespace in included in the class one can automatically * create new objects by referencing classes defined within it */ Class1 NEMessage1 = new Class1(); lblMessage1.Text = NEMessage1.SayHello(); /* The extended version of the code shown above. Here the object is * referenced using both namespace and class */ NsNeCl.Class1 NEMessage2 = new NsNeCl.Class1(); lblMessage2.Text = NEMessage2.SayHello(); } } This solution works. ------------------------------------------------------------------------------------------------ Class Library NsEqCl. Build release dll. using System; using System.Collections.Generic; using System.Text; /* In this example, the namespace carries the same name as the class. In my tests * it did not work * * ACB - Nov 2005 */ namespace NsEqCl { public class NsEqCl { public string SayHello() { return "Hello, World"; } } } ------------------------------------------------------------------------------------------------ Website CallNsEqCl. Include in this project the release dll produced in NsEqCl. Default.aspx code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMessage1" runat="server"></asp:Label> <br /> <asp:Label ID="lblMessage2" runat="server"></asp:Label></div> </form> </body> </html> - - - - - - - - - - - - - - - - - - - - - - File Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NsEqCl; // Intellisence identifies two methods in NsEqCl public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // Error 1 The type or namespace name 'Class1' could not be found (are you missing a using directive or an assembly reference?) //Class1 NEMessage1 = new Class1(); //lblMessage1.Text = NEMessage1.SayHello(); // Error 1 The type or namespace name 'Class1' does not exist in the namespace 'NsEqCl' (are you missing an assembly reference?) //NsEqCl.Class1 NEMessage2 = new NsEqCl.Class1(); //lblMessage2.Text = NEMessage2.SayHello(); } } Hi,
While assembling my previous I made a muck up in the source behind Website CallNsEqCl. I am reposting the source in its entirety below. I would like to point out that the line "using NsEqCl;" may be removed when using the Namespace.Class referencing. Include in this project the release dll produced in NsEqCl. Default.aspx code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblMessage1" runat="server"></asp:Label> <br /> <asp:Label ID="lblMessage2" runat="server"></asp:Label></div> </form> </body> </html> File Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NsEqCl; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // This code does not work //NsEqCl NEMessage1 = new NsEqCl(); //lblMessage1.Text = NEMessage1.SayHello(); // This code works NsEqCl.NsEqCl NEMessage2 = new NsEqCl.NsEqCl(); lblMessage2.Text = NEMessage2.SayHello(); } } So unlike what I observed originally, when namespace = class name it seems that one has to use the namespace.class notation. Regards ACB "acb" <chrib***@gmail.com> wrote in message
http://www.amazon.com/gp/product/0764536176/002-0352563-9176070?v=glance&n=283155&n=507846&s=books&v=glance
news:1132914913.788746.171380@z14g2000cwz.googlegroups.com... > I'm too new on the subject to comment on that :-) My only observation > is that the book ASP.NET by Danny Ryan and Tommy Ryan (Hungry Minds > publishers) has examples written in a form in which the namespace and > the class have the same names. Take a look at some of the reviews, specifically those which comment on the errors in the code examples. > For the benefit of others who might encounter a problem similar to So now you know for yourself that the book you were using leaves a lot to be > mine, the problem ***was*** the fact the namespace and the class had > the same name. desired... :-)
Other interesting topics
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
Can't edit, delete or add row in an Access database in a website 2 GredView {0:d} formatting doesn't work Event 1089 .ascx file as index page? Adding CssClass on .ascx page Horizontal CSS drop-down menu? How to add Label control to DataList? Dynamically Loading the User Control and Passing Parameters to it. References VS Imports |
|||||||||||||||||||||||