Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello am inexperience programmer working on a database in sql using c#. in my form i have created a search field which search based on the text in combobox1 and textbox1. My problem is anything i search , the results does not link with my textboxes. it only links with datagridview and listbox but not textboxes. Before the search the datagridview and listbox link with my textboxes but anytime i search it does not.
this is my code for the search:
private void search()
{
string str = (@"Data Source=BROTHER-PC;Initial Catalog=iEMYS;Integrated Security=True");


SqlConnection con = new SqlConnection(str);
if (comboBox1.SelectedIndex == 0)
{
try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where TeacherId like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Teacher ID was Not Found. Try Another!");


}
}
catch (Exception)
{

}
}

else if (comboBox1.SelectedIndex == 1)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand(
"select * from teacher where EMYSNO like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("EMYSNO was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 2)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand(
"select * from teacher where [First Name] like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("First Name was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 3)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where [Last Name] like '%" + searchtext + "%'",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Last Name was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 4)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where Sex like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("SexType was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}

}
else if (comboBox1.SelectedIndex == 5)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where Hometown like '%" + searchtext + "%'",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Hometown was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 6)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where Region like '%" + searchtext + "%'",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Region was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 7)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Single like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 8)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Married like '%" + searchtext + "%' order by TeacherId",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 9)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Name Of Spouse] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Name Of Spouse was not found! Try again");

}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 10)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Student like '%" + searchtext + "%' order by TeacherId",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 11)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where Institution like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Institution was not found! Try again");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 12)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Employed like '%" + searchtext + "%' order by TeacherId",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 13)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where Profession like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Profession was not found! Try again");

}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 14)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where Unemployed like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 15)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where [Telephone #1] like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Telephone #1 was Not Found. Try Another!");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 16)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Telephone #2] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Telephone #2 was Not Found. Try Another!");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 17)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where Email like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Email was Not Found. Try Another!");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 18)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where [Postal Address] like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Postal Address was Not Found. Try Another!");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 19)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where [Home Address] like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Home Address was Not Found. Try Another!");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 20)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand(
"select * from teacher where [Home Area] like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Home Area was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 21)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Baptism like '%" + searchtext + "%' order by TeacherId",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 22)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where Confirmation like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 23)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where [Church Status] like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
MessageBox.Show("Church Status was Not Found. Try Another!");
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 24)
{

try
{
string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Position like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Position was Not Found. Try Another!");


}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 25)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Brigade like '%" + searchtext + "%' order by TeacherId",
con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 26)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Christ Little Band] like '%" + searchtext + "%' order by TeacherId", con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 27)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Girls Fellowship] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 28)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand("select * from teacher where Guild like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 29)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Men Fellowship] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 30)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Singing Band] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 31)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Sussana Wesley] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{
}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 32)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Women Fellowship] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}
else if (comboBox1.SelectedIndex == 33)
{

try
{

string searchtext = textBox1.Text;
SqlCommand cmd =
new SqlCommand(
"select * from teacher where [Youth Fellowship] like '%" + searchtext + "%' order by TeacherId", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{



}
}
catch (Exception ex)
{
}
}

}
Posted
Comments
George Jonsson 2-Jul-14 3:34am    
Please format your code with the tools provided.

Have you bound your text boxes to any data source?
MaximusDebois 2-Jul-14 3:38am    
yes
Data Source=BROTHER-PC;Initial Catalog=iEMYS;Integrated Security=True
keerth516 2-Jul-14 3:46am    
hey you want to bind data to datagrid based on combox selection and textbox data right?
or else you have other scenario to implement.
MaximusDebois 2-Jul-14 4:17am    
yeah
j snooze 2-Jul-14 13:21pm    
Since you are new I'd like to do you a favor and cut the amount of code you have by a lot(it could be done by more, but you said you are new so I'll try to keep it somewhat simple). First you should notice you have repeat code all over the place. with only 1 thing changing (the sql statement search field) you should use a function to return your search field. See if the code makes sense to you. It is not complete, I only did the first few, but it will save you from a maintenance nightmare later. Ideally you would bind your combo box to a list object that contains the search field and then you wouldn't even need the function....but thats deeper than you probably to get at this point. Here is the cut down code..


private void search()
{
string str = (@"Data Source=BROTHER-PC;Initial Catalog=iEMYS;Integrated Security=True");

SqlConnection con = new SqlConnection(str);
try
{
string searchtext = textBox1.Text;
SqlCommand cmd = new SqlCommand("select * from teacher where " + GetSearchField(comboBox1.SelectedIndex) + "like '%" + searchtext + "%'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds, "teacher");
teacherDataGridView.DataSource = ds.Tables["teacher"];
listBox1.DataSource = ds.Tables["teacher"];
if (teacherDataGridView.RowCount == 1)
{

MessageBox.Show("Teacher ID was Not Found. Try Another!");


}
}
catch (Exception)
{

}
}

private string GetSearchField(int searchSelect)
{
string searchField = string.Empty;

switch (searchSelect)
{
case 1:
searchField = "TeacherId";
break;
case 2:
searchField = "EMYSNO";
break;
case 3:
searchField = "[First Name]";
break;
case 4:
searchField = "[Last Name]";
break;
default:
searchField = "TeacherId";
break;
} //end switch

return searchField;
}

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