Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

calling a method or function declared in mdi parent form in a child form?

$
0
0

Hi my name is vishal from India(Chennai).

i am an amateur in c# with windows forms. i am in process of converting vb6 with ms access project to c# with sql server 2008. i do find at times difficult to transform vb6 code to c#.

The problem i am facing is i do not know how to call a method/function declared in mdi parent form in child form? i  have browsed through net but so far with no success. Anyone please help me.

My mdi parent form name: MDIParent1.cs

my child form name: Patient.cs

my mdi parent form method/function: public string updateUserActivities()

can anyone please help help me how to call this method/function in my child form?

My MdiParent1.cs code:

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 System.Data.SqlClient;
using System.Configuration;

namespace DRRS_in_Csharp
{
    public partial class MDIParent1 : Form
    {
        private int childFormNumber = 0;
        long pUserID;
        public MDIParent1()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form();
            childForm.MdiParent = this;
            childForm.Text = "Window " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.IsMdiContainer = true;
            Form Patient = new Form();
            Patient.MdiParent = this;
            Patient.Show();
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }
        public SqlConnectionStringBuilder connBuilder;
        public SqlConnection conn;

        private void initializeDB()
        {

            connBuilder = new SqlConnectionStringBuilder();
            connBuilder.InitialCatalog = "TusDB";
            connBuilder.DataSource = "NPD-4\\SQLEXPRESS";
            connBuilder.IntegratedSecurity = true;
            connBuilder.AsynchronousProcessing = true;

            conn = new SqlConnection(connBuilder.ToString());
            conn.Open();
        }

         public string updateUserActivities(long refID, int ActType, string ActivityStr)
        {
      
            SqlDataAdapter da = new SqlDataAdapter();
            int b;
            da.InsertCommand = new SqlCommand("Insert into user_activities2(user_id,row_upd_date,activity,type,ref_id)" + "Values(@user_id,@row_upd_date,@activity,@type,@ref_id)", conn);
            da.InsertCommand.Parameters.Add("@user_id", SqlDbType.VarChar).Value = pUserID;
            da.InsertCommand.Parameters.Add("@row_upd_date", SqlDbType.DateTime).Value = Convert.ToDateTime(this);
            da.InsertCommand.Parameters.Add("@activity", SqlDbType.VarChar).Value = ActivityStr;
            da.InsertCommand.Parameters.Add("@type", SqlDbType.Int).Value = ActType;
            da.InsertCommand.Parameters.Add("@ref_id", SqlDbType.VarChar).Value = refID;
            b = da.InsertCommand.ExecuteNonQuery();
            conn.Close();

        }
        private void MDIParent1_Load(object sender, EventArgs e)
        {
            initializeDB();
        }

        private void patientToolStripMenuItem_Click(object sender, EventArgs e)
        {
           

        }
    }
}



Patient.cs code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;



namespace DRRS_in_Csharp
{
    
    public partial class Patient : Form
    {
        public Patient()
        {
            InitializeComponent();
        }

