Solved

Update Data

Posted on 2012-03-25
6
328 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.

739 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