Click here to Skip to main content
15,883,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi my name is vishal for past 10days i have been breaking my head on how to bind selected item of a combobox from a form to existing form in c# windows forms with sql server2008? Given below is my c# code of form named:frmDialyzer
C#
 public partial class frmDialyzer : Form
    {
public frmDialyzer()
        {
            InitializeComponent();
            this.FillDropDownList();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
              {
                 conn.Open();
               }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string ManufacturerPull = ("Select distinct(ManufacturerName) from EquipmentData");
            SqlCommand mcd = new SqlCommand(ManufacturerPull);
            mcd.Connection = conn;
            mcd.CommandType = CommandType.Text;
            SqlDataReader cdr = mcd.ExecuteReader();
            while (cdr.Read())
            {
                ManufacturerPull = cdr[0].ToString();
                cboManufacturer.Items.Add(ManufacturerPull);
            }
            cdr.Close();
        }
 public void FillDropDownList()
        {
            string Sql = "Select p.patient_id as patient_id,(n.patient_first_name+' '+n.patient_last_name) as Name from patient_id p,patient_name n where n.patient_id=p.patient_id and n.status=1 and not exists(Select * from dialyser where dialyser.deleted_status=0 and dialyser.closed_status=0 and dialyser.patient_id=p.patient_id)";
            DataTable dt = new DataTable();
            SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(Sql, conn);
            dt.Load(cmd.ExecuteReader());
            cboPatientID.DataSource = dt;
            cboPatientID.ValueMember = "patient_id";
            cboPatientID.DisplayMember = "Name";
            cboPatientID.SelectedValue = 0;
        }
private void btnAssign_Click(object sender, EventArgs e)
        {
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            int autoGenId = -1;
            cmd = new SqlCommand("Insert into dialyser(dialyserID,manufacturer,mfr_ref_number,mfr_lot_number,mfr_date,exp_date,start_date,packed_volume,dialyzer_size,deleted_status,closed_status,patient_id,row_upd_date,user_id)" + "Values(@dialyserID,@manufacturer,@mfr_ref_number,@mfr_lot_number,@mfr_date,@exp_date,@start_date,@packed_volume,@dialyzer_size,@deleted_status,@closed_status,@patient_id,GetDate(),@user_id);Select @autoGenId = SCOPE_IDENTITY();", conn);
            cmd.Parameters.AddWithValue("@dialyserID", string.Format("0000" + txtDID.Text.ToString()));
            cmd.Parameters.AddWithValue("@manufacturer", cboManufacturer.Text.ToString());
            cmd.Parameters.AddWithValue("@mfr_ref_number", txtMFRRefNo.Text.ToString());
            cmd.Parameters.AddWithValue("@mfr_lot_number", txtMFRLotNo.Text.ToString());
            cmd.Parameters.AddWithValue("@mfr_date", dtMFRDate.Value);
            cmd.Parameters.AddWithValue("@exp_date", dtExpDate.Value);
            cmd.Parameters.AddWithValue("@start_date", dtStartDate.Value);
            cmd.Parameters.AddWithValue("@packed_volume", txtPVol.Text.ToString());
            cmd.Parameters.AddWithValue("@dialyzer_size", cboequipmentType.Text.ToString());
            cmd.Parameters.AddWithValue("@deleted_status", 0);
            cmd.Parameters.AddWithValue("@closed_status", 0);
            cmd.Parameters.AddWithValue("@patient_id", cboPatientID.SelectedValue);
cmd.Parameters.AddWithValue("@user_id", pUserID);
            cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
            ((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 4, cboManufacturer.Text.ToString());
 MessageBox.Show("Reprocessing data was successfully added", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
}

where cboPatientID is name of my of my combobox in frmDialyzer with DropDownStyle property:DropDownList

Given below is my c# code for search patient in frmDialyzer:
C#
private void btnSearch_Click(object sender, EventArgs e)
        {
            string dPatientID;
            dPatientID = Convert.ToString(cboPatientID.SelectedValue);
            frmPatient p = new frmPatient();
            p.loadPatient(dPatientID);
            p.Show();
        }

I try to bind my selected item/value of combobox:cboPatientID in frmDialzyer to existing form named:frmPatient using loadPatient(string mPatientID) which is declared and initialized in frmPatient.
Given below is c# code of function loadPatient(string mPatientID) in frmPatient:
C#
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");
       }

When i try the below code in my frmDialyzer:
C#
private void btnSearch_Click(object sender, EventArgs e)
        {
            string dPatientID;
            dPatientID = Convert.ToString(cboPatientID.SelectedValue);
            frmPatient p = new frmPatient();
            p.loadPatient(dPatientID);
            p.Show();
        }

Upon executing the above code i get only blank form of frmPatient. If i put
dPatientID=Convert.ToString(cboPatientID.SelectedItem);
i get error telling SqlException was unhandled:
Conversion failed when converting the varchar value 'system.data.datarowview' to data type int.
which points to line below in loadPatient function in frmPatient:
C#
adp.Fill(dataset, "patient_id");

where "patient_id" is one of my tables in sql server2008.
Can anyone help me please? Any help/guidance in solving of this problem would be greatly appreciated! I can always create another function similar to loadPatient function in frmPatient for this purpose.But please help/guide me what modifications should i need to in my function loadPatient() in frmPatient.
If any other details required for solving of this problem Please ask/tell.!
Posted
Updated 27-May-14 23:32pm
v3
Comments
anuradha.sardesai 28-May-14 5:47am    
after this line
dPatientID = Convert.ToString(cboPatientID.SelectedValue);
are you getting correct value of dPatientID?
anuradha.sardesai 28-May-14 5:50am    
Secondly, check if this query works on the backend directly,

"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"

If you patient_id is string / char field, it will have to be in a pair of quotes
Member 10248768 28-May-14 5:57am    
Yes i checked it and it worked perfectly in my back-end since i was using it to bind selected row of a listview to existing form in c# windows forms with sql server2008. I can tell you i can always create another function like loadPatient() in frmPatient for binding selected item/value of combobox from one form(frmDialyzer) to existing form(frmPatient) in c# windows forms with sql server2008.Just tell/guide me what modifications must i do in that function to get required output.? Reply please?
Member 10248768 28-May-14 5:52am    
Hi thanks for replying to my query on such short notice.
Actually i am only getting blank form of frmPatient(no datas in textboxes,comboboxes and datetimepickers)nothing more i get. Should i need to modify my c# code of loadPatient() function in frmPatient? Reply please?
anuradha.sardesai 28-May-14 5:56am    
If you try to debug, till where are you getting expected results? ANd at the query I mentioned above, add a breakpoint, check the value of SelectString and execute EXACTLY the same query on backend. See if you get any data back.

1 solution

In your function loadPatient() I don't see a con.open line. Are you sure it's open?
 
Share this answer
 
Comments
Member 10248768 29-May-14 2:17am    
You are correct thank you!

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