Link to home
Start Free TrialLog in
Avatar of mattfox77
mattfox77Flag for United States of America

asked on

Need help getting 2 values from c# class

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

ASKER CERTIFIED SOLUTION
Avatar of xbrady
xbrady

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cebik
or just class like this

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

Open in new window

Avatar of kitts
kitts


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;
 
            }


Avatar of mattfox77

ASKER

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

Cebik
Your post makes no sense... what are you referring to?  Where do I use this code?  how is it like xbrady's post?
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.
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?

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();
               
why no sense?
change this class into my (smaller)
and use them the same like this bigger
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! :-)