Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am trying to create a search button on my form which uses values from a comboBox as its primary key. but i get the error message cannot convert from 'System.Windows.Forms.ComboBox' to 'string'. What should i do
In the class I have this
C#
public bool searchstaffsalarypaymentDetails(string salaryid, string salarypurpose, string salaryyears, string salarymonths)
        {
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

Behind the form I have this
C#
private void lklSearch5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                clsStaffsalarypayment person5 = new clsStaffsalarypayment();

                if (person5.searchstaffsalarypaymentDetails(cboStaffid5, cboPurpose, cboMonth, cboYear))
                {
                    var
                    _with5 = this;
                    _with5.cboStaffid5.Text = person5.ID5.ToString();
Posted
Updated 28-May-12 19:25pm
v2

1 solution

The error message occurs since you are not using the ComboBox value for the searchstaffsalarypaymentDetails method, but the ComboBox itself.
Maybe you should rewrite it as:
C#
person5.searchstaffsalarypaymentDetails(cboStaffid5.SelectedItem.ToString(), cboPurpose.SelectedItem.ToString(), cboMonth.SelectedItem.ToString(), cboYear.SelectedItem.ToString()))
 
Share this answer
 
v2
Comments
mikeoabban 29-May-12 2:53am    
it worked but after run the program and click on the search button i get this
message object reference not set to an instance of an object
pls what should ido
JF2015 29-May-12 3:06am    
Make sure that the comboBoxes.SelectedItem is not NULL. You should add a check before calling your procedure, or simply select one items like:
cboStaffid5.SelectedIndex = 0;

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