本文介绍了Combobox值到数据库sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好, 我正在尝试使用3层业务逻辑将组合框值传递给数据库。目前,我的表单页面如下所示:
Hello, I am trying to pass combobox value to a database, using a 3 tier business logic. Currently my form page looks like this:
private void Savebtn_Click(object sender, EventArgs e) { try { SaveRole(tbroledesc.Text, /*combobox selected item gets passed here*/); MessageBox.Show("Club role saved successfully"); } catch (Exception er) { MessageBox.Show(er.Message.ToString()); } } public void SaveRole(string RoleDesc, /*combobox selecteditem*/) { ClubRoles cr = new ClubRoles(); RoleCreatedBy = Convert.ToInt32(lstcreatedby.SelectedItem); cr.insertingclubroles(RoleDesc, RoleCreatedBy); gettingclubroles(); //refresh the list }我如何将组合框中选择的内容作为参数传递给它? 我正在填充组合框这个:
How can I pass what ever is selected in the combobox drop down as the parameter? I am filling the combobox like this:
private void loadclient() { DataSet dsloadclient; dsloadclient = new ClubRoles().createdby(); //need fix duplicate values DataTable dt = dsloadclient.Tables[0].DefaultView.ToTable(true, "ClientName", "ClientId"); lstcreatedby.DataSource = dt; lstcreatedby.DisplayMember = "ClientName"; lstcreatedby.ValueMember = "ClientId"; }任何帮助将不胜感激。 谢谢。
Any help will be appreciated. Thanks.
推荐答案像这样修改代码................. 。 获取你应该只使用SelectedValue的值因为你只在值字段中绑定ClientId Modify your code like this way.................. to get the values you should use only SelectedValue because you bind the ClientId in value field only public void SaveRole(string RoleDesc, /*combobox selecteditem*/) { ClubRoles cr = new ClubRoles(); if (lstcreatedby.SelectedIndex > 0) { RoleCreatedBy = Convert.ToInt32(lstcreatedby.SelectedValue); cr.insertingclubroles(RoleDesc, RoleCreatedBy); } gettingclubroles(); //refresh the list }
private void loadclient() { DataSet dsloadclient; dsloadclient = new ClubRoles().createdby(); //need fix duplicate values DataTable dt = dsloadclient.Tables[0].DefaultView.ToTable(true, "ClientName", "ClientId"); lstcreatedby.DataSource = dt; lstcreatedby.DataTextField= "ClientName"; lstcreatedby.DataValueField= "ClientId"; }
您可以使用intellisense学到很多东西。 要获取所选项目,请使用SelectedItem属性 msdn.microsoft/en-us/library/system.windows.formsbobox.selecteditem(v = vs.110) .aspx [ ^ ] You can learn a lot by using intellisense. To get the selected item, use the SelectedItem property, msdn.microsoft/en-us/library/system.windows.formsbobox.selecteditem(v=vs.110).aspx[^]
更多推荐
Combobox值到数据库sql
发布评论