简体   繁体   English

在SQL上找不到值时如何显示消息框

[英]How to Display Messagebox When No Values Found on SQL

Following is a code to get values from table in sql and set it to relevant fields. 以下是从sql中的表中获取值并将其设置为相关字段的代码。 However i want to know how to write if condition in below block code so if the datatable contains the USERID it will performs function below but if it didn't find the USERID it should popup a error message saying no User Found. 但是我想知道如何在下面的块代码中编写条件,因此,如果数据表包含USERID,它将在下面执行功能,但是如果找不到USERID,它将弹出一条错误消息,提示未找到用户。

SqlCommand myCommand = new SqlCommand
("SELECT * From USER_TABLE WHERE USERID =" + userIdTextBox.Text, con1);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
    nameTextBox.Text = (myReader["FIRST_NAME"].ToString());
    lnameTextBox.Text = (myReader["LAST_NAME"].ToString());
    posTextBox.Text = (myReader["POSITION"].ToString());
    emailTextBox.Text = (myReader["E_MAIL"].ToString());
    phoneTextBox.Text = (myReader["PHONE"].ToString());
    usernameTextBox.Text = (myReader["USERNAME"].ToString());
    userLevelTextBox.Text = (myReader["USER_LEVEL"].ToString());
    string filename = (myReader["PROFILE_PICTURE"].ToString());
    profilePicBox.Load(filename);

}
        if (myReader.Read()) //assuming you only ever have a single result...
        {
            //set form fields.
        }
        else
        {
            //message box 
        }

Edit based on comment from @dmitry-bychenko 根据@ dmitry-bychenko的评论进行编辑

You need to check if(myReader.HasRows) 您需要检查if(myReader.HasRows)

  if(MyReader.HasRows)
    {
         while (myReader.Read())
                {
                   //your code here.
                }
    }
    else
    {
      // your alert.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM