Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is my c sharp code and i want to count the village members and show the value in label or panal

how i can

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Pirwal_Khel.Models.VillageMembersModel;
using Pirwal_Khel.Templets;
using Pirwal_Khel.Utilities;
using Pirwal_Khel.Utilities.Lists;
using PKDBFrameWork;
using PKDBFrameWork.Windows;

namespace Pirwal_Khel
{

    public enum EnrollmentStatus
    {
        Enrolled = 1,
        Unerolled = 2,
    }

    public enum Gender
    {
        Male = 1,
        Female = 2,
    }
    public partial class AddNewVillageMemberForm : TamplateForm
    {
        public AddNewVillageMemberForm()
        {
            InitializeComponent();
        }

        public int VillMemberId { get; set; }
        private void AddNewVillageMemberForm_Load(object sender, EventArgs e)
        {
            LoadDataintoComboBox();
            LoadDataandBindWithConrolIfUpdated();
        }

        private void LoadDataintoComboBox()
        {
            ListData.LoadDataIntoComboBox(GuradianRelationshipComboBox, "usp_GetGurdianRelationshipComboBox");
            ListData.LoadDataIntoComboBox(BankComboBox, "usp_GetBanksComboBox");
            ListData.LoadDataIntoComboBox(VillageComboBox, "usp_GetVillageComboBox");
            ListData.LoadDataIntoComboBox(VillageAdminComboBox, "usp_GetVillageAdminComboBox");
            ListData.LoadDataIntoComboBox(VillageOccupationComboBox, "usp_GetVillageOccupationComboBox");
            ListData.LoadDataIntoComboBox(MaritalStatusComboBox, "usp_GetValuesforMaritalStatusComboBox");
            ListData.LoadDataIntoComboBox(LivingStatusComboBox, "usp_GetValuesForLivingStatusComboBox");
        }

        private void LoadDataandBindWithConrolIfUpdated()
        {
            if (this.IsUpdate)
            {
                DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());

                DataTable dtVillageMember = db.GetDataList("usp_GetVillageMembersbyVillageMemberId", new DBParameters
                {
                    parameters = "@VillageMemberid",
                    value = this.VillMemberId
                });

                DataRow row = dtVillageMember.Rows[0];

                FamilyNumberTextBox.Text = row["Family_Number"].ToString();
                MemberNameTextBox.Text = row["Name"].ToString();
                MemberGuardianNameTextBox.Text = row["G_Name"].ToString();
                GuradianRelationshipComboBox.SelectedValue = row["Relationship_Id"];
                MemberCNICTextBox.Text = row["CNIC"].ToString();
                MemberGuardianCNICTextBox.Text = row["G_CNIC"].ToString();
                DateOfBirthDateTimePicker.Text = row["Date_Of_Birth"].ToString();
                MemberMobileNumberTextBox.Text = row["Contact"].ToString();
                MaleRadioButton.Checked = (Convert.ToInt32(row["Gender_Id"]) == 1) ? true : false;
                FemaleRadioButton.Checked = (Convert.ToInt32(row["Gender_Id"]) == 2) ? true : false;
                MaritalStatusComboBox.SelectedValue = row["Marital_Status_Id"];
                GuradianAccountNoTextBox.Text = row["Account_No"].ToString();
                BankComboBox.SelectedValue = row["Bank_Id"];                
                VillageComboBox.SelectedValue = row["Vill_Id"];          
                VillageAdminComboBox.SelectedValue = row["Vill_Admin_Id"];
                LivingStatusComboBox.SelectedValue = row["Living_Status_Id"];
                VillageOccupationComboBox.SelectedValue = row["Vill_Ocup_id"];
                EnrolledRadioButton.Checked = (Convert.ToInt32(row["Active_Status_Id"]) == 1) ? true : false;
                UnEnrolledRadioButton.Checked = (Convert.ToInt32(row["Active_Status_Id"]) == 2) ? true : false;
                
                

            }
        }

