Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am accessing values from the database to fill the drop down list.
now when i click on the show button or if second way i select the index of the list. the page becomes auto refresh and the first value of the list selects auto. the result displayed only for the first value in the drop down list each time. how i could solve it......
C#
protected void ddlNames_SelectedIndexChanged(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        con = new SqlConnection("Data Source=KADSERVER;Initial Catalog=Kadsyscon_temp;User ID=sa;Password=syscon1234");
        con.Open();
        //DataSet ds = new DataSet();
        dtLogin = new DataTable();
        string str1 = "select Date,InTime ,OutTime from EmpAttendanceDetails WHERE EID=" + ddlNames.SelectedValue.ToString();
        Command = new SqlCommand(str1, con);
        Command.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter(Command);
        da.Fill(dtLogin);

        gvDetails.DataSource = dtLogin;
        gvDetails.DataBind();
    }
}

this is the code i have written. and i used the same code on the button click but the problem persists....
Posted
Updated 13-Feb-12 5:24am
v2

The fact you use the same code twice, is terrible design, in fact, all of this is bad. You call the DB every time someone clicks a button. Why do you need to do that ? You basically reset the list every time someone selects something, which will, of course, reset the selection.
 
Share this answer
 
Comments
ChandanBadgujar 13-Feb-12 23:59pm    
yes, but i have used code twice one by one not at a time. then which code should i eliminate now for autorefresh. someone told me to use viewstate, how is it, will it work???
Al Moje 14-Feb-12 0:34am    
Maybe you could consider viewstate, but remember how about when your page has post back?
When page having post back hence, viewstate value becomes null...
I have just got my mistake. I was using the "!ispostback" on the selectedindexchanged of dropdownlist and also tried on show button click. but then i used it on page load at the time of filling the dropdown list. and it solved my problem. now the drop down list do not refreshes and remains on the index whichever is selected. thanks for all to help me!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900