Results 1 to 7 of 7

Thread: ASP.NET C# coding

  1. #1
    Funking Prink! Raz316's Avatar
    Join Date
    Jul 2003
    Location
    Deal, Kent, UK
    Posts
    2,978
    Thanks
    130
    Thanked
    62 times in 52 posts

    ASP.NET C# coding

    Hi everyone,

    as a follow on from my previous thread about alternatives to PHP/Mysql. I am giving ASP.NET C# a look.

    Now, in PHP I would have a config.php file included which would create a few object instances based on some class files I had also created ( $conn = new databaseconnection('localhost','bob','dog','bob_db'); $pageview = new pageview($conn,$table_name); )

    each of those objects could then be called later within a specific page and display what is required/expected. I would also include a header and footer php script which have their content created based on variables.

    I was expecting ASP.net c# to be different and I know you compile your application whereas php does it on the fly, but I am really struggling to see how I can do something similar.

    Obviously I need to do alot of reading up to get going with this, but I was expecting this kind of thing to be pretty standard.

    I suppose what I am getting at is, in PHP I do not need to have much code written on a per page basis, but as far as I can currently tell with ASP.net, I have to. Am I missing something here?

    Ta

  2. #2
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: ASP.NET C# coding

    If you're using database connections and suchlike regularly, then it's well worth having a separate set of classes that you maintain separately from your webpages and which handle that side of the application logic. For instance (using SQL Server and a stored procedure with 1 parameter):

    public class DataAdapter {
    private const String CONNSTRING = "my_connection_details";

    public static DataTable GetMyData() {
    SqlConnection conn = new SqlConnection(CONNSTRING);
    SqlCommand cmd = conn.CreateCommand();
    DataTable dt;

    cmd.CommandText = "my_stored_procedure";
    cmd.Parameters.Add(new SqlParameter("foo", "bar"));

    dt.Load(cmd.Executereader());

    cmd.Dispose();
    conn.Close();
    conn.Dispose();

    return dt;
    }
    }

    Then, in your webpage when you need to access the data, you just write "DataTable mydata = DataAdapter.GetMyData();", and the static method is called to retrieve the data. If you always do the same thing with that data you could write another class or method that does that work for you. Using object oriented coding like this makes ASP.NET really powerful.

    If you want to have a single object accessible by any page (slightly odd way of programming) you can use the global.asax file to run code at both application and session startup, to create these objects. You can also use global.asax to subscribe to other events, such as requests.

    If you want headers and footers these can be created as user controls (.ascx files). You can insert these directly into your HTML (after registering them with the page) and use attributes to set runtime properties, or change the properties programatically in the Page_Load() method.

    In fact, there's all sorts of ways to handle the kinds of things you want to do. The important thing to remember is that in ASP.NET you're not just running a single consecutive script from start to finish to create the output. It's far more akin to the lifecycle of object oriented applications. So you can't just "include" a piece of script to run; you need to create classes and objects and manipulate them. There is no limit to what you can have your classes / objects do, though

    Afterthought: As an aside, it is a misconception that ASP.NET applications have to be pre-compiled. They can also be compiled on the fly; IIS then caches the resulting assembly and resuses it until such time as the underlying code files are changed. You can follow the same principle for all the code in your applciation, so the source for classes you write can be stored in a \App_Code\ folder and IIS will compile it when it needs it. Because the assemblies get cached, it's only the first request (after each code change, that is ) to an application that is slowed down by the compilation. On the other hand, you could also write a Class Library using Visual Studio and pre-compile this, placing the resulting dll in a \bin\ folder. This will reduce the compilation time of the first request to the site.
    Last edited by scaryjim; 08-06-2009 at 01:22 PM.

  3. Received thanks from:

    Raz316 (08-06-2009)

  4. #3
    Funking Prink! Raz316's Avatar
    Join Date
    Jul 2003
    Location
    Deal, Kent, UK
    Posts
    2,978
    Thanks
    130
    Thanked
    62 times in 52 posts

    Re: ASP.NET C# coding

    wow thanks Jim I will look at the ascx stuff too (currently looking at lots of www.asp.net videos, which is quite helpful

    I was actually looking at the Global.asax file and looking to create my own database object during application startup. However I was then unable to make reference to it from any .aspx/.aspx.cs pages. (e.g. the name dbConn does not exist in the current content).

    I know I'm probably getting ahead of myself (ie, a lil impatient), so I think I/my bosses are going to have to sit back and wait for me to find my feet.

  5. #4
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: ASP.NET C# coding

    Take your time with ASP.NET. When I moved here it was my first time working with it and it took me several months to settle in. Once you get used to what you can and can't do you'll find it's incredibly powerful. I was lucky in as much as I'd done OO in Java before, so setting up objects etc was second nature.

    I believe you can create public properties in the global.asax file, but they become properties of a custom object derived from HttpApplication, and I'm not sure how you'd go about doing the appropriate cast. Basically every variable you declare (and method / function you write) will be a property of an object or (if you declare it as static) a class. So there's no such thing as a "global" variable, however there will be objects you can access at the Application level. Complex, huh Keep plugging away, it's well worth it in the end.

  6. #5
    Senior Member
    Join Date
    Jul 2004
    Location
    London
    Posts
    2,456
    Thanks
    100
    Thanked
    75 times in 51 posts
    • Mblaster's system
      • Motherboard:
      • ASUS PK5 Premium
      • CPU:
      • Intel i5 2500K
      • Memory:
      • 8gb DDR3
      • Storage:
      • Intel X25 SSD + WD 2TB HDD
      • Graphics card(s):
      • Nvidia GeForce GTX 570
      • PSU:
      • Corsair HX520
      • Case:
      • Antec P180
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • HP w2207 (22" wide)
      • Internet:
      • Rubbish ADSL

    Re: ASP.NET C# coding

    A bit of a thread hijack here, but how easy would you think it is to move from an OO desktop application background (Java) to ASP.NET C# web development? I hear C# is pretty similar to Java in syntax, but is developing for the web in C# much of a different approach to desktop application development?
    I don't mean to sound cold, or cruel, or vicious, but I am so that's the way it comes out.

  7. #6
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: ASP.NET C# coding

    Quote Originally Posted by Mblaster View Post
    A bit of a thread hijack here, but how easy would you think it is to move from an OO desktop application background (Java) to ASP.NET C# web development? I hear C# is pretty similar to Java in syntax, but is developing for the web in C# much of a different approach to desktop application development?
    C# = C++++, kinda cheaky considering the java heritage don't ya think, so its very easy to learn from java, espesually if you know the ancestor!

    http://en.wikipedia.org/wiki/Compari...Sharp_and_Java shows the differences which should be of use to the OP as well.

    Now in .Net when doing web dev there are plenty of Design Patterns to choose from. Web Pages are inheirently stateless, regretably most people want state in them. Its mastering these design patterns that really accelerate peoples web creation abilities, a mate of mine has been desiging his own snazzy little one with a lot of AOP principles, using this the speed at which he can nock up a very complex web app is impressive.
    throw new ArgumentException (String, String, Exception)

  8. #7
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: ASP.NET C# coding

    Pretty much agree with TheAnimus there. As far as syntax goes you'll find yourself typing Java keywords by mistake occasionally (I still use final instead of const on a regular basis then get confused about why my pages don't compile ) but otherwise you'll be bashing on no problem.

    The design patterns issue is a more complex one. Designing for the web *isn't* like writing a desktop app - because the medium is stateless it isn't really suited to having rich user interaction. The problem is that people have started to expect very complex interaction with web pages, which means you need to do one of: a) send a lot of additional data with your pages and use a client-side scripting language like JavaScript to manipulate the page, b) make a lot of round trips between the client and server to simulate responding to user input (remember that each time someone clicks a link or button what they're *really* doing is making a new request to your server, rather than ineracting with a dynamic application), or c) use a plug-in technology like Flash, ActiveX etc. Obviously c) kind of defeats the object to a certain extent because then you're really writing a desktop app that happens to be deployed over the internet, a) has compatibility problems as not all web browsers interpret all scripting languages the same, and b) (which IMNSHO is the correct way to do it ) means you don't get a responsive, dynamic user interface.

    Don't forget that no matter what you program your back end in, a web page is essentially just an HTML document. HTML is not a dynamic, responsive medium - it's a tagged text document. If that's not the medium you want to work in, you're better off using one of the plug-in technologies (which will be more like writing a desktop app).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. ASP.NET + html form problem.. (GCheckout)
    By Spud1 in forum Software
    Replies: 3
    Last Post: 05-10-2007, 01:39 PM
  2. I'm Giving up Coding (but only for the next 3 weeks)
    By TheAnimus in forum General Discussion
    Replies: 24
    Last Post: 12-05-2006, 02:26 PM
  3. ASP > ASP.NET Reference Books?
    By Stoo in forum Software
    Replies: 9
    Last Post: 16-04-2006, 10:42 PM
  4. Replies: 28
    Last Post: 01-06-2005, 03:08 AM
  5. ASP.Net hosting
    By Mart in forum Software
    Replies: 0
    Last Post: 06-10-2003, 09:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •