Solved

How do I use the OnItemUpdating event of a listview?

Posted on 2009-07-01
5
653 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now