returning more than first element from sql server

C

cm@gowcity

IM AM NEW TO WEB SERVICE AND C# AND HAVE A QUESTION / PROBLEM

MY SIMPLE WEB METHOD

[WebMethod]
public string logtime(string ClientUserName)
{
try
{
SqlConnection sqlConn = new SqlConnection(
"data source=localhost;" +
"initial catalog=isa;integrated security=true");
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "SELECT logTime FROM WebProxyLog " +
"WHERE ClientUserName = '" + ClientUserName + "'";
sqlCmd.Connection = sqlConn;
sqlConn.Open();
string logtime = (string)sqlCmd.ExecuteScalar();
sqlConn.Close();
return logtime;
}
catch(Exception e)
{
throw new Exception("Error calculating cost: " + e.Message);
}
}

NOW THIS RETURNS THE FIRST ELEMENT FROM THE SET RETURNED FROM THE
QUERY (ITHINK) BECAUSE I AM USING EXECUTESCALAR?

HOW COULD I MODIFY THIS TO RETURN ALL INSTANCES SO MY CLIENT
APPLICATION CAN ITERATE THROUGH EACH?

HOPE SOMEBODY HAS TIME FOR A NEWBIE

THANKS IN ADVANCE

COLIN
 
M

Michael Pearson

Return a Dataset.

Do some reading on how to populate a dataset and just return that dataset
from yoru Webmethod. You've got most of the code you need to return the
dataset already.

Michael


cm@gowcity said:
IM AM NEW TO WEB SERVICE AND C# AND HAVE A QUESTION / PROBLEM

MY SIMPLE WEB METHOD

[WebMethod]
public string logtime(string ClientUserName)
{
try
{
SqlConnection sqlConn = new SqlConnection(
"data source=localhost;" +
"initial catalog=isa;integrated security=true");
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "SELECT logTime FROM WebProxyLog " +
"WHERE ClientUserName = '" + ClientUserName + "'";
sqlCmd.Connection = sqlConn;
sqlConn.Open();
string logtime = (string)sqlCmd.ExecuteScalar();
sqlConn.Close();
return logtime;
}
catch(Exception e)
{
throw new Exception("Error calculating cost: " + e.Message);
}
}

NOW THIS RETURNS THE FIRST ELEMENT FROM THE SET RETURNED FROM THE
QUERY (ITHINK) BECAUSE I AM USING EXECUTESCALAR?

HOW COULD I MODIFY THIS TO RETURN ALL INSTANCES SO MY CLIENT
APPLICATION CAN ITERATE THROUGH EACH?

HOPE SOMEBODY HAS TIME FOR A NEWBIE

THANKS IN ADVANCE

COLIN
 
T

Tu-Thach

You can use output parameters

Tu-Thac

----- cm@gowcity wrote: ----

IM AM NEW TO WEB SERVICE AND C# AND HAVE A QUESTION / PROBLE

MY SIMPLE WEB METHO

[WebMethod
public string logtime(string ClientUserName

tr

SqlConnection sqlConn = new SqlConnection
"data source=localhost;"
"initial catalog=isa;integrated security=true")
SqlCommand sqlCmd = new SqlCommand()
sqlCmd.CommandText = "SELECT logTime FROM WebProxyLog "
"WHERE ClientUserName = '" + ClientUserName + "'"
sqlCmd.Connection = sqlConn
sqlConn.Open()
string logtime = (string)sqlCmd.ExecuteScalar()
sqlConn.Close()
return logtime

catch(Exception e

throw new Exception("Error calculating cost: " + e.Message)



NOW THIS RETURNS THE FIRST ELEMENT FROM THE SET RETURNED FROM TH
QUERY (ITHINK) BECAUSE I AM USING EXECUTESCALAR

HOW COULD I MODIFY THIS TO RETURN ALL INSTANCES SO MY CLIEN
APPLICATION CAN ITERATE THROUGH EACH

HOPE SOMEBODY HAS TIME FOR A NEWBI

THANKS IN ADVANC

COLI
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top