Reputation: 171
How am i going to prevent duplicate data insert entered in list view, can anyone help for this, i am still new in C#
ListViewItem lvi = new ListViewItem();
if (lvwRentBook.Items.Count == 0)
{
lvi.Text = books.BookCode.ToString();
lvi.SubItems.Add(books.BookDesc.ToString());
lvi.SubItems.Add(books.SupplierCode.ToString());
lvi.SubItems.Add(books.PricePurchase.ToString());
lvi.SubItems.Add(txtRentPRice.Text.ToString());
lvi.SubItems.Add(books.PricePenalty.ToString());
lvi.SubItems.Add("1".ToString());
lvi.SubItems.Add(books.Author.ToString());
lvi.SubItems.Add(books.Category.ToString());
lvi.SubItems.Add(books.Active.ToString());
lvi.SubItems.Add(books.ModifiedBy.ToString());
lvi.SubItems.Add(books.ModifiedOn.ToString());
lvi.SubItems.Add(books.CreatedBy.ToString());
lvi.SubItems.Add(books.CreatedOn.ToString());
lvwRentBook.Items.Add(lvi);
}
Upvotes: 1
Views: 816
Reputation: 291
A solution I found was to enclose my whole ListView within an UpdatePanel:
<asp:UpdatePanel ID="udpSiteInvite" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ListView ID="lsvSiteInvite" InsertItemPosition="FirstItem" DataSourceID="srcSiteInvite"
OnItemCommand="lsvSiteInvite_ItemCommand" runat="server">
...
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
The only drawback is that this will repopulate the controls you used (textboxes, at least; not sure about DDLs). This wasn't a problem for me but if it is for you, I suppose it's possible to clear out the controls.
Upvotes: 0
Reputation: 457
Try the following
if(lvwRentBook != null)
{
ListViewItem lvi = lvwRentBook.FindItemWithText(books.BookCode.ToString());
// if it is null means, item does not exist.You can go ahead and add it.
if (lvi == null)
{
lvi = new ListViewItem();
lvi.Text = books.BookCode.ToString();
lvi.SubItems.Add(books.BookDesc.ToString());
lvi.SubItems.Add(books.SupplierCode.ToString());
lvi.SubItems.Add(books.PricePurchase.ToString());
lvi.SubItems.Add(txtRentPRice.Text.ToString());
lvi.SubItems.Add(books.PricePenalty.ToString());
lvi.SubItems.Add("1".ToString());
lvi.SubItems.Add(books.Author.ToString());
lvi.SubItems.Add(books.Category.ToString());
lvi.SubItems.Add(books.Active.ToString());
lvi.SubItems.Add(books.ModifiedBy.ToString());
lvi.SubItems.Add(books.ModifiedOn.ToString());
lvi.SubItems.Add(books.CreatedBy.ToString());
lvi.SubItems.Add(books.CreatedOn.ToString());
lvwRentBook.Items.Add(lvi);
}
}
Upvotes: 2
Reputation: 1079
There are other problems with the code but in terms of preventing duplicates consider using a HashSet. I'm not sure what you're trying to do with your code here so I can't give you more help, but a HashSet will automatically prevent duplicate entries.
http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx
Upvotes: 0