Solved

Need help getting 2 values from c# class

Posted on 2009-05-08
10
296 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now