Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Checking Duplicate Values within a list using SpQuery.

using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Configuration;
[assembly: AllowPartiallyTrustedCallers]

namespace TestWebPart1
{
[Guid("12084c4a-5bff-43e9-b89e-40bb11f2be86")]
public class TestWebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtFname;
Button btnSearchDuplicateValues;
string strError = string.Empty;
public TestWebPart1()
{

}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(strError);
try
{
writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write(""); writer.Write("
");
writer.Write("Enter Friend Name");
writer.Write("
");
txtFname.RenderControl(writer);
writer.Write("
");
btnSearchDuplicateValues.RenderControl(writer);
writer.Write("
");
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
protected override void CreateChildControls()
{
try
{

txtFname = new TextBox();
txtFname.ID = "txtFname";
this.Controls.Add(txtFname);

btnSearchDuplicateValues = new Button();
btnSearchDuplicateValues.ID = "btnSearchDuplicateValues";
btnSearchDuplicateValues.Text = "Check Availability";
btnSearchDuplicateValues.Click += new EventHandler(btnSearchDuplicateValues_Click);
this.Controls.Add(btnSearchDuplicateValues);
}
catch (Exception ex)
{
strError += ex.ToString();
}
}

void btnSearchDuplicateValues_Click(object sender, EventArgs e)
{
try
{
SPWeb curentWeb = SPControl.GetContextWeb(Context);
SPList lst = curentWeb.Lists["Telno"];
SPQuery sQuery = new SPQuery();
sQuery.Query = "" + txtFname.Text.ToString().ToLower() + "";
SPListItemCollection myColl = lst.GetItems(sQuery);
if (myColl.Count > 0)
{
strError += "";
}
else
{
strError += "";
}
}
catch (Exception ex)
{
strError += ex.ToString();
}
}
}
}

No comments:

Post a Comment