        private void UnMarriedRadioButton_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (IsFormValidate())
            {
                if (this.IsUpdate)
                {
                    UpdatRecord();
                    PKMessageBox.ShowSuccessMessage("ترمیم شدہ ریکاڈ کامیابی کیساتھ محفوظ ہوگیا");
                }
                else
                {
                    SaveRecord();
                    PKMessageBox.ShowSuccessMessage("نیا ریکاڈ کامیابی کیساتھ محفوظ ہوگیا");
                }

                this.Close();
            }
        }

        private void UpdatRecord()
        {
            DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());
            db.SaveOrUpdateRecord("usp_UpdateVillageMemberTableRecord", GetObject());
        }

        private void SaveRecord()
        {
            DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());
            db.SaveOrUpdateRecord("usp_InsertIntoVillageMemberTable", GetObject());
        }

        private VillageMembersModel GetObject()
        {
            
            

            VillageMembersModel villmember = new VillageMembersModel();
            villmember.Vill_Mem_Id = (this.IsUpdate) ? this.VillMemberId : 0;
            villmember.Name = MemberNameTextBox.Text;
            villmember.G_Name = MemberGuardianNameTextBox.Text;
            villmember.CNIC = string.IsNullOrEmpty(MemberCNICTextBox.Text)?MemberCNICTextBox.Text:null ;
            villmember.G_CNIC = MemberGuardianCNICTextBox.Text;
            villmember.Family_Number = Convert.ToInt32(FamilyNumberTextBox.Text);
            villmember.Vill_Id = Convert.ToInt32(VillageComboBox.SelectedValue);
            villmember.Vill_Admin_Id = Convert.ToInt32(VillageAdminComboBox.SelectedValue);
            villmember.Relationship_Id = Convert.ToInt32(GuradianRelationshipComboBox.SelectedValue);
            villmember.Bank_Id = Convert.ToInt32((BankComboBox.SelectedValue == null) ? null : BankComboBox.SelectedValue);
            villmember.Date_Of_Birth = DateOfBirthDateTimePicker.Value.Date;
            villmember.Contact = MemberMobileNumberTextBox.Text;
            villmember.Account_No = string.IsNullOrEmpty(GuradianAccountNoTextBox.Text)?GuradianAccountNoTextBox.Text:null;
            villmember.Gender_Id = GetGenderRadioButton();
            villmember.Active_Status_Id = GetEnrollement();
            villmember.Vill_Ocup_id = Convert.ToInt32(VillageOccupationComboBox.SelectedValue);
            villmember.Marital_Status_Id = Convert.ToInt32(MaritalStatusComboBox.SelectedValue);
            villmember.Living_Status_Id = Convert.ToInt32(LivingStatusComboBox.SelectedValue);

            DateTime startdate = DateOfBirthDateTimePicker.Value;
            DateTime enddate = DateTime.Now;
            villmember.Age = CalculateAge(startdate,enddate).ToString("0");
            return villmember;

           
        }

        private int GetEnrollement()
        {
            if (EnrolledRadioButton.Checked)
            {
                return (int)EnrollmentStatus.Enrolled;
            }
            else
            {
                return (int)EnrollmentStatus.Unerolled;
            }
        }

        private int GetGenderRadioButton()
        {
            if (MaleRadioButton.Checked)
            {
                return (int)Gender.Male;
            }
            else
            {
                return (int)Gender.Female;
            }
        }


        private bool IsFormValidate()
        {
            if (MemberNameTextBox.Text.Trim() == string.Empty)
            {
                PKMessageBox.ShowErrorMessage("ممبر نام درکار ہے");
                MemberNameTextBox.Focus();
                return false;
            }
            return true;
        }


        private double CalculateAge(DateTime dbDob,DateTime PreDate)
        {
            double age = 0;
            DateTime DOB = dbDob;
            DateTime PresentDate = PreDate;
            TimeSpan TSpan = PresentDate - DOB;
            double days = TSpan.TotalDays;
            age = (days / 365);
            return age;
        } 
    }
}


What I have tried:

this is my c sharp code and i want to count the village members and show the value in label or panal

how i can

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Pirwal_Khel.Models.VillageMembersModel;
using Pirwal_Khel.Templets;
using Pirwal_Khel.Utilities;
using Pirwal_Khel.Utilities.Lists;
using PKDBFrameWork;
using PKDBFrameWork.Windows;

namespace Pirwal_Khel
{

    public enum EnrollmentStatus
    {
        Enrolled = 1,
        Unerolled = 2,
    }

