我在从网络表单更新我的数据库时遇到问题。我使用了一系列由数据源填充的文本框和下拉列表,但由于某种原因我无法更新它们。
基本上,一行的主键被传递到下一页,其中的文本框、下拉列表填充了该行的数据。
这是我迄今为止尝试过的
//Update the record with new information entered into controls
protected void UpdateBut_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=stephenp\\sqlexpress;Initial Catalog=Asset management System DB;Integrated Security=True"))
{
string sql = "UPDATE Peripherals SET PeripheralType=@PeripheralType, Model=@Model, PeripheralSerialNumber=@SerialNum, " +
"Company = @Company, Department=@Department, Status=@Status, WarrantyExpirationDate=@Warranty, PeripheralCapexNumber=@CapexNum, " +
"IPAddress=@IPAddress, LastModifiedDate = @LastMD, LastModifiedBy=@LastMB WHERE (PeripheralTagNumber = @PeripheralTagNumber)";
//try
//{
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@PeripheralType", TypeDDL.Text.Trim());
cmd.Parameters.AddWithValue("@Model", ModelTB.Text.Trim());
cmd.Parameters.AddWithValue("@SerialNum", SerialNumTB.Text.Trim());
cmd.Parameters.AddWithValue("@Company", CompanyDDL.Text.Trim());
cmd.Parameters.AddWithValue("@Department", DepartmentDDL.Text.Trim());
cmd.Parameters.AddWithValue("@Status", StatusDDL.Text.Trim());
cmd.Parameters.AddWithValue("@Warranty", DateTime.Parse(WarrantyTB.Text.Trim()).ToShortDateString());
cmd.Parameters.AddWithValue("@CapexNum", CapexNumTB.Text.Trim());
cmd.Parameters.AddWithValue("@IPAddress", IPAddressTB.Text.Trim());
cmd.Parameters.AddWithValue("@LastMD", DateTime.Now.Date.ToShortDateString());
cmd.Parameters.AddWithValue("@LastMB", Session["username"].ToString());
cmd.Parameters.AddWithValue("@PeripheralTagNumber", ID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
//}
//catch (SqlException ex) { }
}
this.getData();
}
谢谢