Solved

Need help getting 2 values from c# class

Posted on 2009-05-08
10
301 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 63
.NET universe documentation poster 2 22
Why am I losing my session variable value 11 39
Need Help With GDI+ in VB.Net 8 40
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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

732 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