    public enum Gender
    {
        Male = 1,
        Female = 2,
    }
    public partial class AddNewVillageMemberForm : TamplateForm
    {
        public AddNewVillageMemberForm()
        {
            InitializeComponent();
        }

        public int VillMemberId { get; set; }
        private void AddNewVillageMemberForm_Load(object sender, EventArgs e)
        {
            LoadDataintoComboBox();
            LoadDataandBindWithConrolIfUpdated();
        }

        private void LoadDataintoComboBox()
        {
            ListData.LoadDataIntoComboBox(GuradianRelationshipComboBox, "usp_GetGurdianRelationshipComboBox");
            ListData.LoadDataIntoComboBox(BankComboBox, "usp_GetBanksComboBox");
            ListData.LoadDataIntoComboBox(VillageComboBox, "usp_GetVillageComboBox");
            ListData.LoadDataIntoComboBox(VillageAdminComboBox, "usp_GetVillageAdminComboBox");
            ListData.LoadDataIntoComboBox(VillageOccupationComboBox, "usp_GetVillageOccupationComboBox");
            ListData.LoadDataIntoComboBox(MaritalStatusComboBox, "usp_GetValuesforMaritalStatusComboBox");
            ListData.LoadDataIntoComboBox(LivingStatusComboBox, "usp_GetValuesForLivingStatusComboBox");
        }

        private void LoadDataandBindWithConrolIfUpdated()
        {
            if (this.IsUpdate)
            {
                DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());

                DataTable dtVillageMember = db.GetDataList("usp_GetVillageMembersbyVillageMemberId", new DBParameters
                {
                    parameters = "@VillageMemberid",
                    value = this.VillMemberId
                });

                DataRow row = dtVillageMember.Rows[0];

                FamilyNumberTextBox.Text = row["Family_Number"].ToString();
                MemberNameTextBox.Text = row["Name"].ToString();
                MemberGuardianNameTextBox.Text = row["G_Name"].ToString();
                GuradianRelationshipComboBox.SelectedValue = row["Relationship_Id"];
                MemberCNICTextBox.Text = row["CNIC"].ToString();
                MemberGuardianCNICTextBox.Text = row["G_CNIC"].ToString();
                DateOfBirthDateTimePicker.Text = row["Date_Of_Birth"].ToString();
                MemberMobileNumberTextBox.Text = row["Contact"].ToString();
                MaleRadioButton.Checked = (Convert.ToInt32(row["Gender_Id"]) == 1) ? true : false;
                FemaleRadioButton.Checked = (Convert.ToInt32(row["Gender_Id"]) == 2) ? true : false;
                MaritalStatusComboBox.SelectedValue = row["Marital_Status_Id"];
                GuradianAccountNoTextBox.Text = row["Account_No"].ToString();
                BankComboBox.SelectedValue = row["Bank_Id"];                
                VillageComboBox.SelectedValue = row["Vill_Id"];          
                VillageAdminComboBox.SelectedValue = row["Vill_Admin_Id"];
                LivingStatusComboBox.SelectedValue = row["Living_Status_Id"];
                VillageOccupationComboBox.SelectedValue = row["Vill_Ocup_id"];
                EnrolledRadioButton.Checked = (Convert.ToInt32(row["Active_Status_Id"]) == 1) ? true : false;
                UnEnrolledRadioButton.Checked = (Convert.ToInt32(row["Active_Status_Id"]) == 2) ? true : false;
                
                

            }
        }

