簡體   English   中英

在SQL上找不到值時如何顯示消息框

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

以下是從sql中的表中獲取值並將其設置為相關字段的代碼。 但是我想知道如何在下面的塊代碼中編寫條件,因此,如果數據表包含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 
        }

根據@ dmitry-bychenko的評論進行編輯

您需要檢查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