Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need help getting 2 values from c# class

Posted on 2009-05-08
10
Medium Priority
?
305 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 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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