1
Answer

check record already exists using combobox incsharp

Photo of narasiman rao

narasiman rao

12y
2.5k
1

 Note: it is windows application.

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.

how can i do using csharp

 Note: it is windows application.
 

Answers (1)

0
Photo of Dorababu Meka
NA 8.2k 1.2m 12y
Before saving the record get all the days from the database with the name suresh

Select Day from table where name='Suresh'

SqlConnection con=new SqlConnection("your connection string");
SqlCommand cmd=new SqlCommand("Select Day from table where name='"+condition+"'",con);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds=new DataSet();
da.Fill(ds);

List<string> lstDays=new List<string>();
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
lstDays.Add(ds.Tables[0].Rows[i]["Day"].ToString());
}

if(lstDays.Contains(textboxvalue)
{
    MessageBox.Show("The day already exists");
}
else
{
insert to table
}