        private void Patient_Load(object sender, EventArgs e)
        {
           
            SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=TusDB;Persist Security Info=true");
            SqlDataAdapter da = new SqlDataAdapter();
            dtDOB.MaxDate=Convert.ToDateTime(this);
            dtDOB.Value=Convert.ToDateTime(this);
            DataTable dt=new DataTable();
            cboSex.ValueMember="patient_sex";
            cboSex.DisplayMember="Sex";
            cboVirology.ValueMember="Virus type";
            cboVirology.DisplayMember="Virus type";
            LoadDoctorID();

        }
        public string LoadDoctorID()
        {
           
            String LDoctID;
             String dMstr;
            DataTable dt=new DataTable();
            SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Tuscan7;Persist Security Info=true");
            SqlDataAdapter da=new SqlDataAdapter();
            conn.Open();
            da.SelectCommand=new SqlCommand("Select p.doctor_id as doctor_id,n.doctor_first_name as doctor_fname,n.doctor_middle_name as doctor_mname,n.doctor_last_name as doctor_lname from doctordetail n,doctor p where n.doctor_id=p.doctor_id and n.status=true",conn);
            dMstr="";
            LDoctID="";
            cboDoctor.Items.Clear();
            while(dt.Rows.Count!=-1)
            da.InsertCommand.Parameters.Add("doctor_id",SqlDbType.VarChar).Value=LDoctID;
                   LDoctID=SqlCommand("Select replace(6-length(LDoctID))+LDoctID");
            if (da.InsertCommand.Parameters.Add("doctor_fname",SqlDbType.VarChar).Value==null)
            da.InsertCommand.Parameters.Add("doctor_fname",SqlDbType.VarChar).Value=dMstr;
            if (da.InsertCommand.Parameters.Add("doctor_mname",SqlDbType.VarChar).Value==null)
                dMstr=dMstr+ da.InsertCommand.Parameters.Add("doctor_mname",SqlDbType.VarChar).Value;
            if (da.InsertCommand.Parameters.Add("doctor_lname",SqlDbType.VarChar).Value==null)
                dMstr=dMstr+da.InsertCommand.Parameters.Add("doctor_lname",SqlDbType.VarChar).Value;
            cboDoctor.Items.Add(dMstr);
            cboDoctor.Items.Add(LDoctID);
            da.InsertCommand.Parameters.Add("doctor_id",SqlDbType.VarChar).Value=cboDoctor.Items.Add(cboDoctor.SelectedIndex);
            da.InsertCommand.ExecuteNonQuery();
        }