        private void UnMarriedRadioButton_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (IsFormValidate())
            {
                if (this.IsUpdate)
                {
                    UpdatRecord();
                    PKMessageBox.ShowSuccessMessage("ترمیم شدہ ریکاڈ کامیابی کیساتھ محفوظ ہوگیا");
                }
                else
                {
                    SaveRecord();
                    PKMessageBox.ShowSuccessMessage("نیا ریکاڈ کامیابی کیساتھ محفوظ ہوگیا");
                }

                this.Close();
            }
        }

        private void UpdatRecord()
        {
            DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());
            db.SaveOrUpdateRecord("usp_UpdateVillageMemberTableRecord", GetObject());
        }

        private void SaveRecord()
        {
            DBSQLServer db = new DBSQLServer(AppSetting.ConnectionString());
            db.SaveOrUpdateRecord("usp_InsertIntoVillageMemberTable", GetObject());
        }

        private VillageMembersModel GetObject()
        {
            
            

            VillageMembersModel villmember = new VillageMembersModel();
            villmember.Vill_Mem_Id = (this.IsUpdate) ? this.VillMemberId : 0;
            villmember.Name = MemberNameTextBox.Text;
            villmember.G_Name = MemberGuardianNameTextBox.Text;
            villmember.CNIC = string.IsNullOrEmpty(MemberCNICTextBox.Text)?MemberCNICTextBox.Text:null ;
            villmember.G_CNIC = MemberGuardianCNICTextBox.Text;
            villmember.Family_Number = Convert.ToInt32(FamilyNumberTextBox.Text);
            villmember.Vill_Id = Convert.ToInt32(VillageComboBox.SelectedValue);
            villmember.Vill_Admin_Id = Convert.ToInt32(VillageAdminComboBox.SelectedValue);
            villmember.Relationship_Id = Convert.ToInt32(GuradianRelationshipComboBox.SelectedValue);
            villmember.Bank_Id = Convert.ToInt32((BankComboBox.SelectedValue == null) ? null : BankComboBox.SelectedValue);
            villmember.Date_Of_Birth = DateOfBirthDateTimePicker.Value.Date;
            villmember.Contact = MemberMobileNumberTextBox.Text;
            villmember.Account_No = string.IsNullOrEmpty(GuradianAccountNoTextBox.Text)?GuradianAccountNoTextBox.Text:null;
            villmember.Gender_Id = GetGenderRadioButton();
            villmember.Active_Status_Id = GetEnrollement();
            villmember.Vill_Ocup_id = Convert.ToInt32(VillageOccupationComboBox.SelectedValue);
            villmember.Marital_Status_Id = Convert.ToInt32(MaritalStatusComboBox.SelectedValue);
            villmember.Living_Status_Id = Convert.ToInt32(LivingStatusComboBox.SelectedValue);

            DateTime startdate = DateOfBirthDateTimePicker.Value;
            DateTime enddate = DateTime.Now;
            villmember.Age = CalculateAge(startdate,enddate).ToString("0");
            return villmember;

           
        }

        private int GetEnrollement()
        {
            if (EnrolledRadioButton.Checked)
            {
                return (int)EnrollmentStatus.Enrolled;
            }
            else
            {
                return (int)EnrollmentStatus.Unerolled;
            }
        }

        private int GetGenderRadioButton()
        {
            if (MaleRadioButton.Checked)
            {
                return (int)Gender.Male;
            }
            else
            {
                return (int)Gender.Female;
            }
        }


        private bool IsFormValidate()
        {
            if (MemberNameTextBox.Text.Trim() == string.Empty)
            {
                PKMessageBox.ShowErrorMessage("ممبر نام درکار ہے");
                MemberNameTextBox.Focus();
                return false;
            }
            return true;
        }


        private double CalculateAge(DateTime dbDob,DateTime PreDate)
        {
            double age = 0;
            DateTime DOB = dbDob;
            DateTime PresentDate = PreDate;
            TimeSpan TSpan = PresentDate - DOB;
            double days = TSpan.TotalDays;
            age = (days / 365);
            return age;
        } 
    }
}
Posted
Updated 26-Jul-20 23:52pm
v2
Comments
OriginalGriff 27-Jul-20 3:16am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Patrice T 27-Jul-20 3:17am    
And you have a question ?
Richard MacCutchan 27-Jul-20 3:40am    
Why have you posted the same code in both places above?
Otekpo Emmanuel 27-Jul-20 3:45am    
You can use SQL Count statement.
SELECT COUNT(*) FROM tableName;
Maciej Los 27-Jul-20 5:31am    
As to the method of [CalculateAge], i'd strongly recommend to read this: Working with Age: it's not the same as a TimeSpan![^]

1 solution

My best guess is:

C#
DataTable dtVillageMember = db.GetDataList("your_usp_to_grab_village_members", new DBParameters(...));
//display the count of village members
label1.Text = dtVillageMember.Rows.Count(); //returns the number of rows in a datatable object


For further details, please see:DataTable.Rows Property (System.Data) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900