VS2005 C# Web Newbie Question

E

es330td

Environment: Windows 2003, SQL 2K5, VS2K5 (BTW...I own copies of
these. I am not in a position to upgrade to 2008 or 2010 versions of
any of the above.)

(Note: I am not new to web development or programming. I wrote some
pretty extensive apps in VB.Net and Classic ASP several years ago. My
employer is a C# shop so I decided to use this as an opportunity to
jump to C# from VB.)

After two years out of IT I am trying to learn C# for an app I want to
write. I downloaded a package of examples from MSDN entitled "101 C#
Samples." When I try to open a .csproj it tells me that it needs to
be converted so I am assuming it was created for VS2003. When I try
to run the sample web apps I get errors along the lines of "The type
'Main' already contains a definition for 'btnSave'." It appears to me
that it is doing this because the following lines are in the .aspx.cs
file

public partial class Main: System.Web.UI.Page
{
protected Button btnSave;

while the following line exists in the .aspx file

<asp:Button id="btnSave" Text="Save Changes"

To my uneducated brain, I wouldn't think there shouldn't be a conflict
between the web page and the codebehind page, but it appears that this
is in fact the case. When I was coding in VS2003 and VB.Net this was
not a problem; iirc I would code my objects with the same names to
keep them more clearly connected for readability. I was able to get
one app to run by changing the name of one object to distinguish them
and then work through the code changing the references to the renamed
object until it worked. While I am okay with doing this, and naming
objects different from their codebehind objects, I can't help but
wonder if there is something about which I don't know that would let
me leave these named as they are.

Thanks in advance for your input on this.
 
J

Jeff Johnson

After two years out of IT I am trying to learn C# for an app I want to
write. I downloaded a package of examples from MSDN entitled "101 C#
Samples." When I try to open a .csproj it tells me that it needs to
be converted so I am assuming it was created for VS2003. When I try
to run the sample web apps I get errors along the lines of "The type
'Main' already contains a definition for 'btnSave'." It appears to me
that it is doing this because the following lines are in the .aspx.cs
file

public partial class Main: System.Web.UI.Page
{
protected Button btnSave;

while the following line exists in the .aspx file

<asp:Button id="btnSave" Text="Save Changes"

Actually, the very fact that you have

<asp:Button id="btnSave" Text="Save Changes"

is what's causing

protected Button btnSave;

to be added to the codebehind file. So no, this is not where the problem is.
(It also pretty much confirms that this was originally a .NET 1.x
application, because in 2.0 that control declaration would have been added
to a .Designer.cs file instead of the main .cs file.) Hmmm, now that I say
that, perhaps you should check to see if there is also a .Designer.cs file
and if that button declaration is in there as well. If so, remove it from
the main file. I just recently converted a 1.1 Web app to a 2.0 one and I
didn't have this problem.
 
E

es330td

Actually, the very fact that you have

    <asp:Button id="btnSave" Text="Save Changes"

is what's causing

    protected Button   btnSave;

to be added to the codebehind file. So no, this is not where the problem is.
(It also pretty much confirms that this was originally a .NET 1.x
application, because in 2.0 that control declaration would have been added
to a .Designer.cs file instead of the main .cs file.) Hmmm, now that I say
that, perhaps you should check to see if there is also a .Designer.cs file
and if that button declaration is in there as well. If so, remove it from
the main file. I just recently converted a 1.1 Web app to a 2.0 one and I
didn't have this problem.- Hide quoted text -

- Show quoted text -

As you stated, the conversion log does say it is converting from .Net
1.1 to 2.0. It did not originally have a .Designer.cs file and the
conversion did not create one so it sounds as if I need to learn more
about that before I try to start writing. I was hoping to use these
samples to leverage my 13 years of web development (Yes, I started in
1995 with SQL Server 6.0 and Cold Fusion 1.0) and start coding quickly
but it looks like I'll need a little time learning more about what
happens under the covers to jump into development.
 
J

Jeff Johnson

As you stated, the conversion log does say it is converting from .Net
1.1 to 2.0. It did not originally have a .Designer.cs file and the
conversion did not create one so it sounds as if I need to learn more
about that before I try to start writing. I was hoping to use these
samples to leverage my 13 years of web development (Yes, I started in
1995 with SQL Server 6.0 and Cold Fusion 1.0) and start coding quickly
but it looks like I'll need a little time learning more about what
happens under the covers to jump into development.

Just delete the line that declares the button in the .cs file and try to
build. See if you get a different error.
 
E

es330td

Just delete the line that declares the button in the .cs file and try to
build. See if you get a different error.

This worked. Why is that line necessary if deleting it has no
apparent effect?
 
J

Jeff Johnson

This worked. Why is that line necessary if deleting it has no
apparent effect?

??? But deleting it DID have an effect: it allowed your site to compile!

All I can think is that somehow a .designer.cs file got created and put a
declaration for that button in there but it never got removed from the main
..cs file. I know you said there's no .designer.cs file for that page, but I
recommend you look again....
 
E

es330td

??? But deleting it DID have an effect: it allowed your site to compile!
I got that part. :) I don't understand why it was there, if removing
it didn't break anything. I am guessing it is a convention of
strongly typed coding.
All I can think is that somehow a .designer.cs file got created and put a
declaration for that button in there but it never got removed from the main
.cs file. I know you said there's no .designer.cs file for that page, butI
recommend you look again....

This is my app directory:

Volume in drive C has no label.
Volume Serial Number is B45E-CDCC

Directory of C:\Inetpub\wwwroot\HowToBuildMasterDetailsWebApp

12/09/2010 10:31 AM <DIR> .
12/09/2010 10:31 AM <DIR> ..
12/08/2010 10:36 AM 1,442 about.aspx
12/08/2010 10:36 AM 1,794 about.aspx.cs
09/11/2003 02:21 PM 1,733 about.aspx.resx
12/08/2010 10:36 AM <DIR> App_Code
12/08/2010 10:36 AM 2,006 ConversionReport.txt
12/09/2010 10:31 AM 0 dir.txt
12/08/2010 10:36 AM 82 Global.asax
09/11/2003 02:21 PM 1,733 Global.asax.resx
12/08/2010 10:45 AM 1,612
HowToBuildMasterDetailsWebApp.sln
09/11/2003 02:21 PM 319
HowToBuildMasterDetailsWebApp.vsdisco
12/08/2010 10:45 AM 3,887 main.aspx
12/08/2010 10:47 AM 11,353 main.aspx.cs
09/11/2003 02:21 PM 1,733 main.aspx.resx
09/11/2003 02:21 PM 4,040 ReadMe.htm
09/11/2003 02:21 PM 3,308 Styles.css
12/08/2010 10:36 AM 1,835 UpgradeLog.XML
12/08/2010 10:36 AM 3,225 Web.config
12/08/2010 10:36 AM <DIR> _UpgradeReport_Files
16 File(s) 40,102 bytes

No .designer.cs file in sight
 
A

Arne Vajhøj

I got that part. :) I don't understand why it was there, if removing
it didn't break anything. I am guessing it is a convention of
strongly typed coding.

It should be there for ASP.NET 1.x - it can not be there for
ASP.NET 2.0+.

Arne
 
J

Jeff Johnson

It should be there for ASP.NET 1.x - it can not be there for
ASP.NET 2.0+.

"Cannot"? At all? As I mentioned, I recently converted a big 1.1 app to 2.0
and I noticed that several (not all) control declarations got moved to
..designer.cs files. The declarations are still there, just in a different
place. Please define what you mean by "cannot be there for ASP.NET 2.0."
 
A

Arne Vajhøj

"Cannot"? At all? As I mentioned, I recently converted a big 1.1 app to 2.0
and I noticed that several (not all) control declarations got moved to
.designer.cs files. The declarations are still there, just in a different
place. Please define what you mean by "cannot be there for ASP.NET 2.0."

I am not an ASP.NET expert, but as I remember it then:

ASP.NET 1.x - foobar.aspx is generated into C# code with a
class that inherits from the class in foobar.aspx.cs, controls
are declared in foobar.aspx.cs with visibility of protected so
the generated class inheriting from it can access them, if
not declared in foobar.aspx.cs they are missing.

ASP.NET 2.0+ - foobar.aspx is generated into C# code with a
partial class that is the same as the class in foobar.aspx.cs,
controls are declared in the generated code and can simply
be referred to in foobar.aspx.cs, if declared in foobar.aspx.cs
they will be declared twice in the same class.

People with more ASP.NET knowledge than me - please correct me.

Arne
 
J

Jeff Johnson

I am not an ASP.NET expert, but as I remember it then:

ASP.NET 1.x - foobar.aspx is generated into C# code with a
class that inherits from the class in foobar.aspx.cs, controls
are declared in foobar.aspx.cs with visibility of protected so
the generated class inheriting from it can access them, if
not declared in foobar.aspx.cs they are missing.

ASP.NET 2.0+ - foobar.aspx is generated into C# code with a
partial class that is the same as the class in foobar.aspx.cs,
controls are declared in the generated code and can simply
be referred to in foobar.aspx.cs, if declared in foobar.aspx.cs
they will be declared twice in the same class.

People with more ASP.NET knowledge than me - please correct me.

That's basically it, but the partial class you refer to is split apart
between <page>.aspx.cs and <page>.aspx.designer.cs, and the OP's directory
listing shows that there are no .designer.cs files in sight.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top