Solved

Update Data

Posted on 2012-03-25
6
325 Views
Last Modified: 2012-08-13
How to correct this command of C#?

public IQuery<student> GetStudentAge(int b)
{
    return this objectContext.students.???
}

What I want is to update all StudentAge to become the value of b. My table is student with the column StudentAge...
0
Comment
Question by:Whing Dela Cruz
  • 2
  • 2
  • 2
6 Comments
 
LVL 8

Assisted Solution

by:Eric Flamm
Eric Flamm earned 334 total points
ID: 37762830
Can you provide some additional context - is this in the code-behind of a page or a method in a class? Can you post the full code to enable some testing and review?
0
 

Author Comment

by:Whing Dela Cruz
ID: 37762856
Hi! What I'm trying to do is make some adjustment of the existing code below where it is all working only in single way of action. I want to update the data of the database in 1 click where the Whole age of the student will be updated... My XAML code are as follows
 
        private void btnupdate_Click(object sender, RoutedEventArgs e)
        {
            objctx = new StudentDomainContext();
            var studentid = int.Parse(searchstudentName.Text);
            var query = objctx.GetStudentsByIdQuery(studentid);
            objctx.Load(query, EDitData, null);
        }

        private void EDitData(LoadOperation<student> lo)
        {
            student st = lo.Entities.First();
            st.StudentName = txtstudentName.Text;
            st.StudentAge = int.Parse(txtstudentage.Text);
            try
            {
                objctx.SubmitChanges();
                MessageBox.Show("Data updated successfully!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Data updation failed due to " + ex.Message);
            }
        }

And the class are as follows;

  public IQueryable<student> GetStudentsById(int id)
        {
            return this.ObjectContext.students.Where(s=> s.ID == id);
        }

the code is working... But i want to update the data base of this code. Please tell me if this is possible? Thanks!
0
 
LVL 14

Assisted Solution

by:nishant joshi
nishant joshi earned 166 total points
ID: 37763083
use commit and savechanges methods.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 14

Expert Comment

by:nishant joshi
ID: 37763087
after that use update method of table adapter and call update method.
0
 
LVL 8

Accepted Solution

by:
Eric Flamm earned 334 total points
ID: 37764332
It seems to me that your EditData method already updates the database, changing the student name and the student age based on the values in the respective text boxes.
objctx.SubmitChanges();

Open in new window

should be sufficient to save the changes, provided your StudentDomainContext object is configured correctly.

What are you trying to accomplish that your current application doesn't do?
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 37791895
Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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