错误消息未进入C#Windows窗体 [英] Error message is not getting in C# Windows form

查看:132
本文介绍了错误消息未进入C#Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Team,



我在Windows窗体中有简单的应用程序,它将数据库中的记录显示在ListView中。



这是我的表:



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







我有两个用于SKILLS和EXP的文本框,它根据其选择显示记录



所以我想显示选择的错误信息,这不在我的表格中。但是我没有收到错误消息。



这是我的代码....请帮帮我。






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.

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);
   }
}

推荐答案

好的......先关闭,不要那样做!

不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



其次,缺少记录不是错误 - 这是正常操作 - 因此当没有匹配记录时不会抛出异常。想一想:如果你想看看你是否注册了一个网站,如果你的名字不在他们的系统上就不是错误! :笑:



如果你想知道有多少匹配的记录,那么检查 objDT.Rows.Count 显式为非零,使用 if 语句。
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.


如果没有返回任何内容,您的代码将不会抛出异常查询,它根本不会显示任何内容。



尝试类似
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








if(objDT.Rows.Count >0)
{
   Your for loop;
}
else
{
   MessageBox.Show("No Records Found");
}


这篇关于错误消息未进入C#Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