本文介绍了如何使用s#server2008在c#windows窗体中使用现有窗体中的方法/函数将选定的组合框项目绑定到现有窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好我的名字是过去10天的固定,我一直在打破如何将一个组合框的选定项目从一个表单绑定到c#server2008的c#windows表单中的现有表单?下面是我的c#代码形式名称: frmDialyzer 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();} 其中 cboPatientID 是 frmDialyzer 中我的组合框的名称,带有DropDownStyle属性: DropDownList 以下是 frmDialyzer 中搜索病人的c#代码: where cboPatientID is name of my of my combobox in frmDialyzer with DropDownStyle property:DropDownListGiven 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(); } 我尝试在所选项目/值组合框: cboPatientID > frmDialzyer 使用 loadPatient(string mPatientID)命名为 frmPatient 的现有表格,该表格在 frmPatient 中声明并初始化。 以下是 frmPatient中的函数 loadPatient(字符串mPatientID)的c#代码: 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"); } 当我在我的frmDialyzer中尝试以下代码时: 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(); } 执行上面的代码后,我只得到空白形式的frmPatient。如果我把 dPatientID = Convert.ToString(cboPatientID.SelectedItem); i得到错误告诉 SqlException未处理: 将varchar值'system.data.datarowview'转换为数据类型int。时转换失败,它指向 frmPatient loadPatient 函数中的下面一行: Upon executing the above code i get only blank form of frmPatient. If i putdPatientID=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"); 其中patient_id是我在sql server2008中的一个表。 有人可以帮帮我吗?任何帮助/指导解决这个问题将不胜感激!为此,我总是可以在 frmPatient 中创建另一个类似 loadPatient 函数的函数。但请帮助/指导我在函数中需要进行哪些修改 loadPatient( )在 frmPatient 。 如果解决此问题需要任何其他细节请询问/告诉。!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.!推荐答案 在你的函数loadPatient()中,我没有看到con.open行。你确定它是开放的吗?In your function loadPatient() I don't see a con.open line. Are you sure it's open? 这篇关于如何使用s#server2008在c#windows窗体中使用现有窗体中的方法/函数将选定的组合框项目绑定到现有窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 05:24