【问题标题】:Adding Int value in Textbox Control在文本框控件中添加 Int 值
【发布时间】:2011-04-23 08:27:47
【问题描述】:

当我向我的数据库添加一条记录时,我会从表中检索自动生成的 ID 键。我需要将 ID 放入文本框中,以便稍后在我的代码中使用。这是我的代码,它给了我这个错误:

输入的字符串格式不正确

这是我的代码:

// Insert the record by executing the command
numRowsAdded = command.ExecuteNonQuery();

// Use @@IDENTITY to work out the primary key of 
// the new row and display it
command.CommandText = "SELECT @@IDENTITY";
id = Convert.ToInt32(command.ExecuteScalar());

id = Convert.ToInt32(lessonIDTextbox.Text);

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    如果您这样做的唯一原因是为了稍后在代码中使用,请不要将其放在文本框中。

    您收到上述错误是因为 courseIDTextbox 中没有值(基于您向我们展示的代码)。 id 已经有一个值,您可以稍后在代码中使用它。如果您试图通过回发或其他方式保留该值,那么您可能需要探索其他选项。

    如果有其他原因需要将其放入文本框中,您可以这样做:

    lessonIDTextbox.Text=id.ToString();
    

    【讨论】:

      【解决方案2】:
      lessonIDTextbox.Text=command.ExecuteScalar().ToString()
      

      【讨论】:

        猜你喜欢
        • 2012-11-26
        • 1970-01-01
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        相关资源
        最近更新 更多