        public string loadPatient(string mpatientID)
        {
            patientID = mpatientID;
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Tuscan7;Persist Security Info=true");
            SqlDataAdapter da = new SqlDataAdapter();
            conn.Open();
            da.SelectCommand = new SqlCommand("Select p.patient_id as patient_id,p.patient_dOB as patient_dOB,n.patient_first_name as patient_fname,n.patient_middle_name as patient_mname,n.patient_last_name as patient_lname,p.patient_sex as patient_sex,n.virology as virology,a.apartment_name as apartment_name,a.door_number as door_number,a.street_name_1 as street_name_1,a.street_name_2 as street_name_2,a.street_name_3 as street_name_3,a.village as village,a.city as city,a.state as state,a.country as country,a.apartment_number as apartment_number,a.pincode as pincode from Patient_name n,Patient_id p,address2 a where n.patient_id=p.patient_id and a.patient_id=p.patient_id and p.patient_id="+patientID+" and n.status=true and a.status=true");
            if (da.SelectCommand != null)
                conn.Open();
                object selecteditem=cboVirology.SelectedIndex;
            int x;
            da.InsertCommand=new SqlCommand("Insert into Patient_name(patient_first_name,patient_middle_name,patient_last_name,virology)"+"Values(@patient_first_name,@patient_middle_name,@patient_last_name,@virology)",conn);
            while (dt.Rows.Count != -1)
                txtFname.Text = da.InsertCommand.Parameters.Add("@patient_first_name", SqlDbType.VarChar).Value.ToString();
                txtMname.Text=da.InsertCommand.Parameters.Add("@patient_middle_name",SqlDbType.VarChar).Value.ToString();
                txtLname.Text = da.InsertCommand.Parameters.Add("@patient_last_name", SqlDbType.VarChar).Value.ToString(); ;
                selecteditem=da.InsertCommand.Parameters.Add("@virology",SqlDbType.Int).Value.GetType();
                object selecteditema = cboSex.SelectedIndex;
                int y;
                da.InsertCommand = new SqlCommand("Insert into Patient_id(patient_dOB,patient_sex)" + "Values(@patient_dOB,@patient_sex)", conn);
                dtDOB.Value=da.InsertCommand.Parameters.Add("@patient_dOB", SqlDbType.DateTime).Value.GetType();
                selecteditema = da.InsertCommand.Parameters.Add("@patient_sex", SqlDbType.Int).Value.GetType();
                int z;
                da.InsertCommand = new SqlCommand("Insert into address2(apartment_name,door_number,street_name_1,street_name_2,street_name_3,village,city,state,country,apartment_number,pincode)" + "Values(@apartment_name,@door_number,@street_name_1,@street_name_2,@street_name_3,@village,@city,@state,@country,@apartment_number,@pincode)", conn);
                txtApartmentName.Text = da.InsertCommand.Parameters.Add("@apartment_name", SqlDbType.NVarChar).Value.ToString();
                txtDoorNo.Text = da.InsertCommand.Parameters.Add("@door_number", SqlDbType.VarChar).Value.ToString();
                txtStreet1.Text = da.InsertCommand.Parameters.Add("@street_name_1", SqlDbType.VarChar).Value.ToString();
                txtStreet2.Text = da.InsertCommand.Parameters.Add("@street_name_2", SqlDbType.VarChar).Value.ToString();
                txtStreet3.Text = da.InsertCommand.Parameters.Add("@street_name_3", SqlDbType.VarChar).Value.ToString();
                txtVillageArea.Text = da.InsertCommand.Parameters.Add("@village", SqlDbType.VarChar).Value.ToString();
                txtCity.Text = da.InsertCommand.Parameters.Add("@city", SqlDbType.VarChar).Value.ToString();
                txtState.Text = da.InsertCommand.Parameters.Add("@state", SqlDbType.VarChar).Value.ToString();
                txtCountry.Text = da.InsertCommand.Parameters.Add("@country", SqlDbType.VarChar).Value.ToString();
                txtApartmentNo.Text = da.InsertCommand.Parameters.Add("@apartment_number", SqlDbType.VarChar).Value.ToString();
                cboSex.Enabled = false;
                dtDOB.Enabled = false;
                txtPostalCode.Text = da.InsertCommand.Parameters.Add("@pincode", SqlDbType.VarChar).Value.ToString();
                da.SelectCommand = new SqlCommand("Select * from Patient_contact where patient_id=" + patientID + " and status=true");
                da.InsertCommand = new SqlCommand("Insert into Patient_contact(homenumber,mobilenumber)" + "Values(@homenumber,@mobilenumber)", conn);
                while (dt.Rows.Count != -1)
                    txtHnumber.Text = da.InsertCommand.Parameters.Add("@homenumber", SqlDbType.NVarChar).Value.ToString();
                txtMnumber.Text = da.InsertCommand.Parameters.Add("@mobilenumber", SqlDbType.NVarChar).Value.ToString();
                conn.Close();
                int b;
                long i;
                da.InsertCommand = new SqlCommand("Insert into doctorpatient2(doctor_id)" + "Values(@doctor_id)", conn);
            da.SelectCommand=new SqlCommand("Select * from doctorpatient2 where patient_id="+patientID+" and status=true");
            while (dt.Rows.Count != -1)
                for (i = 0; i < cboDoctor.Items.Count - 1; i++)
                    if (cboDoctor.Items.Add(i)==da.InsertCommand.Parameters.Add("@doctor_id", SqlDbType.VarChar).Value.ToString())
                        cboDoctor.SelectedItem = i;

        }
       

        private string SqlCommand(string p)
{
 	throw new NotImplementedException();
}



