Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys

I have a program that updates an Audit Trail (a listview) when something happens, like a user login, create/delete a new user and so on.
The listview has 10 columns, with columns like: Date, Time, Message Description, Event type and so on.
I need to store each listview entry (subItems of a raw) in an SQL Database Whenever i update the listView with new subItems. I couldn't find a listView ItemsChanged event, so I used the ItemSelectionChanged event. But how can i copy these subItems of an inserted row into the database at each ItemSelectionChanged event? How can i get the contents of each subItems of a row so that i can copy them into a database?

Please help
Posted
Updated 2-Jul-14 23:22pm
v2
Comments
Marcin Kozub 3-Jul-14 6:35am    
What technology? Winforms, WPF, ASP.NET?
katela 4-Jul-14 1:23am    
I am using Winforms
ZurdoDev 3-Jul-14 8:12am    
Where are you stuck? Just get the values and write them into Sql. This is trivial.
Sneha_10 4-Jul-14 8:37am    
How do you update your listview? Do you save your data in some variables & then update listview ?

1 solution

Thank you guys for your assistance, i managed to resolve the problem.
In the listViewQuick_ItemSelectionChanged event, i managed to copy data from the list view like this:

int countRows = 0;
private void listViewQuick_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
C#
listViewQuick.FullRowSelect = true;
                int[] count = new int[10];;
                con = new SqlConnection(cs.DBConn);
                con.Open();

               string cb = "insert into AuditTrail (Date,Time,Location,MessageDescription) VALUES (@d1,@d2,@d3,@d4)";
               cmd = new SqlCommand(cb);
               cmd.Connection = con;

               cmd.Parameters.Add("@d1", System.Data.SqlDbType.VarChar);
               cmd.Parameters.Add("@d2", System.Data.SqlDbType.VarChar);
               cmd.Parameters.Add("@d3", System.Data.SqlDbType.VarChar);
               cmd.Parameters.Add("@d4", System.Data.SqlDbType.VarChar);
               

               cmd.Parameters["@d1"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[0].Text;
               cmd.Parameters["@d2"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[1].Text;
               cmd.Parameters["@d3"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[2].Text;
               cmd.Parameters["@d4"].Value = (string)listViewQuick.SelectedItems[countRows].SubItems[3].Text;
               

               countRows++;

               cmd.ExecuteReader();
               con.Close();

}
 
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