<select önchange="getCountry('country');" id="country" name = "country"><select>
function getCountry(x)
{
var y=document.getElementById(x).value;
document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML =y;
}
function load()
{
document.getElementById('country').value = document.getElementById('ctl00_ContentPlaceHolder1_lblCountry').innerHTML;
}
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Data Source = .\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM [product] where ID = '1'";
conn.Open();
System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
this.lblCountry.Text = dr["country"].ToString();
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
string connectionString = "Data Source = .\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security = True;Connect Timeout=30;User Instance = True";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
cmd.Parameters.Add("country", SqlDbType.VarChar);
cmd.Parameters["country"].Value = this.lblCountry.Text;
cmd.CommandText = "UPDATE [product] SET [country] = @country WHERE [ID] = '1'";
conn.Open();
int numberOfRows = cmd.ExecuteNonQuery();
conn.Close();
}
1. On page load, the SELECT dropdownlist is show 'Japan', so as lblCountry.
2. If i change the Item from 'Japan' to 'Korea', lblCountry also will update to 'Korea'.
3. btnUpdate been click, page will load again, but it stills show 'Japan' instead of 'Korea'. Check at database, it still remains as 'Japan'. The same code/query been used on asp:dropdownlist and it works as expected.
Any advice for this question please? :)