Solved

How do I use the OnItemUpdating event of a listview?

Posted on 2009-07-01
5
659 Views
Last Modified: 2013-12-17
I have written the attached code within the OnItemUpdating event of my listview control but I can't seem to be able to update my backend sql server 2005 database with the users chosen e.NewValues?

Thanks.
protected void lvInterviewers_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        HRsystemDataContext db = new HRsystemDataContext();
 
        DropDownList ddlEditInterviewerID = (DropDownList)lvInterviewers.Items[e.ItemIndex].FindControl("InterviewerEditDDL");
 
        e.OldValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
 
        if (ddlEditInterviewerID.SelectedValue != "20")
        {
            e.NewValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
        }
 
        db.SubmitChanges();
    }

Open in new window

0
Comment
Question by:Shepwedd
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Cyber-spy
ID: 24753230
You should be using the ItemUpdated event - not the ItemUpdating.

ItemUpdating occurs when an Update has been requested, but before the control updates the item.
ItemUpdated however occurs AFTER the control has updated the item
void lvInterviewers_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
        HRsystemDataContext db = new HRsystemDataContext();
 
        DropDownList ddlEditInterviewerID = (DropDownList)lvInterviewers.Items[e.ItemIndex].FindControl("InterviewerEditDDL");
 
        e.OldValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
 
        if (ddlEditInterviewerID.SelectedValue != "20")
        {
            e.NewValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
        }
 
        db.SubmitChanges();}

Open in new window

0
 
LVL 12

Expert Comment

by:Cyber-spy
ID: 24753251
Also, how are you passing the new values to your db.SubmitChanges() method?

The ListViewUpdatedEventArgs e only exists in the context of this method - SubmitChanges can't see this object.

Does db have any properties to set the new value before you call SubmitChanges?
This would much more likely succeed than changing the ListViewUpdatedEventArgs, which is there to give you the new values - it doesn't need them setting!
0
 

Author Comment

by:Shepwedd
ID: 24754194
Cyber-spy,

As you suggested I added the attached code but I still get the same result - no update seems to occur? The errors are getting flagged but no update is happening?

In regards to my db setting, I was hoping to set my drop downs selectedvalue by using the below code:

       HRsystemDataContext db = new HRsystemDataContext();

        var query = from aaii in db.ApplicantInterviewInterviewers
        where aaii.InterviewID == Convert.ToInt32(Request.QueryString["InterviewID"])
        select new { aaii.InterviewerID };

        DropDownList ddlEditInterviewerID = (DropDownList)lvInterviewers.EditItem.FindControl("InterviewerEditDDL");
        ddlEditInterviewerID.SelectedValue = query.FirstOrDefault().InterviewerID.ToString();

and then perform the update by:

if (ddlEditInterviewerID.SelectedValue != "20")
        {
            //e.NewValues["InterviewID"] = lbInterviewID.Text;
            e.NewValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;

            //e.NewValues.Add("22", (ddlEditInterviewerID).SelectedValue);
        }

        db.SubmitChanges();

But this didn't work either.
protected void lvInterviewers_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
    {
        DropDownList ddlEditInterviewerID = (DropDownList)lvInterviewers.EditItem.FindControl("InterviewerEditDDL");
 
        e.OldValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
 
        if (ddlEditInterviewerID.SelectedValue != "20")
        {
            e.NewValues["InterviewerID"] = ddlEditInterviewerID.SelectedValue;
        }
    }

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24754231
Are you sure it's not the updating event as surely I'm wanting to set the NewValues before the actual update?

Thanks.
0
 
LVL 12

Accepted Solution

by:
Cyber-spy earned 500 total points
ID: 24761490
Shepwedd,

There are several problems here.

First of all, there is no point setting values in the ListViewUpdatedEventArgs e in the event handler.
This object is there to pass data to you, so you can deal with it.

Depending on the EventArgs object, you can either use data FROM e or you can get the selected values direct from the controls.

i.e. e.NewValues["InterviewerID"] and ddlEditInterviewerID.SelectedValue are probably already the same value.
You only need the 'OldValues' if you need to compare the the two values and take some action based on that comparison. If you just want to update a database based on the currently selected value, then the OldValues probably aren't much use.

Secondly, all your setting of e.NewValues and e.OldValues stay inside this event handler. When you call db.SubmitChanges(), this method has no knowledge of what you have just done inside the event handler.
 
I don't know anything about your db object, but you need to tell it about the change in InterviewerID, before you submit the changes to the database.

I've posted an example below - you will probably need to change this, based on the actual db object. If you need help with this, you'll need to post a whole lot more code, such as the class that defines the db object.

The question about how to preselect your DDL based upon a QueryString variable is a separate question.
protected void lvInterviewers_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
    {
        DropDownList ddlEditInterviewerID = (DropDownList)lvInterviewers.EditItem.FindControl("InterviewerEditDDL");
 
 
        db.InterviewerID = ddlEditInterviewerID.SelectedValue;
        
        db.SubmitChanges()
 
    }

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question