Solved

How do I use the OnItemUpdating event of a listview?

Posted on 2009-07-01
5
660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

696 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