我正在尝试传递 Drop1.text (DropDownList) 的 selectedItem 以保存在 Emp_Name 字段中的 SQL 表 (TimeSheetDtls) 中,目前,下面的代码不起作用。当我单击按钮(button1_click 已添加到按钮)时,页面会发回,但不会更新表格或写出感谢声明。错误在哪里?
protected void button1_Click(object sender, EventArgs e)
{
string name = Drop1.Text; // Scrub user data
string connString = ConfigurationManager.ConnectionStrings["TimesheetDtlsConnectionString1"].ConnectionString;
SqlConnection Conn = null;
try
{
Conn = new SqlConnection(connString);
Conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = Conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO TimeSheetDtls VALUES (@Emp_Name)";
cmd.Parameters.AddWithValue("@Emp_Name", Drop1.SelectedItem);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected == 1)
{
Response.Write("Thank you very much, " + Drop1.SelectedItem); //Success notification
}
else
{
Response.Write("There was an error, please try again."); //Error notification
}
}
}
catch (Exception ex)
{
//log error
//display friendly error to user
}
finally
{
if ( Conn != null)
{
Conn.Close(); //cleanup connection i.e close
}
}
}