Solved

How do I use the OnItemUpdating event of a listview?

Posted on 2009-07-01
5
665 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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