You must Sign In to post a response.

Duplicate values are not displaying in proper order

I have column(IHno) in a Table, this coloumn is like

Ihno
1
1
2
3
4
4
7
8
9



and 2 textboxes in Windows Application like IhnoFrom(textboxname) and IhnoTo(textboxname)
in these textboxes i want to give input like range from 1 to 10 1(in IhnoFrom Txtbox) to 10 (in IhnoTo Txtbox)

in these range we find and Display MISSING Numbers and DUPLICATE numbers in IHNO column ,in above column Missing nums are 5,6,10. and Duplicate nums are 1,4 in Gridview

Here is my code :

int Start = Convert.ToInt32(txtIhFrm.Text);
int End = Convert.ToInt32(txtIhTo.Text);
//SqlConnection con = new SqlConnection("server=.;integrated security=true;database=DotNetSpider");
DataTable dt = new DataTable();
SqlDataAdapter sqlda = new SqlDataAdapter("SELECT * FROM " + strDbname + "..paid_tran where Batch_no='" + label9.Text.ToString() + "' and Ihno >='" + txtIhFrm.Text.ToString() + "' and Ihno <= '" + txtIhTo.Text.ToString() + "'", cn);
sqlda.Fill(dt);

string Duplicates="";
string Missing ="";
ArrayList AlUniqueValues = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
if (Convert.ToInt32(dr["Ihno"]) >= 2)
{
Duplicates += Convert.ToString(dr["Ihno"]) + ",";
}
AlUniqueValues.Add(Convert.ToString(dr["Ihno"]));
}

for (int i = Start; i <= End; i++)
{
if (!AlUniqueValues.Contains(Convert.ToString(i)))
{
Missing += Convert.ToString(i) + ",";
}
}

MessageBox.Show("Missing Values: " + Missing.TrimEnd(',') + "\nDuplicate Values: " + Duplicates.TrimEnd(','));
}
}

o/p:Missing nos are 12,14 (its correct)
Duplicate nos are 11,11,13,15


it should display only 11 as duplicate value but its displaying complete range of values.


Comments

Author: ni nj a02 Apr 2012 Member Level: Gold   Points : 3

hi,

please change your code like below



List missingList = new List();



for (int i = Start; i <= End; i++)
{
if (!AlUniqueValues.Contains(Convert.ToString(i)))
{

if (!missingList.Contains(Convert.ToString(i)))
missingList.Add(Convert.ToString(i));

}
}



-J