jeudi 8 décembre 2016

Unable to change label text when Invoking Method through Reflection in c#

First Look at my code

namespace HealthClub
{
    public partial class frmTrainerMaster : Form
    {
        DataTable dt = new DataTable();
        frmHome Home = new frmHome();
        public frmTrainerMaster()
        {
            InitializeComponent();            
        }
    }

    private void frmTrainerMaster_Load(object sender, EventArgs e)
        {            
            FillValues("UserNameText");            
        }


   public void FillValues(string UserName)
    {
        DataTable DT;
        SqlCommand cmd = new SqlCommand();
        try
        {
            cmd.Connection = Connections.Connection[UserName];
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "TrainerMaster_pro";
            cmd.Parameters.AddWithValue("Option", "FillValues".Trim());
            if (Connections.Connection[UserName].State == ConnectionState.Closed)
                Connections.Connection[UserName].Open();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DT = new DataTable();
            adp.Fill(DT);
            lblId___.Text = DT.Rows[0][0].ToString();                
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            cmd.Parameters.Clear();
            cmd.Dispose();
            Connections.Connection[UserName].Close();                
        }
    }
}

Now I am calling FillValues() from another class like this

Type tp = Type.GetType("HealthClub." + FormName);
            object myobj = Activator.CreateInstance(tp);
            MethodInfo method = myobj.GetType().GetMethod("FillValues");
            object[] parametersArray = new object[] { UserName };
            method.Invoke(myobj, parametersArray);            

If you look at the FillValues(), I am assigning the database value to a label. When I am calling it in my first class in page load it's working fine.

But when I am Invoking the medthod from second class, Method invokes but database value does not assign to the label.

What extra effort I need to do ?





Aucun commentaire:

Enregistrer un commentaire