输入字符串格式不正确

本文关键字:不正确 格式 字符串 输入 | 更新日期: 2023-09-27 17:49:25

public partial class addtheatre : System.Web.UI.Page
{
    bal objbal = new bal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             DataSet ds = objbal.getid();
            int s;
            string d = "9T000";
            string c = ds.Tables[0].Rows[0][0].ToString();
            if (c == "")
            {
                s = 1;
                d = "9T000" + s;
            }
            else
            {
              s = Convert.ToInt32(c);
                s = s + 1;
                if (s < 999)
                {
                    d = "9T000" + s;
                }
                //s = Convert.ToInt32(c);
                else if (s == 1000)
                {
                    d = "T1000";
                }
                else if (s > 1000)
                {
                    d = "T1000" + s;
                }
            }
            TextBox1.Text = d.ToString();
        }
    }
}

转换时显示为错误(输入字符串格式不正确)。
在数据库中,我把d作为varchar

输入字符串格式不正确

我猜错误是从这里来的:

s = Convert.ToInt32(c);

如果c的值不是整数的字符串表示形式(例如"123"),则这里会出现异常。您可以在调试器中检查c的值(例如在Visual Studio中)以查看问题所在。

我猜错误是从这里来的:

尝试下面的代码

int.TryParse(c,out s );

除了Mark Byers给出的和…
你应该使用
int.TryParse(string)int.Parse(string)将字符串转换为int