c#没有数据时读取无效

时间:2018-03-27 11:08:27

标签: c# submit

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

这是一个bool事BTW。每次调试时,都会记录如下错误

  

无数据存在时读取无效

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

1 个答案:

答案 0 :(得分:0)

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