mattfox77
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?
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
SqlConnection Conn = new SqlConnection(Configuratio
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].Row
packageID = ds.Tables[0].Rows[0]["Pack
}
catch
{
attemptID = 0;
packageID = "None";
}
//need to reutn these 2 values
//return attemptID; return packageID;
}
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?
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;
}
}
}
ASKER
Cebik
Your post makes no sense... what are you referring to? Where do I use this code? how is it like xbrady's post?
Your post makes no sense... what are you referring to? Where do I use this code? how is it like xbrady's post?
ASKER
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.
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.
ASKER
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?
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?
ASKER
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.GetCurrentAttem ptPackage( sUserID, ActID).AttemptId.ToString( );
PackageID = AttemptPkg.GetCurrentAttem ptPackage( sUserID, ActID).PackageId.ToString( );
I figured out how to call the method
CertStatus AttemptPkg = new CertStatus();
sAttID = AttemptPkg.GetCurrentAttem
PackageID = AttemptPkg.GetCurrentAttem
why no sense?
change this class into my (smaller)
and use them the same like this bigger
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! :-)
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! :-)
Open in new window