Click here to Skip to main content
15,867,568 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"

C#
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 6:34am
v5
Comments
Sergey Alexandrovich Kryukov 12-May-16 9:08am    
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
Sergey Alexandrovich Kryukov 13-May-16 8:56am    
I see no information on what you call "error"...
—SA

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):
C#
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;
       }
 
Share this answer
 
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
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.
 
Share this answer
 
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);
}
In your case, you needto use list.Remove(lg) not list.RemoveAll(lg).



C#
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;
       }
 
Share this answer
 
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);
}
 
Share this answer
 

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