Click here to Skip to main content
12,821,284 members (40,509 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
I have 2 combobox
one loads shift
second loads Department

on the click of a button i written query based on that query it should display result. but it only works for first values of two combobox not for others. what is the problem
please help me

public void DisplayEmployee()
                string s = CmbShiftdetails.Text;//.SelectedItem.ToString();
                string sql_displayemp = string.Format("SELECT EmployeeMaster.EmployeeId, EmployeeMaster.EmployeeCode, EmployeeMaster.EmployeeName, EmployeeMaster.Address, EmployeeMaster.PhoneNo, EmployeeMaster.Emailid, EmployeeMaster.Designation, EmployeeMaster.Photo, DepartmentMaster.DepartmentId, DepartmentMaster.DepartmentName, ShiftMaster.ShiftId, ShiftMaster.ShiftName FROM DepartmentMaster INNER JOIN EmployeeMaster ON DepartmentMaster.DepartmentId = EmployeeMaster.DepartmentId INNER JOIN ShiftMaster ON EmployeeMaster.ShiftId = ShiftMaster.ShiftId where ShiftMaster.ShiftName='{0}' and DepartmentMaster.DepartmentName='{1}'", CmbShiftdetails.Text, CmbDepartment.Text);
                dtObj = obj.NonTransaction(sql_displayemp);
                if (dtObj.Rows.Count > 0)
                    TableLayoutPanel tlpobj = new TableLayoutPanel();
                    tlpobj.Name = "Emp";
                    tlpobj.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
                    tlpobj.AutoSize = true;

                    string[] headers = { "EmployeeCode", "EmployeeName", "Address", "PhoneNo", "Emailid", "Designation", "Photo", "Edit", "Delete" };
                    for (int i = 0; i < headers.Length; i++)
                        Label lbl = new Label();
                        lbl.Text = headers[i];
                        tlpobj.Controls.Add(lbl, i, 0);
                    for (int i = 0; i < dtObj.Rows.Count; i++)
                        Button btnedit = new Button();
                        btnedit.Text = "Edit";
                        btnedit.Name = "e_" + dtObj.Rows[i]["EmployeeId"].ToString();
                        btnedit.Click += new EventHandler(btnedit_Click);
                        btnedit.Width = 60;

                        Button btndelete = new Button();
                        btndelete.Text = "Delete";
                        btndelete.Name = "d_" + dtObj.Rows[i]["EmployeeId"].ToString();
                        btndelete.Click += new EventHandler(btndelete_Click);
                        btndelete.Width = 60;

                        Label lblempid = new Label();
                        lblempid.Text = dtObj.Rows[i]["EmployeeId"].ToString();
                        lblempid.Name = "lblempid" + dtObj.Rows[i]["EmployeeId"].ToString();
                        lblempid.Visible = false;

                        Label lblempcode = new Label();
                        lblempcode.Text = dtObj.Rows[i]["EmployeeCode"].ToString();
                        lblempcode.Name = "lblempcode" + dtObj.Rows[i]["EmployeeId"].ToString();

                        Label lblempname = new Label();
                        lblempname.Text = dtObj.Rows[i]["EmployeeName"].ToString();
                        lblempname.Name = "lblempname" + dtObj.Rows[i]["EmployeeId"].ToString();

                        Label lbladdress = new Label();
                        lbladdress.Text = dtObj.Rows[i]["Address"].ToString();
                        lbladdress.Name = "lbladdress" + dtObj.Rows[i]["EmployeeId"].ToString();

                        Label lblphone = new Label();
                        lblphone.Text = dtObj.Rows[i]["PhoneNo"].ToString();
                        lblphone.Name = "lblphone" + dtObj.Rows[i]["EmployeeId"].ToString();

                        Label lblemail = new Label();
                        lblemail.Text = dtObj.Rows[i]["Emailid"].ToString();
                        lblemail.Name = "lblemail" + dtObj.Rows[i]["EmployeeId"].ToString();

                        Label lbldesignation = new Label();
                        lbldesignation.Text = dtObj.Rows[i]["Designation"].ToString();
                        lbldesignation.Name = "lbldesignation" + dtObj.Rows[i]["EmployeeId"].ToString();


                        PictureBox pbemp = new PictureBox();
                        pbemp.SizeMode = PictureBoxSizeMode.StretchImage;
                            pbemp.Image = Image.FromStream(new MemoryStream(((byte[])dtObj.Rows[i]["Photo"])));
                        catch { }
                        pbemp.Name = "pbemp" + dtObj.Rows[i]["EmployeeId"].ToString();


                        // tlpobj.Controls.Add(lblempid, 0, i + 1);
                        tlpobj.Controls.Add(lblempcode, 0, i + 1);
                        tlpobj.Controls.Add(lblempname, 1, i + 1);
                        tlpobj.Controls.Add(lbladdress, 2, i + 1);
                        tlpobj.Controls.Add(lblphone, 3, i + 1);
                        tlpobj.Controls.Add(lblemail, 4, i + 1);
                        tlpobj.Controls.Add(lbldesignation, 5, i + 1);
                        //tlpobj.Controls.Add(lblemail, 4, i + 1);
                        tlpobj.Controls.Add(pbemp, 6, i + 1);
                        //tlpobj.Controls.Add(lbltagid, 6, i + 1);
                        // tlpobj.Controls.Add(lblshiftid, 7, i + 1);
                        tlpobj.Controls.Add(btnedit, 7, i + 1);
                        tlpobj.Controls.Add(btndelete, 8, i + 1);


private void BtnGo_Click(object sender, EventArgs e)
Posted 17-Dec-12 3:48am
Updated 17-Dec-12 3:55am
CHill60 17-Dec-12 11:18am
If it works for some values and not for others I would suggest looking at your database - for example does that combination of shift and department actually exist in the database?
Kiran Susarla 17-Dec-12 11:45am
Are you getting any error? What is your expected result and what is the actual?
Sergey Alexandrovich Kryukov 17-Dec-12 14:29pm
Help with what? You did not ask question, did not explain any problem.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

1. Check the variable 'sql_displayemp' content i.e query string is updated with the second selection of the combo box controls
2. Check 'dtObj.Rows' returns any value for the above 'sql_displayemp'query, try copy the string content and execute in your SQL/database to ensure query is working correctly.
3.Put a break point at the line dtObj.Clear(); and you can able to solve yourself!
Lakshmimsridhar 18-Dec-12 0:06am
i have done all those u did before itself. i am getting result for only for first selection. second selection input its not taking
jibesh 18-Dec-12 0:11am
can you copy the the code if you added anything under the combo selection change event.??
Lakshmimsridhar 18-Dec-12 0:13am
i didnt put selection change event.
jibesh 18-Dec-12 0:17am
thats intresting... it should work. If you dont mind. can you create a new sample project and try the behavior of the combobox . just to make sure that we havent done anything unwanted.

This is what i used to do when i have some issue in native behavior of the control.
i mean: a new winform project with 2 combo and one button. check the .Text property of the combo inside ButtonClick event.
Lakshmimsridhar 18-Dec-12 0:21am
jibesh i done that. in new form i am getting but not getting here
jibesh 18-Dec-12 0:24am
this is really interesting. I would like to see the whole form code then. if its okay to share i can review and get back to you. may be any in shared location?
Lakshmimsridhar 18-Dec-12 0:27am
just a second
Lakshmimsridhar 18-Dec-12 0:39am
thanks for ur help. i solved it :)
jibesh 18-Dec-12 0:40am
cool. btw. what was the problem ?
Lakshmimsridhar 18-Dec-12 4:12am
i have added in pageload cmbshiftdetails.text = "Select",
this was the problem. i removed it. it was working fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100