为什么这个字符串总是恢复为“”?

时间:2013-10-10 16:10:32

标签: c# sql winforms sql-server-ce

继承我的代码:

public userScreen(string uName)
    {
        InitializeComponent();
        userName = uName;
        string topLogNumber = "0";
        textBox2.Text = userName;
        Conversion(topLogNumber);
        textBox1.Text = topLogNumber;
        this.WindowState = FormWindowState.Maximized;

private string Conversion(string lognumber)
    {
        lognumber = gettoplognumber();
        int lognumberint = Convert.ToInt32(lognumber);
        lognumberint = lognumberint+1;
        lognumber = lognumberint.ToString();
        return lognumber;
    }

private string gettoplognumber()
    {
        string selectTopLogNumber = "SELECT MAX (LogNo) from Logs";
        string connection = "Data Source = \"EventControl.sdf\"";
        SqlCeConnection connexion = new SqlCeConnection(connection);
        SqlCeCommand topLog = new SqlCeCommand(selectTopLogNumber, connexion);
        try
        {
            connexion.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            return "error";
        }
        return topLog.ExecuteScalar() as string;

现在,这段代码的作用是查看数据库并将数字递增1。当它运行时(我调试了整个过程),由于某种原因它一切正常,直到它到达textBox1.Text = topLogNumber;此时它会变回“”。任何人????

1 个答案:

答案 0 :(得分:1)

更改此行以获得转化结果

topLogNumber = Conversion(topLogNumber);

或只是将转化结果分配到文本框

textBox1.Text = Conversion(topLogNumber);

Conversion内的计算结果将在return语句中传回。您需要将该calc的结果重新分配给userScreen中使用的变量topLogNumber。