Click here to Skip to main content
14,690,512 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more: , +
i have created list and also created binary file called "login.bim"

private void btnDelete_Click(object sender, EventArgs e)
       {
          List<log> list = null;

           Log lg = new Log();
           lg.Username = this.textBox1.Text;
           lg.Password = this.textBox2.Text;
           lg.Name = this.txtname.Text;
           lg.Contact = Convert.ToInt32(this.txtContact_no.Text); ;
           lg.Email = this.txtEmail_Address.Text;


           list.RemoveAll(lg);

           Stream stream = File.Open("Login.bin", FileMode.Open);
           BinaryFormatter bformatter = new BinaryFormatter();
           list = (List<log>)bformatter.Deserialize(stream);
           stream.Close();

           {
               MessageBox.Show(" details has been deleted!", "Success");
               Reset();
           }

           dtvregister.DataSource = list;
       }


public List<Log> loadData()
       {
           List<Log> lst = null;
           if (File.Exists("Login.bin"))
           {
               Stream stream = File.Open("Login.bin", FileMode.Open);
               BinaryFormatter bformatter = new BinaryFormatter();
               lst = (List<Log>)bformatter.Deserialize(stream);
               stream.Close();


           }
           else
           {
               lst = new List<Log>();


           }
           return lst;
       }


What I have tried:

I have created list and also created binary file called "login.bin"..when i remove from list ,the error displays on screen. why? how to remove inside list details? i have used datagridview also...thank you

Please refer the Edited details

Error 2 Argument 1: cannot convert from 'XYZ_System.Log' to 'System.Predicate<xyz_system.log>'

Error 1 The best overloaded method match for 'System.Collections.Generic.List<xyz_system.log>.RemoveAll(System.Predicate<xyz_system.log>)' has some invalid arguments
Posted
Updated 14-May-16 7:34am
v5
Comments
   
Debugger!

You are asking for help and still give no information on the "error". Hm...

—SA
Rifath apps 12-May-16 9:37am
   
the probelm n this line... list.RemoveAll(lg);
Sergey Alexandrovich Kryukov 12-May-16 10:42am
   
And still no exception information...
—SA
Rifath apps 13-May-16 4:09am
   
please refer edited details..thank you
   
I see no information on what you call "error"...
—SA
Rifath apps 13-May-16 9:10am
   
Error 2 Argument 1: cannot convert from 'XYZ_System.Log' to 'System.Predicate'

Error 1 The best overloaded method match for 'System.Collections.Generic.List.RemoveAll(System.Predicate)' has some invalid arguments
Sergey Alexandrovich Kryukov 13-May-16 17:22pm
   
In what line(s)? Anyway, everything is clear.
1. Of course, invalid. Look at the profile of this method, it clearly tells you what should be the argument.
2. Of course, they are incompatible types. Read documentation on System.Predicate, and you will see what is it.

Look, you did not report any single problem. What you show is not a problem, except, perhaps, you inability to read and understand the documentation or even reflected declarations, but in this case nothing can help you except learning.

There is nothing to answer to...

—SA
Rifath apps 14-May-16 12:32pm
   
yes,,,Finally i correct it successfully,, thank you..again..
Sergey Alexandrovich Kryukov 14-May-16 13:48pm
   
You are very welcome.

Please try to provide comprehensive description of the problem from the very beginning, don't make people pulling information from you. In case of exceptions and compilation errors, you can comment the line in question and refer to such comment in the description of the problem.

—SA
Rifath apps 13-May-16 8:54am
   
No errors found. but is not deleting from list.. why?
   
Read your own post carefully: "the error displays on screen".
—SA
Rifath apps 13-May-16 9:10am
   
i have given the errors above

Error 1 The best overloaded method match for 'System.Collections.Generic.List<xyz_system.log>.RemoveAll(System.Predicate<xyz_system.log>)' has some invalid arguments
Dmitriy Gakh 12-May-16 9:32am
   
You should create your list first. Your list is null when you call RemoveAl.

List<log> list = null;
...
list.RemoveAll(lg);

And after debug your code and fix logical problems using debugger.
Rifath apps 12-May-16 9:37am
   
can u give me full code. itz big help to me. because still on error..thank u
Rifath apps 13-May-16 4:09am
   
please refer edited one.. thank you
Rocker-Star 12-May-16 13:59pm
   
Looking at your code, you have not instantiated the list.

Create the list on the first line List<log> = new List(). That will create a new list and you do not need a remove all call, as the list is already empty.
Rifath apps 13-May-16 4:07am
   
please refer the edited code.thank you
Richard Deeming 13-May-16 10:01am
   
You already have the solution; you just need to combine the advice from the three solutions and the comments:

1) Create the List<Log> instance before you try to call a method on it;
2) Use list.Remove(lg) instead of list.RemoveAll(lg);
3) If it still doesn't work, use the debugger.

I have no enough data about your solution and logic, but this code seems working without runtime errors (if it does not resolve your problem completely, it could be helpful to improve the situation and understand the code better):
private void btnDelete_Click(object sender, EventArgs e)
       {
           Log lg = new Log();
           lg.Username = this.textBox1.Text;
           lg.Password = this.textBox2.Text;
           lg.Name = this.txtname.Text;
           lg.Contact = Convert.ToInt32(this.txtContact_no.Text);
           lg.Email = this.txtEmail_Address.Text;
 
           Stream stream = File.Open("Login.bin", FileMode.Open);
           BinaryFormatter bformatter = new BinaryFormatter();
           List<Log> list = (List<Log>)bformatter.Deserialize(stream);
           stream.Close();
 
           // Try remove commands here

           {
               MessageBox.Show(" details has been deleted!", "Success");
               Reset();
           }
 
           dtvregister.DataSource = list;
       }
   
