Solved

How do I use the OnItemUpdating event of a listview?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

17 Experts available now in Live!

Get 1:1 Help Now