Click here to Skip to main content
16,019,740 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Team ,

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

This is my Table :

Collapse | Copy Code
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 21-May-14 2:18am
v2
Comments
[no name] 21-May-14 8:21am    
Not having a record in your database is not an exception.

That's the correct behaviour, since, from SqlDataAdapter's documentation[^]:
If a command does not return any rows, no tables are added to the DataSet, and no exception is raised.


You might throw yourself the exception (or simply show directly the error message) when objDT.Rows.Count is 0.
 
Share this answer
 
v2
Hi,

include the following code into your code for displaying the message

C#
if(objDT.Rows.Count>0)
{
   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);
      }

}
else
{
  MessageBox.Show("No Records Found", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
 
Share this answer
 
v2
Comments
Member 10272175 21-May-14 8:41am    
Hey Nilschaudhari and Sander Rossel........Since many days I fed up with this small issue
Now its working fine.
Many Many Thanks !! Love you !!
Look first thing is if no records found it is not a kind of exception.

Still if you want to get it as exception you can through your custom exception as mentioned below :-

Ex :

C#
try
{
        if(objDT.Rows.Count <= 0){
        throw new Exception("No Records found.");
    }else{
        //do operations on the records iterating through it
    }

}
catch (Exception ex)
{
    if (ex.Message == "No Records found.")
        {
        MessageBox.Show("No Records Found", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}


OR

You can also directly show the message-box directly at place where you are getting no records in the IF condition satisfaction.

Hope this will definitely be of help, else let me know if any further concerns are there.
 
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