none
How to show rows details of datatable function return on same function ? RRS feed

  • Question

  • I have windows form frmmembers have 4 controls textbox 

    textboxid

    txxtboxName

    textboxaddress

    pictureboxpersonal image

    I have class name annualmember have function GetMembersData

    public DataTable GetMembersData()
            {
                string sql = "select * from ViewGetPunchEmployee";
                DataTable GetMembers = DataAccess.ExecuteDataTable(sql);
                return GetMembers;
            }

    when call function GetMembersData on windows form  frmmembers it will be

    annualmember annual=new annualmember();

    Datatable dtgetmembers=new Datatable();

    dtgetmembers = annaul.GetMembersData(); if (dtgetmembers.Rows.Count > 0) { txtName = convert.tostring(dtgetmembers.Rows[0]["Name"]);

    txtAddress = convert.tostring(dtgetmembers.Rows[0]["ADDRESS"]);

    etc.......... }

    my questions is How to get details of datatable on same function meaning

    these lines 

    if (dtgetmembers.Rows.Count > 0)
                {
    TXTNAME = convert.tostring(dtgetmembers.Rows[0]["Name"]);
    
    etc..........
                }

    i need when calling  getmembersdata function controls assign no need to asssign controls to every windows form

    dtgetmembers = annaul.GetMembersData();
    Are this possible and how if that possible ?


    • Edited by engahmedbarbary Tuesday, April 2, 2019 9:01 AM
    • Moved by CoolDadTx Tuesday, April 2, 2019 1:51 PM Winforms related
    Tuesday, April 2, 2019 3:13 AM

Answers

  • Hi

    Thanks for the feedback.

    Do you want to add controls when load the table details? If yes, you could try the following code. If no, please feel free to tell me.

      if (dtgetmembers.Rows.Count > 0)
                {
                    TextBox txtname = new TextBox();
                    txtname.Location = new Point(25, 25);
                    this.Controls.Add(txtname);
                    TextBox txtaddress = new TextBox();
                    txtaddress.Location = new Point(25, 50);
                    this.Controls.Add(txtname);
                    TextBox txtID = new TextBox();
                    txtID.Location = new Point(25, 75);
                    this.Controls.Add(txtname);
                    this.Controls.Add(txtaddress);
                    this.Controls.Add(txtID);
                    txtname.Text = Convert.ToString(dtgetmembers.Rows[0]["Name"]);
                    txtaddress.Text = Convert.ToString(dtgetmembers.Rows[0]["Address"]);
                    txtID.Text = Convert.ToString(dtgetmembers.Rows[0]["ID"]);
    
                }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 3, 2019 5:55 AM

All replies

  •  

    Hi 

    Thank you for posting here.

    >>i need when calling  getmembersdata function controls assign no need to asssign controls to every windows form

    I could not understand the meaning of the sentence, could you explain it more clearly?

    We are waiting for your update.

    Best regards,

    Jack

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 2, 2019 5:59 AM
  • I need when initialize data to data table data loaded to controls 

    meaning Function GetMembersData() that return datatable 

    i need that function when calling  to assign data to controls also

    dtgetmembers = annaul.GetMembersData();

    //assign data to dtgetmembers and also loaded data to contols

     

    i need GetMembersData as below

    public DataTable GetMembersData()
            {
                string sql = "select * from ViewGetPunchEmployee";
                DataTable GetMembers = DataAccess.ExecuteDataTable(sql);
                return GetMembers;
    if (GetMembers.Rows.Count > 0)
                {
    txtId = convert.tostring(GetMembers.Rows[0]["Id"]);
    txtName = convert.tostring(GetMembers.Rows[0]["Name"]);
    txtAddress = convert.tostring(GetMembers.Rows[0]["ADDRESS"]);
    
    etc..........
                }
            }
    
    when calling only write lines below
    it will loaded datatable and assign controls
    annualmember annual=new annualmember();
    Datatable dtgetmembers=new Datatable();
    dtgetmembers = annaul.GetMembersData();
    but problem facing controls not defined on class
    meaning class not have textbox or any controls to assign
    

    Tuesday, April 2, 2019 9:12 AM
  • Hi

    Thanks for the feedback.

    You could try the following code.

    if (dtgetmembers.Rows.Count > 0)
                {
                     txtName.Text = Convert.ToString(dtgetmembers.Rows[0]["Name"]);
                     txtAddress.Text= Convert.ToString(dtgetmembers.Rows[0]["Address"]);
                     txtID.Text = Convert.ToString(dtgetmembers.Rows[0]["ID"]);
    
                }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 2, 2019 9:33 AM
  • no not that what i need 

    i need to assign controls on function GetMembersData() on class Getmemberdata()

    meaning i need to assign control on function getmemberdata

    add block if(datatable.rows.count>0

    {

    //assign controls

    }


    Tuesday, April 2, 2019 10:17 AM
  • Hi

    Thanks for the feedback.

    Do you want to add controls when load the table details? If yes, you could try the following code. If no, please feel free to tell me.

      if (dtgetmembers.Rows.Count > 0)
                {
                    TextBox txtname = new TextBox();
                    txtname.Location = new Point(25, 25);
                    this.Controls.Add(txtname);
                    TextBox txtaddress = new TextBox();
                    txtaddress.Location = new Point(25, 50);
                    this.Controls.Add(txtname);
                    TextBox txtID = new TextBox();
                    txtID.Location = new Point(25, 75);
                    this.Controls.Add(txtname);
                    this.Controls.Add(txtaddress);
                    this.Controls.Add(txtID);
                    txtname.Text = Convert.ToString(dtgetmembers.Rows[0]["Name"]);
                    txtaddress.Text = Convert.ToString(dtgetmembers.Rows[0]["Address"]);
                    txtID.Text = Convert.ToString(dtgetmembers.Rows[0]["ID"]);
    
                }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 3, 2019 5:55 AM