when i select any item in Dropdown box, and click submit button then selected item jumps to the first item in dropdown box and also it shows mey the value of first item, why ? why doesn't it keep the item that i select , ?
Code:
public partial class EmployerSubmitScrutinyMembers : System.Web.UI.Page
{
String connectionString = "Data Source=COSANOSTRA; MultipleActiveResultSets=true; Initial Catalog=Waleed_orsfinal;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connectionString);
String sqlQuery1 = "select * from tblScruitnyTeam";
SqlCommand com = new SqlCommand(sqlQuery1, con);
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery1, con);
DataTable dt = new DataTable();
try
{
con.Open();
adapter.Fill(dt);
drpdwnTeam.DataSource = dt;
drpdwnTeam.DataTextField = "name";
drpdwnTeam.DataValueField = "teamid";
drpdwnTeam.DataBind();
}
catch (Exception excep)
{
Response.Write("Error:" + excep.Message);
}
finally
{
con.Close();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con1 = new SqlConnection(connectionString);
String sqlQuery2 = "insert into tblMembers values ('"+txtboxMemberName.Text+"','"+txtboxRole.Text+"','"+txtboxMemberSignature.Text +"','"+txtboxMemberDetails.Text+"')";
SqlCommand com = new SqlCommand(sqlQuery2, con1);
SqlCommand com1 = new SqlCommand("returnMemberID", con1);
com1.CommandType = CommandType.StoredProcedure;
com1.Parameters.Add("@memberId", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
try
{
con1.Open();
com.ExecuteNonQuery();
com1.ExecuteNonQuery();
int memberId = (int)com1.Parameters["@memberId"].Value;
Response.Write(memberId);
SqlCommand com2 = new SqlCommand("insert into tblTeamMembers values('"+memberId+"',16)",con1);
com2.ExecuteNonQuery();
Response.Write("DDB= "+ drpdwnTeam.SelectedValue);
}
catch (Exception ex)
{
Response.Write("ERROR: " + ex.Message);
}
finally
{
con1.Close();
}
}
}
Hunain Hafee...
Member
238 Points
639 Posts
dropdown box value changes
Dec 23, 2012 10:00 PM|LINK
when i select any item in Dropdown box, and click submit button then selected item jumps to the first item in dropdown box and also it shows mey the value of first item, why ? why doesn't it keep the item that i select , ?
Code:
public partial class EmployerSubmitScrutinyMembers : System.Web.UI.Page { String connectionString = "Data Source=COSANOSTRA; MultipleActiveResultSets=true; Initial Catalog=Waleed_orsfinal;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(connectionString); String sqlQuery1 = "select * from tblScruitnyTeam"; SqlCommand com = new SqlCommand(sqlQuery1, con); SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery1, con); DataTable dt = new DataTable(); try { con.Open(); adapter.Fill(dt); drpdwnTeam.DataSource = dt; drpdwnTeam.DataTextField = "name"; drpdwnTeam.DataValueField = "teamid"; drpdwnTeam.DataBind(); } catch (Exception excep) { Response.Write("Error:" + excep.Message); } finally { con.Close(); } } protected void btnSubmit_Click(object sender, EventArgs e) { SqlConnection con1 = new SqlConnection(connectionString); String sqlQuery2 = "insert into tblMembers values ('"+txtboxMemberName.Text+"','"+txtboxRole.Text+"','"+txtboxMemberSignature.Text +"','"+txtboxMemberDetails.Text+"')"; SqlCommand com = new SqlCommand(sqlQuery2, con1); SqlCommand com1 = new SqlCommand("returnMemberID", con1); com1.CommandType = CommandType.StoredProcedure; com1.Parameters.Add("@memberId", SqlDbType.Int).Direction = ParameterDirection.ReturnValue; try { con1.Open(); com.ExecuteNonQuery(); com1.ExecuteNonQuery(); int memberId = (int)com1.Parameters["@memberId"].Value; Response.Write(memberId); SqlCommand com2 = new SqlCommand("insert into tblTeamMembers values('"+memberId+"',16)",con1); com2.ExecuteNonQuery(); Response.Write("DDB= "+ drpdwnTeam.SelectedValue); } catch (Exception ex) { Response.Write("ERROR: " + ex.Message); } finally { con1.Close(); } } }help !
gregaDro
Member
318 Points
65 Posts
Re: dropdown box value changes
Dec 23, 2012 10:06 PM|LINK
Because you bind it every time on postback.
try { if (!Page.IsPostBack) { con.Open(); adapter.Fill(dt); drpdwnTeam.DataSource = dt; drpdwnTeam.DataTextField = "name"; drpdwnTeam.DataValueField = "teamid"; drpdwnTeam.DataBind(); } }