Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17:50pm
Comments
Karthik Harve at 18-Feb-13 23:58pm
   
is it web application or windows application ?
ali rad at 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
good
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 + "'"
etc...
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)
  Permalink  
v3
Comments
boogac at 20-Feb-13 4:57am
   
5+
Marcus Kramer at 20-Feb-13 14:17pm
   
My 5.
CHill60 at 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
good
Please Sign up or sign in to vote.

Solution 2

hi
 
in button click event you have to write this code....
 
 
 protected void btnsave_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
 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())
            {
                mdr.Close();
                msgshow.Visible = true;// this is a label control
                msgshow.Text = "Duplicate entry found";
            }
            else
            {
               Do here your Insert Code....
                   
            } 
        }
Catch(Exception ex)
{
msgshow.text=ex.message; 
}
}
 
 
Thanks Happy to help...
  Permalink  
v2
Rate this: bad
good
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;
                }
                i++;
                     
            }
            if (login == true)
            {
                  // Do your code
            }
            
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 383
2 Manas Bhardwaj 324
3 Tadit Dash 305
4 Peter Leow 255
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid