6 Replies - 616 Views - Last Post: 20 February 2010 - 12:02 PM Rate Topic: -----

#1 samartop  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 22-October 09

how make function return more values

Posted 19 February 2010 - 01:50 PM

this code return one value only so how make this code return more value (ex three row from table)
please help me quickly

[WebMethod]
    public string violation_price(string no)
    {

        OleDbConnection cn = new OleDbConnection();
        OleDbCommand cmd = new OleDbCommand();
        OleDbDataReader dr;

        string name;
        double price;
        DateTime date;

        cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=egover3;Data Source=HOME-WS1";


        try
        {
            cn.Open();
            cmd.CommandText = " select voiolation_type,violation_date,violation_price from violation"
                                       + " where vech_no =" + no;

            cmd.Connection = cn;
            dr = cmd.ExecuteReader();

            if (dr.Read())
            {

                name = Convert.ToString(dr.GetString(0));
                date = Convert.ToDateTime(dr.GetDateTime(1));
                price = Convert.ToDouble(dr.GetValue(2));

                string date1 = date.ToString();
                string price1 = price.ToString();


                return "The Data are " + name + " ,price is  " + price1 + "  date ," + date1;
            }
            else
                return "the no not found";

        }
        catch (Exception r)
        {
            return r.Message;
        }
        cn.Close();
    }
}


This post has been edited by eclipsed4utoo: 19 February 2010 - 02:46 PM
Reason for edit:: please use the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how make function return more values

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: how make function return more values

Posted 19 February 2010 - 02:13 PM

Return a string[] array or List<string> instead of string.
Was This Post Helpful? 0
  • +
  • -

#3 samartop  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 22-October 09

Re: how make function return more values

Posted 20 February 2010 - 02:39 AM

View Postjanne_panne, on 19 February 2010 - 01:13 PM, said:

Return a string[] array or List<string> instead of string.


thanks for you
but how i can update this code for work

thanks for you
but how i can update this code for work

thanks for you
but how i can update this code for work
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how make function return more values

Posted 20 February 2010 - 09:46 AM

I personally would use a Generic list for this instead of a string array. Here's how you could do this

[WebMethod]
public List<string> violation_price(string no)
{

    OleDbConnection cn = new OleDbConnection();
    OleDbCommand cmd = new OleDbCommand();
    OleDbDataReader dr;

    string name;
    double price;
    DateTime date;
    List<string> returnValues = new List<string>();

    cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=egover3;Data Source=HOME-WS1";
    try
    {
        cn.Open();
        cmd.CommandText = " select voiolation_type,violation_date,violation_price from violation"
                                   + " where vech_no =" + no;

        cmd.Connection = cn;
        dr = cmd.ExecuteReader();

        if (dr.Read())
        {

            name = Convert.ToString(dr.GetString(0));
            date = Convert.ToDateTime(dr.GetDateTime(1));
            price = Convert.ToDouble(dr.GetValue(2));

            returnValues.Add(name);
            returnValues.Add(string.Format("{0}", price));
            returnValues.Add(date.ToShortDateString());
        }
        else
            returnValues.Add("the no not found");

    }
    catch (Exception r)
    {
        returnValues.Add(r.Message);
    }

        cn.Close();
        return returnValues;
}



Then to find out whether proper values were returned (instead of an exception of the no not found) check the length of the Generic list like so

List<string> yourVar = violation_price("12");

if(yourVar.Count() > 1)
{

}
else
{

}



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#5 samartop  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 22-October 09

Re: how make function return more values

Posted 20 February 2010 - 11:59 AM

View PostPsychoCoder, on 20 February 2010 - 08:46 AM, said:

I personally would use a Generic list for this instead of a string array. Here's how you could do this

[WebMethod]
public List<string> violation_price(string no)
{

    OleDbConnection cn = new OleDbConnection();
    OleDbCommand cmd = new OleDbCommand();
    OleDbDataReader dr;

    string name;
    double price;
    DateTime date;
    List<string> returnValues = new List<string>();

    cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=egover3;Data Source=HOME-WS1";
    try
    {
        cn.Open();
        cmd.CommandText = " select voiolation_type,violation_date,violation_price from violation"
                                   + " where vech_no =" + no;

        cmd.Connection = cn;
        dr = cmd.ExecuteReader();

        if (dr.Read())
        {

            name = Convert.ToString(dr.GetString(0));
            date = Convert.ToDateTime(dr.GetDateTime(1));
            price = Convert.ToDouble(dr.GetValue(2));

            returnValues.Add(name);
            returnValues.Add(string.Format("{0}", price));
            returnValues.Add(date.ToShortDateString());
        }
        else
            returnValues.Add("the no not found");

    }
    catch (Exception r)
    {
        returnValues.Add(r.Message);
    }

        cn.Close();
        return returnValues;
}



Then to find out whether proper values were returned (instead of an exception of the no not found) check the length of the Generic list like so

List<string> yourVar = violation_price("12");

if(yourVar.Count() > 1)
{

}
else
{

}



Hope that helps :)

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how make function return more values

Posted 20 February 2010 - 12:00 PM

Well since you quoted my entire post (not necessary) do you have a statement or question?
Was This Post Helpful? 0
  • +
  • -

#7 samartop  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 22-October 09

Re: how make function return more values

Posted 20 February 2010 - 12:02 PM

thanks thanks
for helping me
:bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1