Solved

Need help getting 2 values from c# class

Posted on 2009-05-08
10
302 Views
Last Modified: 2013-12-17
I have a class that needs to return a string and an int and have searched the knowledgebase to find that I shoudl use another class...which has me a bit lost.

What would be the best way to accomplish this?

public int GetCurrentAttempt(string UserID, int ActID)
        {
            int attemptID;
            string packageID;
            string sSQL = "SELECT TBL_TMX_Attempt.Attempt_PK, TBL_TMX_ActCBT.PackageId FROM iwc_Usr INNER JOIN tblEmp ON iwc_Usr.Usr_EmpFK = tblEmp.Emp_PK INNER JOIN TBL_TMX_Activity INNER JOIN TBL_TMX_Attempt ON TBL_TMX_Activity.Activity_PK = TBL_TMX_Attempt.ActivityFK INNER JOIN TBL_TMX_ActCBT ON TBL_TMX_Attempt.ActivityFK = TBL_TMX_ActCBT.ActivityFK ON tblEmp.Emp_PK = TBL_TMX_Attempt.EmpFK WHERE     (iwc_Usr.Usr_Name = N'" + UserID + "') AND (TBL_TMX_Activity.Activity_PK = " + ActID + ")"
            SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SumTotalSTEP"].ConnectionString);
            SqlCommand cmd = new SqlCommand(sSQL, Conn);
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill();
            if (ds.Tables[0].Rows.Count > 0)
            {
                //Get Attempt_PK and PackageID and return
                try
                {
                    attemptID = int.Parse(ds.Tables[0].Rows[0]["Attempt_PK"].ToString());
                    packageID = ds.Tables[0].Rows[0]["PackageID"].ToString();
                }
                catch
                {
                    attemptID = 0;
                    packageID = "None";
                }
                //need to reutn these 2 values
return attemptID; return packageID;
 
            }

Open in new window

0
Comment
Question by:mattfox77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 3

Accepted Solution

by:
xbrady earned 500 total points
ID: 24340560
You can create a class like this:

public class AttemptPackage
{
      public AttemptPackage()
      {
      }

      private int attemptId
      public int AttemptId
      {
            get
            {
                  return attemptId;
            }
            set
            {
                  attemptId = value;
            }
      }

      private String packageId
      public String PackageId
      {
            get
            {
                  return packageId;
            }
            set
            {
                  packageId = value;
            }
      }
}



Then change your method to be this:
public AttemptPackage GetCurrentAttempt(string UserID, int ActID)
        {
            int attemptID;
            string packageID;
            string sSQL = "SELECT TBL_TMX_Attempt.Attempt_PK, TBL_TMX_ActCBT.PackageId FROM iwc_Usr INNER JOIN tblEmp ON iwc_Usr.Usr_EmpFK = tblEmp.Emp_PK INNER JOIN TBL_TMX_Activity INNER JOIN TBL_TMX_Attempt ON TBL_TMX_Activity.Activity_PK = TBL_TMX_Attempt.ActivityFK INNER JOIN TBL_TMX_ActCBT ON TBL_TMX_Attempt.ActivityFK = TBL_TMX_ActCBT.ActivityFK ON tblEmp.Emp_PK = TBL_TMX_Attempt.EmpFK WHERE     (iwc_Usr.Usr_Name = N'" + UserID + "') AND (TBL_TMX_Activity.Activity_PK = " + ActID + ")"
            SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SumTotalSTEP"].ConnectionString);
            SqlCommand cmd = new SqlCommand(sSQL, Conn);
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill();

            AttemptPackage attemptPackage = new AttemptPackage();
            if (ds.Tables[0].Rows.Count > 0)
            {
                //Get Attempt_PK and PackageID and return
                try
                {
                    attemptPackage.AttemptId = int.Parse(ds.Tables[0].Rows[0]["Attempt_PK"].ToString());
                    attemptPackage.PackageId = ds.Tables[0].Rows[0]["PackageID"].ToString();
                }
                catch
                {
                    attemptPackage.AttemptId = 0;
                    attemptPackage.PackageId = "None";
                }
                //need to reutn these 2 values
            }
            return attemptPackage;
}
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24341041
or just class like this

public class AttemptPackage
{      
      public int attemptId;
      public String packageId;    
}

Open in new window

0
 
LVL 2

Expert Comment

by:kitts
ID: 24342550

if you don't wnt to use the return object, you can use the references in your method header, and then use the variables after the method call.

public void GetCurrentAttempt(string UserID, int ActID, ref int attemptId, ref string packageId)
        {
            //int attemptID;
            //string packageID;
            string sSQL = "SELECT TBL_TMX_Attempt.Attempt_PK, TBL_TMX_ActCBT.PackageId FROM iwc_Usr INNER JOIN tblEmp ON iwc_Usr.Usr_EmpFK = tblEmp.Emp_PK INNER JOIN TBL_TMX_Activity INNER JOIN TBL_TMX_Attempt ON TBL_TMX_Activity.Activity_PK = TBL_TMX_Attempt.ActivityFK INNER JOIN TBL_TMX_ActCBT ON TBL_TMX_Attempt.ActivityFK = TBL_TMX_ActCBT.ActivityFK ON tblEmp.Emp_PK = TBL_TMX_Attempt.EmpFK WHERE     (iwc_Usr.Usr_Name = N'" + UserID + "') AND (TBL_TMX_Activity.Activity_PK = " + ActID + ")"
            SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SumTotalSTEP"].ConnectionString);
            SqlCommand cmd = new SqlCommand(sSQL, Conn);
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;
            adapter.Fill();
            if (ds.Tables[0].Rows.Count > 0)
            {
                //Get Attempt_PK and PackageID and return
                try
                {
                    attemptID = int.Parse(ds.Tables[0].Rows[0]["Attempt_PK"].ToString());
                    packageID = ds.Tables[0].Rows[0]["PackageID"].ToString();
                }
                catch
                {
                    attemptID = 0;
                    packageID = "None";
                }
                //need to reutn these 2 values
//return attemptID; return packageID;
 
            }


0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:mattfox77
ID: 24344421
xbrady

is that legitimate code?  my intellisense is freaking out... it makes no sense to me either.

CAn you explain more about this code?
public class AttemptPackage
{
      public AttemptPackage()
      {
      }
 
      private int attemptId
      public int AttemptId
      {
            get
            {
                  return attemptId;
            }
            set
            {
                  attemptId = value;
            }
      }
 
      private String packageId
      public String PackageId
      {
            get
            {
                  return packageId;
            }
            set
            {
                  packageId = value;
            }
      }
}

Open in new window

0
 
LVL 1

Author Comment

by:mattfox77
ID: 24344436
Cebik
Your post makes no sense... what are you referring to?  Where do I use this code?  how is it like xbrady's post?
0
 
LVL 1

Author Comment

by:mattfox77
ID: 24344446
kitts,
thanks for the input,  but isn't your approach outdated?  i.e. not following OOP philosophy?  
Im a newb so I don;t know,  but posts elsewhere have discouraged me from using references and encourages object,  like xbrady has suggested.
0
 
LVL 1

Author Comment

by:mattfox77
ID: 24344495
xbrady
Ok I dont understand,  but when I paste your code in it seems to work.  But how do I call the method to return the 2 values in my codebehind?

0
 
LVL 1

Author Closing Comment

by:mattfox77
ID: 31579793
Thanks xbrady! exactly what I needed.  It took me a bit to understand it,  but I finally got it working!

I figured out how to call the method
CertStatus AttemptPkg = new CertStatus();
                sAttID = AttemptPkg.GetCurrentAttemptPackage(sUserID, ActID).AttemptId.ToString();
                PackageID = AttemptPkg.GetCurrentAttemptPackage(sUserID, ActID).PackageId.ToString();
               
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24345979
why no sense?
change this class into my (smaller)
and use them the same like this bigger
0
 
LVL 2

Expert Comment

by:kitts
ID: 24355951
Hey mattfox77,

My code was indeed the old way of doing it. But, I have added it because, if you don't want to use the OO route, you could use the old fashioned way. But, hey it works! :-)
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question