我有一个获取详细信息表单,我知道使用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();
}
}
答案 0 :(得分:3)
由于您填写了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
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();
}
答案 1 :(得分:1)
我不确定我是否完全理解你的问题。我会做的是:
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.Text是否包含有效数字:在这种情况下会抛出SQL错误! 我添加了一个数字转换检查,确保至少存储过程执行运行没有错误。无论如何,您应该将整个过程包装在try-catch中以拦截任何不可预测的错误(db offline或内部db错误等)。 然后,我使用table.Rows.Count来验证存储过程是否返回了结果。
马里奥。