views:

87

answers:

3

I'm using following DropDownList event to select an employee from MS SQL Server 2005 and showing the employee's information on TextBox.

protected void employeeDropDownList_SelectedIndexChanged(object sender,
                                                                EventArgs e)
{
   EmployeeDAL employeeDAL = new EmployeeDAL();
   DataTable dataTable = employeeDAL.GetEmployeeData();
   for (int i = 1; i <= dataTable.Rows.Count; i++)
    {
      if (Convert.ToInt32(employeeDropDownList.SelectedValue) == i)
      {
        nameTextBox.Text = dataTable.Rows[i-1]["employee_Name"].ToString();
        useNameTextBox.Text=dataTable.Rows[i-1]["employee_UserName"].ToString();
        addressTextBox.Text=dataTable.Rows[i-1]["employee_Address"].ToString();
        break;
       }
     }
 }

Then I'm using the following Button event to update Employee information.

protected void employeeUpdateButton_Click(object sender, EventArgs e)
{
   EmployeeDAL employeeDAL = new EmployeeDAL();
   EmployeeDAO employeeDAO = new EmployeeDAO
                         {
                      EmployeeID = Convert
                                  .ToInt32(employeeDropDownList.SelectedValue),
                      Name = nameTextBox.Text,
                      Username = useNameTextBox.Text,
                      Address = addressTextBox.Text,
                         };

        employeeDAL.UpdateEmployee(employeeDAO);
 }

But the problem is... values of the TextBoxes are not changing. I mean TextBoxes are keeping previous values those were assigned in employeeDropDownList_SelectedIndexChanged event. But why? What should I do now?

A: 

The button click is being executed before the text fields have been updated, put a break point in both eventhandlers. Put a watch on the textbox and on you dao object and watch how they are updated when yhou post back. I really don't think it is a data issue but do not totally discount it, just seems, to me at least, an asp.net issue with caching the textbox values between postbacks.

Monkieboy
I didn't use any Text_Changed event. So how I will put a break point in Text_Chenged event? How I will call Text_Changed event?
I didn't change anything in ViewState. And I don't have postback setting in TextBox. Thanks.
I dont think its necessary to use onTextChange event here. Because I'm clicking on a Button to assign the textBox text into the property.
Sorry, I have been working for 26 hours without a break, I will edit my answer referencing the correct event handlers.
Monkieboy
Thank you very much for your patience.
I am convinced you have a viewstate issue - try disabling viewstate and see what happens.
Monkieboy
No thank you - many would have marked me down for not reading the question properly
Monkieboy
<%@ Page EnableViewState="true" Language="C#" AutoEventWireup="true" %> I use this code for the Viewstate.
26 Hours... Its a long time friend.
change your text box by setting the viewstate attribute to false
Monkieboy
<asp:textbox enableviewstate="false" .... /> I think I have the attribute called correct, but intell!sense should help
Monkieboy
Why should I set EnableViewState false? Though I tried with it, but it doesn't work.
Just wanted to make sure that the controls value was not being saved in the viewstate. When you step through the code are you able to notice which order the methods are being called, they might be correct in their function but the order they are executed might not be.
Monkieboy
A: 

Well, since we don't have the implementation of your EmployeeDAL class, it's a little hard to know exactly what the problem is. Here are a few things I would check:

  1. Make sure the GetEmployeeData funtion doesn't implement some caching internally.
  2. Check the UpdateEmployee function to verify that it does actually commit to the database. Sometimes update functions only change things in memory and there is a different function to actually commit those changes to the store.
  3. In your employeeUpdateButton_Click function, double check the value you are setting to the EmployeeID property.
  4. Run SQL Profiler to see exactly what T-SQL is hitting the database.
Russell McClure
GetEmployeeData() funtion is working properly and showing data on textBoxes. UpdateEmployee() function working properly too. I debugged them. In employeeUpdateButton_Click event EmployeeID value is taken properly. In textBoxes values are not updating so database is updating with previous value.
Do you have ViewState enabled on your TextBoxes?
Russell McClure
I'he EnableViewState = "True" for the page. Thanks
<%@ Page EnableViewState="true" Language="C#" AutoEventWireup="true" %>
I use this code for the Viewstate.
Okay, but individual controls can opt out by overriding the page level setting. So you should double check your controls. Maybe you could add the ASPX definition of your TextBoxes to your question.
Russell McClure
If ViewState is working fine, then my next guess would be that you have some code that is setting the value of the TextBoxes back to the original values during the button click post back but BEFORE the employeeUpdateButton_Click function gets its turn to execute.
Russell McClure
TextBoxes values are never changing after the employeeDropDownList_SelectedIndexChanged event. And thats the problem.
A: 

Ok finally I got the answer but can't explain it. For the DropdownList I used the following code:

and the main culprit was this OnLoad event. When I'm running the application without this OnLoad event then my problem is solved. Could anyone explain what was wrong with this OnLoad event?

I missed the code section, but giving here<asp:DropDownList ID="employeeDropDownList" runat="server" AutoPostBack="true" OnLoad="employeeDropDownList_SelectedIndexChanged" onselectedindexchanged="employeeDropDownList_SelectedIndexChanged"> </asp:DropDownList>
Right. That's exactly what I was saying in my last comment (underneath my answer). During the post back something WAS running BEFORE the employeeUpdateButton_Click. The OnLoad event runs first so that was wiping out the new values input by the user. After the OnLoad ran, then your employeeUpdateButton_Click ran but by then it was too late. The new values had already been wiped out. You do need to initialize your DropDownList but you should not do it in the same function and you need to wrap it in a "if (!Page.IsPostBack) {}".
Russell McClure
OnLoad="employeeDropDownList_SelectedIndexChanged" why are you calling employeeDropDownList_SelectedIndexChanged method on OnLoad event?
AsifQadri
Good question. I do this because at first when page is loaded the TextBoxes don't show any employee information(but TextBoxes show corresponding employee information when selected index is changed). So, to show the employee information when page is appeared I've done this.