简体   繁体   English

将文本框值转换为 int 并检查它是否为空

[英]Convert Textbox value to int and also check for is it Empty or Not

I am trying to update record of a person in a gridview when i am input into textbox(tAge) it shows me an exception Input string was not in a correct format.当我输入到文本框(tAge)时,我试图更新 gridview 中一个人的记录,它显示一个异常输入字符串的格式不正确。 i had tried a lot of codes but won't work please help me out.And thanks in advance.我已经尝试了很多代码,但无法正常工作,请帮助我。在此先感谢 here is my code to update Person details这是我更新人员详细信息的代码

protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
    int personID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
    int intResult = 0;
    GridViewRow row = GridView1.Rows[e.RowIndex];
    TextBox tFN = (TextBox)row.FindControl("txtFName");
    TextBox tLN = (TextBox)row.FindControl("txtLName");
    TextBox tAge = (TextBox)row.FindControl("txtAge");
    // instantiate BAL
    PersonBAL pBAL = new PersonBAL();
    PersonBO person = new PersonBO();
    try
    {
        person.PersonID = personID;
        person.FirstName = tFN.Text;
        person.LastName = tLN.Text;
        person.Age = Int32.Parse(tAge.Text);
        //if (String.IsNullOrEmpty(String.Trim(tFN.Text)))
        if (tFN.Text.Trim() == "")
        {
            lblMessage.Text = "Name can't be Blank";
        }
        else if (tLN.Text.Trim() == "")
        {
            lblMessage.Text = "LastName can't be Blank";
        }
        else if (tAge.Text.Trim()=="")
        {
            lblMessage.Text = "Age can't be Blank";
        }
        else
        {
            intResult = pBAL.Update(person); 
        if (intResult > 0 && tFN.Text != "")
        {
            string message = "Updated Successfully!";
            string script = "window.onload = function(){ alert('";
            script += message;
            script += "');";
            script += "window.location = '";
            script += Request.Url.AbsoluteUri;
            script += "'; }";
            ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
        }
        //else if( tFN.Text == "")
        //{
        //} 
        else
        {
            string message = "Already Exist!";
            string script = "window.onload = function(){ alert('";
            script += message;
            script += "');";
            script += "window.location = '";
            script += Request.Url.AbsoluteUri;
            script += "'; }";
            ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
            //lblMessage.Text = message.ToString();
        }
    }
    }
    catch (Exception ee)
    { 
        lblMessage.Text = ee.Message.ToString(); 
    }

    finally
    {
        person = null;
        pBAL = null;
    }
    GridView1.EditIndex = -1;
    // Refresh the list
    BindGrid();
}

I would suggest to check the Text property first before assigning it to the appropriate values.我建议先检查Text属性,然后再将其分配给适当的值。 For the conversion there exist a nice method TryParse it returns a bool and will only parse if the format is correct.对于转换,存在一个很好的方法TryParse它返回一个bool并且仅在格式正确时才进行解析。

try
{
    // ask whether it is blank or full of spaces
    if (string.IsNullOrWhiteSpace(tFN.Text))
    {
        lblMessage.Text = "Name can't be Blank";
    }
    else 
    {
        person.FirstName = tFN.Text;
    }

    // do the same again for the last name
    if (string.IsNullOrWhiteSpace(tLN.Text))
    {
        lblMessage.Text = "Name can't be Blank";
    }
    else 
    {
        person.FirstName = tLN.Text;
    }


    // use TryParse for the age
    int p_age;
    if (Int.TryParse(tAge.Text, out p_age))
    {
        person.Age = p_age;
    }
    else 
    {
        lblMessage.Text = "Age is in incorrect Format";
    }

   ....

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

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