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
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:
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:
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:
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:
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.!