0

I have a page which is developed in ASP.NET and C#. On this particular page the logged in users can see who their manager is and the manager contact details etc. The details are being retrieved from a database. I am using the following code

protected string ManagerData()
{
    string UsrName = User.Identity.Name;
    string mName;
    string mNum;
    using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select ManagerName,ManagerNumber from Managers where UserName=@UserName"))
        {
            SqlParameter para = new SqlParameter("UserName", UsrName);
            cmd.Parameters.Add(para);
            cmd.Connection = connection;
            connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                mName = reader.GetString(0);
                mNum = reader.GetString(1);
            }
        }
    }
    return //How would i return both of the string
}

It returns the values. To display them I use the following code

<h5>Your Manager is:<%:ManagerData() %> </h5>

But if I do

<h5>Your Managers Number is:<%:ManagerData() %> </h5> 

It returns the name of Manager in both cases. So my question is should I write a separate method to get the managers number or is there a way to return two strings from the above method?

Thanks in advance for all your help and support

2 Answers 2

1

Pass the parameter to the ManagerData function whether you need name or number.

protected string ManagerData(String sValue)
{
    string UsrName = User.Identity.Name;
    string mName;
    string mNum;
    using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("select ManagerName,ManagerNumber from Managers where UserName=@UserName"))
        {
            SqlParameter para = new SqlParameter("UserName", UsrName);
            cmd.Parameters.Add(para);
            cmd.Connection = connection;
            connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                mName = reader.GetString(0);
                mNum = reader.GetString(1);
            }
        }
    }

    if(sValue = "name")
     return mName;
    else
     return mNum    
}

<h5>Your Manager is:<%:ManagerData("name") %> </h5>

<h5>Your Managers Number is:<%:ManagerData("number") %> </h5> 
Sign up to request clarification or add additional context in comments.

Comments

0

Returning multiple values

You can use "out" parameters for getting multiple values. for eg.

   Public string mName;
   Public string mNum;
        protected void ManagerData(out string mName,out string mNum)
    {
        string UsrName = User.Identity.Name;
        string mName;
        string mNum;
        using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("select ManagerName,ManagerNumber from Managers where UserName=@UserName"))
            {
                SqlParameter para = new SqlParameter("UserName", UsrName);
                cmd.Parameters.Add(para);
                cmd.Connection = connection;
                connection.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    reader.Read();
                    mName = reader.GetString(0);
                    mNum = reader.GetString(1);
                }
            }
        }
        
    }

on Source page

  <h5>Your Manager is:<%= mName %></h5>
     <h5>Your Manager is:<%= mNum %></h5>

After this function executes you can have values of mName and mNum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.