Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 6th, 2007, 11:42 AM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 41
Rep Power: 0 csrocker101 is on a distinguished road
Method not returning string

I have a method that is supposed to returning a string but is not. When I try to reference the method in another program, the data type its supposed to return is showing up as null, signifying that nothing got passed. Here is what my program looks like.
namespace _8246_Server_Application
{
    public delegate void MesageEventHandler(object sender, string msg);
    
    public class ServerApp
    {
        private string status = "Connected" ;
        NetworkStream socketStream;
        DatabaseApp databaseapp = new DatabaseApp();
        string SQLcommand = "hello";

        public event MesageEventHandler MessageHandler;
        
        public void OnMessage(string message)
        {
            if (MessageHandler != null)
            {
                MessageHandler(this, message);
            }
            
        }
        public void runServer()
        {    
            //creates a TcpListener Object to listen for incoming connections
            TcpListener listener;
            //creates a socket to send and recieve data
            Socket connection;
            //creates a network stream
            
            try
            {
                //creates an instance Tcp listenter to listen for connections on port 43
                listener = new TcpListener(IPAddress.Any, 43);

                while (true)
                {
                    //begins listening for incoming connections
                    listener.Start();
                    //accepts socket if request is made to connect
                    connection = listener.AcceptSocket();
                    //sets a network stream instance to the connection socket
                    socketStream = new NetworkStream(connection);
                    //calls method doRequest which takes a socketstream and handles its request
                    doRequest();
                    //closes the socket stream
                    socketStream.Close();
                    //closes connection
                    connection.Close();
                   
                }
                
            }
            catch (Exception e)
            {
                e.ToString();
            } 
        }
        public string doRequest()
        {
            //streamReader reads incoming connection from client
            StreamWriter sw = new StreamWriter(socketStream);
            StreamReader sr = new StreamReader(socketStream);
            //writes in coming data
           //Assigns data from the streamreader to a string
            string clientInfo = sr.ReadLine();
            int pos = clientInfo.IndexOf(" ");
            if(pos > 0)
            {
                SQLcommand = "UPDATE information SET location = 'In the Lab' WHERE username = '123456'";
                status = "Database Updated";
            }  
            else 
            {
                status = "Searched Database";
                SQLcommand = "SELECT From D";
            }
          
           sw.WriteLine(status);
           sw.Flush();
           OnMessage(databaseapp.RunDatabase());
           return SQLcommand;
        }
    }
}

The main problem I am having is in my DoRequest Method. OnMessage is a method call which invokes a event message handler that when recieves a request starts the database then prints out the database info to a windows form. The database.Rundatabase()) is a reference to my database application which which returns a string containing the database application which is then printed to the windows form. My problem I am having is that when I try and call the doRequest() method in my database application to pass an SQL statement it comes up as null.
public string RunDatabase()
        {
            ServerApp serverapp = new ServerApp();

            
            
            //create a connection to a Microsoft database entited Server.mdb in the C drive
            OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\ACWcoursework.mdb");
        
           //creates global OleDbCommand to be used later in the program
           OleDbCommand readCommand = new OleDbCommand();
          
         
           //if user inputs anything else than database displayed in table
           readCommand = new OleDbCommand(serverapp.DoRequest(),    Connection);

Everytime I try and reference the serverapp.DoRequest() it comes up as null and is not returning the string I wish to pass it. I used a break, and SQL command is what I want it be but when I use the "step into" function once it goes to the OnMessage() function it skips my return statement. I tried putting the OnMessage() function somewhere else but it doesnt work properly. Any ideas??
csrocker101 is offline   Reply With Quote
Old Apr 6th, 2007, 6:45 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
It looks like your doRequest method is calling databaseapp.RunDatabase() and then databaseapp.RunDatabase() is calling doRequest. This continues until your computer runs out of memory.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 8:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 5:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 10:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 12:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC