Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Karthik Harve 18-Feb-13 23:58pm    
is it web application or windows application ?
aliwpf 19-Feb-13 1:06am    
Do you initialize Days comboBox from DataBase with saved Day value such as sunday at your MainWindow Loaded event?

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)
Share this answer
boogac 20-Feb-13 4:57am    
fjdiewornncalwe 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
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...
Share this answer
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
Share this answer

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