views:

45

answers:

4

Sorry for the newb question but I am an experienced old-school programmer but I am new to a lot of the .NET ways and want to do it right. Please forgive any bad terminology or ask for clarification of I butcher it too much.

I have an ASP.NET Web Site I am creating in Visual Web Developer 2008 Express using Visual Basic as the language. It is actually a rewrite-with-mods of an existing ASP script and I'm trying to chew what I've bitten off. I created my page with a text box (txtOutput) on it and since the end product is very complex, and it's generally good practice to separate like functions, I have created 4 separate classes in the App_Code section.

My main page is Sync.aspx and has a code file Sync.aspx.vb with it.

One of my code files is SyncDatabases.vb and in it I have created

Public Class SyncDB  

In that class I have created some routines such as

Public Function ReadMSDB(ByVal SQLString as string) as Boolean

In this routine I want to put information in my text box txtOutput on the main form (the default - Form1).

My problem is that if I try:

Form1.txtOutput.Text = "Hello world" 

or just

txtOutput.Text = "Hello world" 

it says Name 'Form1' (or 'txtOutput') is not declared. I am sure I am missing something simple but have no clue what it is. I assume it's the fact that the write command is in a file (class?) outside the file (class?) containing the page itself but I don't know how to address it properly.

Any help is appreciated but please, don't assume a very high base level knowledge of .Net - I'm an old fart that's not up on these new-fangled contraptions. :) Thanks!

+1  A: 

Hey,

Are you trying to refer to txtOutput.Text within the ReadMSDB method? If you are, you need to pass it in as a reference, as in :

Public Function ReadMSDB(ByVal txtOutput As TextBox, ByVal SQLString as string) as Boolean

And in the code-behind of the web page do:

Dim o as new SyncDB
o.ReadMSDB(txtOutput, "...")

HTH.

Brian
I want to output messages to the text box from within the ReadMSDB method as in txtOutput.Text = "Read 53 records successfully" If I pass it in as shown above can I then do that from within ReadMSDB? I think my issue is too much linear thinking instead of OOP. Thanks!
Deverill
Yes, you can do that because you pass in the direct reference, though yes, it would be better for the ReadMSDB method not to be responsible for working with the UI, but for doing its work, and the UI responding to it (maybe pass back a status object from the method). Separation of Concerns principle.
Brian
+1  A: 

Generally in ASP.Net you want the page class to be driving things. It should call out to the methods in your other classes and set the results to controls. This means that rather than returning a boolean, look at having your ReadMSDB function return a string. That boolean looks like a good candidate for an exception instead.

More than that, though, I look with deep suspicion on any method that accepts a parameter named "SQLString". That tells me there's a good chance you have an sql injection vulnerability in your code because there's no mechanism left to use propery query parameters. You may want to re-think how you're doing your entire data access layer. I know that sounds like a big job, but your current code looks like it has a very serious problem with it. Myself and others have answers all over Stack Overflow with examples of better patterns for this.

Joel Coehoorn
Deverill
A: 

In addition to previous answers.

This is good that you are trying to keep the separation of concerns, but you might want to do this more consciously using one of available patterns. This could help you understand ASP.NET architecture and write a cleaner code from beginning. I will not touch ASP.NET MVC, you can check it yourself.

The first way is to use Model-View-Presenter pattern. Here is a brief description of how to use it: http://www.codeproject.com/KB/architecture/ModelViewPresenter.aspx

The second way is to use Microsoft's Web Client Software Factory. This is more complex way and mostly used in large enterprise applications.

Artem K.
Thank you Artem. I appreciate the pointers to the proper way to do things. Indeed, I am a bit lost when it comes to such things and your links will help.
Deverill
+1  A: 

This answer might be too simple for what you are trying to accomplish, but have you thought about using a partial class instead of 4 separate classes? A partial class would allow you to separate like functions into different files, but will allow you access to controls on your form. The other answers provide good information also, but like a lot of different things with .NET there is more then one way accomplish what you are trying to do.

Matt
I like this. It's probably not the "official" best answer but it will work short term while I work on making it right. Thanks!
Deverill