如何通知用户某个元素在表中不存在?

本文关键字:元素 不存在 何通知 通知 用户 | 更新日期: 2023-09-27 18:04:24

我有一个get详细信息表单,我知道在这里使用try和catch作为验证方式是不好的做法。如何检查CustID是否存在,然后告诉用户他们输入的内容不存在?

抱歉,如果这是一个愚蠢的问题,这是显而易见的…我是初学者。

public void getdetails()
{
    lblMessage.Text = "";
    if (txtCID.Text == "")
    {
        lblMessage.Text = "Please enter a Customer ID before obtaining details.";
    }
    else
    {
        command.Connection.Open();
        command.Connection = conn;                
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "GetCustomer";
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@CustID";
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        param.Value = txtCID.Text;
        command.Parameters.Add(param);
        adapter.SelectCommand = command;
        adapter.Fill(table);
        txtFName.Text = table.Rows[0].Field<string>("FirstName");
        txtFName.DataBind();
        txtLName.Text = table.Rows[0].Field<string>("Surname");
        txtLName.DataBind();
        rdoGender.Text = table.Rows[0].Field<string>("Gender").ToString();
        txtAge.DataBind();
        txtAge.Text = table.Rows[0].Field<int>("Age").ToString();
        txtAge.DataBind();
        txtAdd1.Text = table.Rows[0].Field<string>("Address1").ToString();
        txtAge.DataBind();
        txtAdd2.Text = table.Rows[0].Field<string>("Address2").ToString();
        txtAge.DataBind();
        txtCity.Text = table.Rows[0].Field<string>("City").ToString();
        txtAge.DataBind();
        txtPhone.Text = table.Rows[0].Field<string>("Phone").ToString();
        txtAge.DataBind();
        txtMobile.Text = table.Rows[0].Field<string>("Mobile").ToString();
        txtAge.DataBind();
        txtEmail.Text = table.Rows[0].Field<string>("Email").ToString();
        txtEmail.DataBind();
        command.Connection.Close();
    }
}

如何通知用户某个元素在表中不存在?

由于您填写了DataTable,因此很容易确定客户是否存在,因此使用DataTable.Rows.Count > 0:

bool customerExists = table.Rows.Count > 0;
if(!customerExists)
{
    lblMessage.Text = $"The customer with CustomerID={txtCID.Text} is unknown.";        
}

除此之外…

  • 使用using语句连接和实现IDisposable的所有内容
  • 用c#将字符串转换为int,不要让数据库为你做。这样,使用int.TryParse,还可以验证输入

所以这是你的方法,包括这些和其他改进:

public void LoadCustomerDetails()
{
    lblMessage.Text = "";
    if (String.IsNullOrWhiteSpace(txtCID.Text))
    {
        lblMessage.Text = "Please enter a CustomerID before obtaining details.";
        return;
    }
    DataTable table = new DataTable();
    int customerID;
    using (var conn = new SqlConnection(Properties.Settings.Default.TestDbCon))
    using (var da = new SqlDataAdapter("GetCustomer", conn))
    using (var cmd = da.SelectCommand)
    {
        cmd.CommandType = CommandType.StoredProcedure;
        if (!int.TryParse(txtCID.Text.Trim(), out customerID))
        {
            lblMessage.Text = "Please enter a valid integer CustomerID before obtaining details.";
            return;
        }
        cmd.Parameters.Add("@CustID", SqlDbType.Int).Value = customerID;
        da.Fill(table); // you don't need to open/close the connection with Fill
    }
    if (table.Rows.Count == 0)
    {
        lblMessage.Text = $"No customer with CustomerID={customerID} found.";
        return;
    }
    DataRow custumerRow = table.Rows.Cast<DataRow>().Single(); // to cause an exception on multiple customers with this ID
    txtFName.Text = custumerRow.Field<string>("FirstName");
    txtLName.Text = custumerRow.Field<string>("Surname");
    rdoGender.Text = custumerRow.Field<string>("Gender").ToString();
    txtAge.Text = custumerRow.Field<int>("Age").ToString();
    txtAdd1.Text = custumerRow.Field<string>("Address1").ToString();
    txtAdd2.Text = custumerRow.Field<string>("Address2").ToString();
    txtCity.Text = custumerRow.Field<string>("City").ToString();
    txtPhone.Text = custumerRow.Field<string>("Phone").ToString();
    txtMobile.Text = custumerRow.Field<string>("Mobile").ToString();
    txtEmail.Text = custumerRow.Field<string>("Email").ToString();
}

我不太明白你的问题。我要做的是:

public bool getdetails()
{
    bool found = false;
    int id;
    bool isnumber;
    lblMessage.Text = "";
    isnumber = int.TryParse(txtCID.Text, out id);
    if (!isnumber)
    {
        lblMessage.Text = "Please enter a valid Customer ID before obtaining details.";
    }
    else
    {
        command.Connection.Open();
        command.Connection = conn;                
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "GetCustomer";
        SqlParameter param = new SqlParameter();
        param.ParameterName = "@CustID";
        param.SqlDbType = SqlDbType.Int;
        param.Direction = ParameterDirection.Input;
        param.Value = id;
        command.Parameters.Add(param);
        adapter.SelectCommand = command;
        adapter.Fill(table);
        if (table.Rows.Count > 0)
        {
          txtFName.Text = table.Rows[0].Field<string>("FirstName");
          txtFName.DataBind();
          txtLName.Text = table.Rows[0].Field<string>("Surname");
          txtLName.DataBind();
          rdoGender.Text = table.Rows[0].Field<string>("Gender").ToString();
          txtAge.DataBind();
          txtAge.Text = table.Rows[0].Field<int>("Age").ToString();
          txtAge.DataBind();
          txtAdd1.Text = table.Rows[0].Field<string>("Address1").ToString();
          txtAge.DataBind();
          txtAdd2.Text = table.Rows[0].Field<string>("Address2").ToString();
          txtAge.DataBind();
          txtCity.Text = table.Rows[0].Field<string>("City").ToString();
          txtAge.DataBind();
          txtPhone.Text = table.Rows[0].Field<string>("Phone").ToString();
          txtAge.DataBind();
          txtMobile.Text = table.Rows[0].Field<string>("Mobile").ToString();
          txtAge.DataBind();
          txtEmail.Text = table.Rows[0].Field<string>("Email").ToString();
          txtEmail.DataBind();
          found = true;
        }
        else
        {
           lblMessage.Text = "User with ID " + id + " does not exists";
        }
        command.Connection.Close();
    }
    return found;
}

如果id未指定或不存在,函数将返回false。另一个问题是你没有检查是否txtCID。文本包含一个有效的数字:在这种情况下,将抛出一个SQL错误!我添加了一个数字转换检查,以确保至少存储过程的执行没有错误。无论如何,您应该将整个过程包装在try-catch中,以拦截任何不可预测的错误(db脱机或内部db错误等)。然后,我使用table.Rows.Count来验证存储过程是否返回了结果。

马里奥。