v4
Comments
Rifath apps 13-May-16 6:23am
   
you gave me some code. where is the removing option ?
Dmitriy Gakh 13-May-16 7:02am
   
You could put remove commands in place commented as "Try remove commands here". Try do different options and see results. Hope it will be helpful.
Rifath apps 13-May-16 8:07am
   
Error 1 The type or namespace name 'log' could not be found (are you missing a using directive or an assembly reference?)
Rifath apps 13-May-16 8:10am
   
Yes i tried your code itz working fine.. the problem is not deleting from Datagridview.. why?
Rifath apps 13-May-16 8:11am
   
The dataGridView showing details...old details.. no any changes in datagridview
Dmitriy Gakh 13-May-16 9:47am
   
It is new question :-) You asked about unexpected error. The error is fixed. Let's do next step. I think you should better understand what you should delete. I think you should delete an item in the your list in place I commented for deleting code.
Rifath apps 13-May-16 10:02am
   
yes. your correct.. may i want to delete from loadData also.. so the loadData already stored user details.. so delete from list and also loadData
Rifath apps 13-May-16 10:02am
   
IS it possible ?>
Dmitriy Gakh 13-May-16 10:15am
   
I think call

dtvregister.DataSource = list;

deletes all in dtvregister and take new data from the list. So, you should delete entry only in the list, should not ?
Dmitriy Gakh 13-May-16 9:39am
   
log should be replaced by Log.
Rifath apps 13-May-16 9:44am
   
solution ?
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Quote:
when i remove from list ,the error displays on screen. why?
impossible to answer. the reason is explained in the error message you didn't post.
Asking questions is a skill[^]

When you will debug your code, pay attention to list, I see where you want to remove things, but not what you put in list.
GIVE THE ERROR MESSAGE !

[Update]
Quote:
I have created list
No what I see; You created a list variable name of type list, but NULL is not initialization.
Second problem, before removing things from a list, you must start by putting something inside the list.
   
v5
Comments
Dmitriy Gakh 13-May-16 5:25am
   
You are right. I tech my students to use debugger as soon as they started coding. Step by step operations show many details.
But here people even did not read comments before acting...
Rifath apps 13-May-16 6:22am
   
yes i try to learn about it... thank you..
Dmitriy Gakh 13-May-16 6:54am
   
You are in right way. :-)
Patrice T 13-May-16 8:32am
   
The first step on the right way will be when he will eventually give us the error message.
Rifath apps 13-May-16 8:48am
   
i have insert some modify.. and this error message came ( Rows cannot be programmatically removed unless the DataGridView is data-bound to an IBindingList that supports change notification and allows deletion.)

for (int i = dtvregister.SelectedCells.Count - 1; i >= 0; i--)
{
dtvregister.Rows.RemoveAt(dtvregister.SelectedCells[i].RowIndex);
}
Patrice T 13-May-16 8:55am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Update the code too since the error message is related changes in code.
Rifath apps 13-May-16 9:10am
   
i have given the errors above
In your case, you needto use list.Remove(lg) not list.RemoveAll(lg).



private void btnDelete_Click(object sender, EventArgs e)
       {
          List<log> list = null;
            Log lg = new Log();
           lg.Username = this.textBox1.Text;
           lg.Password = this.textBox2.Text;
           lg.Name = this.txtname.Text;
           lg.Contact = Convert.ToInt32(this.txtContact_no.Text); ;
           lg.Email = this.txtEmail_Address.Text;

           list.Remove(lg);
 
           Stream stream = File.Open("Login.bin", FileMode.Open);
           BinaryFormatter bformatter = new BinaryFormatter();
           list = (List<log>)bformatter.Deserialize(stream);
           stream.Close();
 
           {
               MessageBox.Show(" details has been deleted!", "Success");
               Reset();
           }
 
           dtvregister.DataSource = list;
       }
   
v3
Comments
Rifath apps 13-May-16 9:18am
   
No.. not working list.Remove(lg);
Richard Deeming 13-May-16 10:03am
   
You need to create the List<Log> instance before you can call a method on it - see Solution 6.

But you're correct in saying that the OP needs to use Remove instead of RemoveAll.
Rifath apps 14-May-16 12:35pm
   
final answer

private void btnDelete_Click(object sender, EventArgs e)
{

bool flag = false;
int x = 0;
Log lecObj = new Log();
lecObj.Username = txtname.Text;

for (; x < list.Count; x++)
{
lecObj = list[x];
if (lecObj.Username == txtname.Text);
{
flag = true;
break;
}
}
list.RemoveAt(dtvregister.SelectedRows[x].Index);
Stream stream = File.Open("Login.bin", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, list);
stream.Close();

btnGetDetails_Click(sender,e);
}
The final Answer,, Itz working fine... please refer this code if any one has doubt or itz will use full some one

private void btnDelete_Click(object sender, EventArgs e)
{

bool flag = false;
int x = 0;
Log lecObj = new Log();
lecObj.Username = txtname.Text;

for (; x < list.Count; x++)
{
lecObj = list[x];
if (lecObj.Username == txtname.Text);
{
flag = true;
break;
}
}
list.RemoveAt(dtvregister.SelectedRows[x].Index);
Stream stream = File.Open("Login.bin", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, list);
stream.Close();

btnGetDetails_Click(sender,e);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900