Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Team ,

I have simple application in windows form which displays records from database into ListView.

This is my Table :

NAME   EDUCATION   SKILLS   EXP 

  A       BE        JAVA     1



I have two textBoxes for SKILLS and EXP ,It shows records according its selection

So I want to show error message for selection which is not in my table . However I am not getting error message.

This is my code ....Please help me.

C#
private void Search_Click(object sender, EventArgs e)
{
   try
   {
      SqlConnection objCon = new SqlConnection("Data Source=OM-PC; Initial Catalog=master; Integrated Security=True");
      SqlDataAdapter objAdapt = new SqlDataAdapter("SELECT * FROM VACANCY WHERE SKILLS='" + textBox1.Text + "'and EXPERIENCE='" + textBox5.Text + "'", objCon);

      DataTable objDT = new DataTable();
      objAdapt.Fill(objDT);
      //now initialize listview
      // Set the view to show details.
      listView2.View = View.Details;
      // Allow the user to edit item text.
      listView2.LabelEdit = true;
      // Allow the user to rearrange columns.
      listView2.AllowColumnReorder = true;
      // Select the item and subitems when selection is made.
      listView2.FullRowSelect = true;
      // Display grid lines.
      listView2.GridLines = true;
      // Sort the items in the list in ascending order.
      // listView1.Sorting = SortOrder.Ascending;
      //finally fill listview
      for (int i = 0; i < objDT.Rows.Count; i++)
      {
         //    listView2.Enabled = true;

         DataRow objDR = objDT.Rows[i];
         ListViewItem listitem = new ListViewItem(objDR["JOBCODE"].ToString());
         listitem.SubItems.Add(objDR["COMPANY"].ToString());

         listitem.SubItems.Add(objDR["SKILLS"].ToString());
         listitem.SubItems.Add(objDR["EXPERIENCE"].ToString());
         listitem.SubItems.Add(objDR["POST"].ToString());
         listitem.SubItems.Add(objDR["POSTED"].ToString());

         listView2.Items.Add(listitem);
      }
   }
   catch (Exception)
   {
      MessageBox.Show("No Records Found", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
   }
}
Posted
Updated 13-May-14 0:17am
v4

OK...first off, don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, a lack of records is not an error - it's a normal operation - so no exception will be thrown when there are no matching records. Think about it: if you want to see if you are signed up with a website, it isn't an error if your name isn't on their system! :laugh:

If you want to know how many matching records there are, then check the objDT.Rows.Count for being non-zero explicitly, using an if statement.
 
Share this answer
 
Your code will not throw an exception if nothing is returned from the query, it simply won't display anything.

Try something like
if(objDT.Rows.Count == 0
     MessageBox.Show(" No records found for this skills");
else
     for (int i = 0; i < objDT.Rows.Count; i++)
     {
        ... rest of your code


[Edit - a little more help on where that check could go]
 
Share this answer
 
v2
Comments
Member 10272175 13-May-14 5:49am    
Thanks for ans Sir,'

Sir , In my table I have skills=JAVA and EXP=1 ,My goal is If I select SKILLS=JAVA having EXP=2
Then it should throw error " No records found for this skills "

Please help me for same
C#
if(objDT.Rows.Count >0)
{
   Your for loop;
}
else
{
   MessageBox.Show("No Records Found");
}
 
Share this answer
 
v2
Comments
Member 10272175 13-May-14 6:01am    
Thanks for ans Sir,' Sir , In my table I have skills=JAVA and EXP=1 ,My goal is If I select SKILLS=JAVA having EXP=2 Then it should throw error " No records found for this skills " Please help me for same
CHill60 13-May-14 6:50am    
The poster has given you the answer. Just change the text of the MessageBox

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900