        private void button1_Click(object sender, EventArgs e)
        {   
            long PatientID;
            string dFieldName = "";
            if (txtFname.Text.ToString().Trim() == "")
            {
                dFieldName = "First Name should not be empty";
            }
            else if (cboSex.Text.ToString().Trim() == "")
            {
                dFieldName = "Sex should not be empty";
            }
            else if (cboVirology.Text.ToString().Trim() == "")
            {
                dFieldName = "Virology state of patient should not be empty";
            }
            else if (txtHnumber.Text.ToString().Trim() == "")
            {
                dFieldName = "Home number of patient should not be empty";
            }
            else if (txtCity.Text.ToString().Trim() == "")
            {
                dFieldName = "City of patient should not be empty";
            }
            else if (txtCountry.Text.ToString().Trim() == "")
            {
                dFieldName = "Country of patient should not be empty";
            }
            else if (cboDoctor.Text.ToString().Trim() == "")
            {
                dFieldName = "Doctor Name should not be empty";
            }
            if (dFieldName.ToString().Trim() != "")
            {
                MessageBox.Show(dFieldName);
            }
           long pUserID;
            long patientID;
            long patient;
            SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=Tuscan7;Persist Security Info=true");
            SqlDataAdapter da=new SqlDataAdapter();
            SqlCommand cmd=new SqlCommand();
            cmd.Connection=conn;
            cmd.CommandType=CommandType.Text;

            conn.Open();
            
            if(patientID==0)
                conn.Open();
            object selecteditem=cboSex.SelectedIndex;
               int x;
            da.InsertCommand=new SqlCommand("Insert into Patient_id(patient_id,patient_sex,patient_dOB,row_upd_date,user_id)" + " Values(@patient_id,@patient_sex,@patient_dOB,@row_upd_date,@user_id)",conn);
            da.InsertCommand.Parameters.Add("@patient_sex",SqlDbType.Int).Value=selecteditem;
            da.InsertCommand.Parameters.Add("@patient_dOB",SqlDbType.DateTime).Value=dtDOB.Value;
            da.InsertCommand.Parameters.Add("@row_upd_date",SqlDbType.DateTime).Value=Convert.ToDateTime(this);
            da.InsertCommand.Parameters.Add("@user_id",SqlDbType.VarChar).Value=pUserID;
            x=da.InsertCommand.ExecuteNonQuery();
            long vPatientID;
            if(patientID==0)
                vPatientID=da.InsertCommand.Parameters.Add("@patient_id",SqlDbType.Int).Value=patient;
            else
                vPatientID=patientID;
            if(patientID>0)
                cmd=new SqlCommand("update Patient_name set status=false where patient_id="+patientID);
            da.InsertCommand=new SqlCommand("Insert into Patient_name(patient_id,patient_first_name,patient_middle_name,patient_last_name,virology,row_upd_date,status,user_id)"+"Values(@patient_id,@patient_first_name,@patient_middle_name,@patient_last_name,@virology,@row_upd_date,@status,@user_id)",conn);
            conn.Open();
            object selecteditema=cboVirology.SelectedIndex;
            da.InsertCommand.Parameters.Add("@patient_id",SqlDbType.Int).Value=vPatientID;
            da.InsertCommand.Parameters.Add("@patient_first_name",SqlDbType.VarChar).Value=txtFname.Text;
            da.InsertCommand.Parameters.Add("@patient_middle_name",SqlDbType.VarChar).Value=txtMname.Text;
            da.InsertCommand.Parameters.Add("@patient_last_name",SqlDbType.VarChar).Value=txtLname.Text;
            da.InsertCommand.Parameters.Add("@virology",SqlDbType.Int).Value=selecteditema;
            da.InsertCommand.Parameters.Add("@row_upd_date",SqlDbType.DateTime).Value=Convert.ToDateTime(this);
            da.InsertCommand.Parameters.Add("@status",SqlDbType.Bit).Value=true;
            da.InsertCommand.Parameters.Add("@user_id",SqlDbType.VarChar).Value=pUserID;
            x=da.InsertCommand.ExecuteNonQuery();
            if((txtHnumber.Text!="")|| (txtMnumber.Text!=""))
                if(patientID>0)
                    cmd=new SqlCommand("update patient_contact set status=false where patient_id="+patientID);
            da.InsertCommand=new SqlCommand("Insert into patient_contact(patient_id,homenumber,mobilenumber,row_upd_date,status,user_id)"+"Values(@patient_id,@homenumber,@mobilenumber,@row_upd_date,@status,@user_id)",conn);
            conn.Open();
            da.InsertCommand.Parameters.Add("@patient_id",SqlDbType.Int).Value=vPatientID;
            da.InsertCommand.Parameters.Add("@homenumber",SqlDbType.NVarChar).Value=txtHnumber.Text;
            da.InsertCommand.Parameters.Add("@mobilenumber",SqlDbType.NVarChar).Value=txtMnumber.Text;
            da.InsertCommand.Parameters.Add("@row_upd_date",SqlDbType.DateTime).Value=Convert.ToDateTime(this);
            da.InsertCommand.Parameters.Add("@status",SqlDbType.Bit).Value=true;
            da.InsertCommand.Parameters.Add("@user_id",SqlDbType.VarChar).Value=pUserID;
            x=da.InsertCommand.ExecuteNonQuery();

            if(patientID>0)
                cmd=new SqlCommand("update Address9 set status=false where patient_id="+patientID);
            da.InsertCommand=new SqlCommand("Insert into Address9(apartment_name,door_number,street_name_1,street_name_2,street_name_3,village,city,state,country,row_upd_date,apartment_number,patient_id,status,user_id,pcode)"+"Values(@apartment_name,@door_number,@street_name_1,@street_name_2,@street_name_3,@village,@city,@state,@country,@row_upd_date,@apartment_number,@patient_id,@status,@user_id,@pcode)",conn);
            da.InsertCommand.Parameters.Add("@apartment_name",SqlDbType.NVarChar).Value=txtApartmentName.Text;
            da.InsertCommand.Parameters.Add("@door_number",SqlDbType.NVarChar).Value=txtDoorNo.Text;
            da.InsertCommand.Parameters.Add("@street_name_1",SqlDbType.VarChar).Value=txtStreet1.Text;
            da.InsertCommand.Parameters.Add("@street_name_2",SqlDbType.VarChar).Value=txtStreet2.Text;
            da.InsertCommand.Parameters.Add("@street_name_3",SqlDbType.VarChar).Value=txtStreet3.Text;
            da.InsertCommand.Parameters.Add("@village",SqlDbType.NVarChar).Value=txtVillageArea.Text;
            da.InsertCommand.Parameters.Add("@city",SqlDbType.NVarChar).Value=txtCity.Text;
            da.InsertCommand.Parameters.Add("@state",SqlDbType.VarChar).Value=txtState.Text;
            da.InsertCommand.Parameters.Add("@country",SqlDbType.NVarChar).Value=txtCountry.Text;
            da.InsertCommand.Parameters.Add("@row_upd_date",SqlDbType.DateTime).Value=Convert.ToDateTime(this);
            da.InsertCommand.Parameters.Add("@apartment_number",SqlDbType.NVarChar).Value=txtApartmentNo.Text;
            da.InsertCommand.Parameters.Add("@patient_id",SqlDbType.Int).Value=vPatientID;
            da.InsertCommand.Parameters.Add("@status",SqlDbType.Bit).Value=true;
            da.InsertCommand.Parameters.Add("@user_id",SqlDbType.VarChar).Value=pUserID;
            da.InsertCommand.Parameters.Add("@pcode",SqlDbType.VarChar).Value=txtPostalCode.Text;
            x=da.InsertCommand.ExecuteNonQuery();

            if(patientID>0)
                cmd=new SqlCommand("update doctorpatient2 set status=false where patient_id="+patientID);
            da.InsertCommand=new SqlCommand("Insert into doctorpatient2(patient_id,doctor_id,row_upd_date,status,user_id)"+"Values(@patient_id,@doctor_id,@row_upd_date,@status,@user_id)",conn);
            da.InsertCommand.Parameters.Add("@patient_id",SqlDbType.Int).Value=vPatientID;
            da.InsertCommand.Parameters.Add("@doctor_id",SqlDbType.Int).Value=cboDoctor.Items.Contains(cboDoctor.SelectedItem);
            da.InsertCommand.Parameters.Add("@status",SqlDbType.Bit).Value=true;
            da.InsertCommand.Parameters.Add("@row_upd_date",SqlDbType.DateTime).Value=Convert.ToDateTime(this);
            da.InsertCommand.Parameters.Add("@user_id",SqlDbType.VarChar).Value=pUserID;
            x=da.InsertCommand.ExecuteNonQuery();

   


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>