Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

how to bind selected item or value in combobox to existing form in c# windows forms with sql server 2008

$
0
0

hi my name is vishal

i was wondering on how to bind selected item in combobox and then click on search button in my form named:frmDialyzer to existing form?

i have form named:frmDialyzer in that i have 1combobox,2buttons

combobox1 name:cboPatientID,enabled:true and visible:true

button1 name:btnSearch,text:Search Patient,enabled:true andvisible:true

button2 name:btnAssign,text:Assign to Patient,enabled:true andvisible:true

given below is code for btnSearch in c# windows form with sql server 2008:

private void btnSearch_Click(object sender, EventArgs e)
        {
            string dPatientID;
            dPatientID = Convert.ToString(cboPatientID.SelectedIndex);
            frmPatient p = new frmPatient();
            p.loadPatient(dPatientID);
            p.Show();
        }

when i execute the above code in c# i get only blank form of form named:frmPatient and i dont get datas binded to respective textboxes,comboboxes and datetimepickers.

 As you can see I populate my combobox named:cboPatientID through following below code in c# windows forms with sql server 2008:

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;
namespace DRRS_CSharp
{
    public partial class frmDialyzer : Form
    {
        int dStepIndex;
        int pUserID;
        public frmDialyzer()
        {
            InitializeComponent();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            string PatientPull = ("Select p.patient_id as patient_id,n.patient_first_name as patient_fname,n.patient_last_name as patient_lname from patient_name n,patient_id p where n.patient_id=p.patient_id and n.status=1 and p.patient_id not in (Select patient_id from dialyser3 where deleted_status=0)");
            SqlCommand cmd = new SqlCommand(PatientPull);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                PatientPull = dr[0].ToString() + dr[1].ToString() + dr[2].ToString();
                cboPatientID.Items.Add(PatientPull);
            }
            dr.Close();
}

Given below is code of loadPatient method/function which is coded in form named:frmPatient:

public void loadPatient(string mPatientID)
        {
            btnCreate.Text = "SAVE";
            SqlConnection conn = new SqlConnection(conString);
            string SelectString=("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,h.homenumber as homenumber,h.mobilenumber as mobilenumber,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,o.doctor_first_name+' '+o.doctor_last_name+' '+o.doctor_middle_name as doctor_name from patient_id p,patient_name n,patient_contact h,address a,doctordetail o where n.patient_id=p.patient_id and a.patient_id=p.patient_id and p.patient_id=h.patient_id and p.patient_id=o.doctor_id and p.patient_id=@patientId");
            SqlDataAdapter adp = new SqlDataAdapter(SelectString,conn);
            adp.SelectCommand.Parameters.Add("@patientId", SqlDbType.VarChar, 50);
            adp.SelectCommand.Parameters["@patientId"].Value = mPatientID;
            dataset = new DataSet();
            adp.Fill(dataset, "patient_id");
            adp.Fill(dataset, "patient_name");
            adp.Fill(dataset, "patient_contact");
            adp.Fill(dataset, "address");
            adp.Fill(dataset, "doctordetail");
            txtFName.DataBindings.Add("Text", dataset, "patient_name.patient_fname");
            txtMName.DataBindings.Add("Text", dataset, "patient_name.patient_mname");
            txtLName.DataBindings.Add("Text", dataset, "patient_name.patient_lname");
            dtDOB.DataBindings.Add("Text", dataset, "patient_id.patient_dob");
            cboSex.DataBindings.Add("Text", dataset, "patient_id.patient_sex");
            cboVirology.DataBindings.Add("Text", dataset, "patient_name.virology");
            txtHNumber.DataBindings.Add("Text", dataset, "patient_contact.homenumber");
            txtMNumber.DataBindings.Add("Text", dataset, "patient_contact.mobilenumber");
            txtApartmentNo.DataBindings.Add("Text", dataset, "address.apartment_number");
            txtApartmentName.DataBindings.Add("Text", dataset, "address.apartment_name");
            txtDoorNo.DataBindings.Add("Text", dataset, "address.door_number");
            txtStreet1.DataBindings.Add("Text", dataset, "address.street_name_1");
            txtStreet2.DataBindings.Add("Text", dataset, "address.Street_name_2");
            txtStreet3.DataBindings.Add("Text", dataset, "address.Street_name_3");
            txtVillageArea.DataBindings.Add("Text", dataset, "address.village");
            txtCity.DataBindings.Add("Text", dataset, "address.city");
            txtState.DataBindings.Add("Text", dataset, "address.state");
            txtPCode.DataBindings.Add("Text", dataset, "address.pincode");
            txtCountry.DataBindings.Add("Text", dataset, "address.country");
            cboDoctor.DataBindings.Add("Text", dataset, "doctordetail.doctor_name");
        }

What i want is after user selects a patient from combobox named:cboPatientID in formnamed:frmDialyzer and when he clicks btnSearch in formnamed:frmDialyzer it should show or go to form named:frmPatient with their respective datas with the help of functionloadPatient which is coded in form:frmPatient.

i know i have to do some modifications in my btnSearch in formnamed:frmDialyzer but i dont know how to do it.Can anyone help me please?any help/guidance in solving this problem would be greatly appreciated.

I hope i get a reply.



vishal


Viewing all articles
Browse latest Browse all 2535

Trending Articles



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