Click here to Skip to main content
13,345,744 members (52,130 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have two combobox as follows;

Name combobox
Days combobox

in the name retrieved from the database and display into the combobox.
in the days column manually typed in the code sunday to saturday.

in the run mode i select the name and select the days and click the save button. the records are saved in the database. it is working fine.

for example as follows;

Name Suresh
Days Sunday

the above record is saved in the database.

then i have one search button,when i click the search button from the database records are displayed in the datagridview,when i click the datagridview, the particular record is displayed in the respective combobox.

suppose when i select the suresh and select the sunday and click save button, that time validate ,For suresh sunday is already exists.

for that how to validate the Combobox.using csharp.

please help me.

Thanks & Rgds,
Narasiman P.
Posted 18-Feb-13 18:50pm
Karthik Harve 18-Feb-13 23:58pm
is it web application or windows application ?
ali rad 19-Feb-13 1:06am
Do you initialize Days comboBox from DataBase with saved Day value such as sunday at your MainWindow Loaded event?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try placing a sql query behind the Save button e.g.
sql = "select from table where name='" + Name.Text + "' and days = '" + Days.Text + "'"
then if the SqlDataReader.HasRows (or whatever you are using) returns true you know the combination already exists.
Display an error message and don't perform the save

(NB I haven't done it here for the sake of being lazy but you should really use proper parameterised queries rather than inserting the content of textboxes as I've done here)
boogac 20-Feb-13 4:57am
Marcus Kramer 20-Feb-13 14:17pm
My 5.
CHill60 20-Feb-13 17:56pm
Thank you! And I lurve the emboldening on the *use parameterisation guys* improvement - we *will* get the message out there
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

in button click event you have to write this code....

 protected void btnsave_Click(object sender, EventArgs e)
 MySqlCommand msql = new MySqlCommand("select from tablename where name='" +Name_combobox.Text + "' and days = '" + Days_combobox.Text + "' ", con);
            MySqlDataReader mdr = msql.ExecuteReader();
            if (mdr.Read())
                msgshow.Visible = true;// this is a label control
                msgshow.Text = "Duplicate entry found";
               Do here your Insert Code....
Catch(Exception ex)

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

Solution 3

the best idea is u must have an unique id (Primary key) for each name so that u can distinguish easily.
on the leave event of combobox, u can use SqlDataReader to read the data from database and compare the strings using a loop...
simple example:
int i = 0, index;
            string dbUserName;
            string dbPassword;
            string username = txtUserName.Text;
            string password = txtPassword.Text;
            bool login = false;          
            while (i < dt.Rows.Count)
                DataRow drow = dt.Rows[i];
                dbUserName = drow[0].ToString();
                dbPassword = drow[1].ToString();
                string uName = dbUserName.Trim();
                string pw = dbPassword.Trim();
                if (username == uName && password == pw && role == index)
                    login = true;
            if (login == true)
                  // Do your code

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 |
Web04 | 2.8.180111.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2018
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