Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete All

Posted on 2012-03-26
7
Medium Priority
?
280 Views
Last Modified: 2012-04-05
How to delete all data in sql in one click?
This is most likely related with my pending question which is now still searching for the solution. The XAML.cs here are as follows

StudentDomainContext objctx;

private void btndelete_Click(object sender, RoutedEventArgs e)
{
objctx = new StudentDomainContext();
var query = objctx.GetStudentsQuery();
objctx.Load(query, Datadelete, null);
}

private void Datadelete(LoadOperation<student> lo)
{
student st = lo.Entities.First();
objctx.student.Remove(st);
try
{
objctx.SubmitChanges();
MessageBox.Show("Data deleted!")
}
catch (Exception ex)
{
MessageBox.Show("Data deletion failed" +ex.Message);
}

}

The Class query are as follos......
public IQueryable<student> GetStudents()
{
return this.ObjectContext.students;
}

The codes are working but when I execute, only one data will be deleted. What I want here is to delete all datas inside my database concerning student Table. Please Help!
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
  • 4
  • 3
7 Comments
 
LVL 15

Assisted Solution

by:angus_young_acdc
angus_young_acdc earned 1000 total points
ID: 37765245
It is because you are only selecting the first value on this line: student st = lo.Entities.First();

Try changing it to lo.Entities.All();
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 37765249
Or change the query to DELETE FROM [YourStudentTableName]

If there is no Where clause then it will delete everything in that table.
0
 

Author Comment

by:Whing Dela Cruz
ID: 37765317
Hi! I tried this lo.Entities.All(); but it doesn't work! Can you provide me how to change the query?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 37765321
var query = objctx.GetStudentsQuery();


The above line should be returning your query (from what I can work out from the amount of code), within the GetStudentsQuery method you should see what is happening and be able to update it accordingly.
0
 

Author Comment

by:Whing Dela Cruz
ID: 37766572
Hi! I'm trying but it does'nt work...
0
 

Accepted Solution

by:
Whing Dela Cruz earned 0 total points
ID: 37769624
Hi! Everyone, What I did here is to put something Loop... But I dought if it is good I dea because I feel that there's something delay of the Looping. Any Idea Please....

private void Datadelete(LoadOperation<student> lo)
{
foreach (student st in lo.Entities)
objctx.student.Remove(st);
try
{
objctx.SubmitChanges();
MessageBox.Show("Data deleted!")
}
catch (Exception ex)
{
MessageBox.Show("Data deletion failed" +ex.Message);
}

}

 Please tell me if there is another solution aside from this....
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 37810117
Thanks!
0

Featured Post

[Webinar] Protection from Cyberattacks

In this session, we’ll dive into the complexities of modern cyber threats and why only multi-vector protection can keep today’s businesses secure through the various stages of a cyberattack, across multiple vectors. Thursday September 14, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

730 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