| csrocker101 |
Apr 6th, 2007 12:42 PM |
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??
|