C# 在Textbox控件中添加Int值
当我向数据库中添加记录时,我从表中检索自动生成的ID键。我需要将ID放入文本框中,以便以后在代码中使用。这是我的代码,它给了我这个错误: 输入字符串的格式不正确 这是我的密码:C# 在Textbox控件中添加Int值,c#,asp.net,C#,Asp.net,当我向数据库中添加记录时,我从表中检索自动生成的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 dis
// 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);
如果您这样做的唯一原因是为了以后在代码中使用,请不要将其放在文本框中 您得到上述错误是因为lessonIDTextbox中没有值(基于您向我们展示的代码)
id
已经有一个值,您可以稍后在代码中使用它。如果您试图在回发或其他内容中保留该值,那么您可能需要探索其他选项
如果有其他原因需要将其放在文本框中,您可以这样做:
lessonIDTextbox.Text=id.ToString();
lessonIDTextbox.Text=id.ToString();