【问题标题】:c# Invalid attempt to read when no data is presentc# 无数据时尝试读取无效
【发布时间】:2018-09-05 18:29:41
【问题描述】:

我在这里找不到问题,但这不起作用!任何帮助将不胜感激。

顺便说一句,这是一个布尔的东西。每次我调试时,它都会记录一个错误,如下所示

不存在数据时尝试读取无效

ICCqueueLabelDropDownList.Items.Clear();
string queryString = "(SELECT  [name] FROM [asterisk].[dbo].[sip_friends] where name = '" + phoneNumberDropDownList.SelectedItem + "');";
        SqlConnection conn = new SqlConnection(connectionString);

        SqlCommand selectCmd = new SqlCommand(queryString, conn);
        SqlDataReader myReader = null;
        bool value = false;
        try
        {
            conn.Open();                                
            myReader = selectCmd.ExecuteReader();          

            //myReader.Read();

            if (myReader["name"].ToString() != "" )  /*   ( myReader["name"].ToString() != ""  */
            {
                myReader.Read();


                value = true;
            }





        }
        catch (Exception ex)
        {
            //ErrorLabel.Text = ex.Message;
            hiba.Visible = true;
            hiba.Text = ex.Message + "\n Check Insert Call User Device ÁLERT!";

        }
        myReader.Close();
        conn.Close();
        return (value);
    }

【问题讨论】:

标签: c# submit


【解决方案1】:

@andrew,请查看下面的代码,让我知道它是否适合您?

        string connectionString = "[YOUR_CONNECTION_STRING]";
        ICCqueueLabelDropDownList.Items.Clear();
        string queryString = "(SELECT  [name] FROM [asterisk].[dbo].[sip_friends] where name = '" + phoneNumberDropDownList.SelectedItem + "');";
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand selectCmd = new SqlCommand(queryString, conn);
        SqlDataReader myReader = null;
        bool value = false;
        try
        {
            conn.Open();
            myReader = selectCmd.ExecuteReader();
            if (myReader.Read())
            {
                if (myReader["name"].ToString() != "")  
                {
                    value = true;
                }
            }
        }
        catch (Exception ex)
        {
        }
        myReader.Close();
        conn.Close();
        return (value);
    }

【讨论】:

  • 嗨!感谢您的帮助,但它不起作用。当我这样做时,网络应用程序不会弹出错误,但程序不会前进
  • 您能否发送一个您遇到的错误快照,我尝试使用给定的解决方案,根据您提到的情况,它似乎对我有用。因此,如果可能的话,请给我一个代码或错误快照。谢谢。
猜你喜欢
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多