kvnsdr
asked on
C# DataView and the 'if' statement???
For some reason the DataView always loads "No Match Found" in colums 2 and 5 of my ListView. It's obviously not findin any matches, however the code complies and the program runs without error. I'm sure the DataSet (ds) is working correctly and loaded with data. I feel the problem maybe in the 'if' statement. I'm undecided on using 'Count' or 'Equals' like so:
if(dv.Count > 0) // Match found...................
{
strDest1 = dv[0]["value"].ToString();
Q. What is wrong with my dataview???
*** Full Code Below***
private void Load()
{
DataSet ds = new DataSet();
string SQL1 = "SELECT col_1, col_2 FROM table2";
SqlConnection cn1 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da1 = new SqlDataAdapter(SQL1,cn1);
cn1.Open();
da1.Fill(ds);
cn1.Close();
dt = ds.Tables[0];
string SQL2 = "SELECT col_1, col_2, COUNT(col_2)AS 'count' FROM table1 GROUP BY col_1, col_2 HAVING COUNT (*) >5 ORDER BY count DESC";
SqlConnection cn2 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da2 = new SqlDataAdapter(SQL2,cn2);
cn2.Open();
SqlDataReader SqlReader2 = da2.SelectCommand.ExecuteR eader();
listView1.Items.Clear();
listView1.BeginUpdate();
try
{
while(SqlReader2.Read())
{
string str_col_1 = SqlReader2.GetString(0);
string str_col_2 = SqlReader2.GetString(1);
int cnt_count = SqlReader2.GetInt32(2);
DataView dv = new DataView(ds.Tables["table2 "]);
dv = dt.DefaultView;
dv.RowFilter = "col_1 LIKE ' * '";
dv.RowStateFilter = DataViewRowState.CurrentRo ws;
if(dv.Count > 0) // Match found..............
{
strDest1 = dv[0]["value"].ToString();
}
else
{
strDest1 = "No Match Found";
}
if(dv.Count > 0) // Match found..........
{
strSrc2 = dv[0]["value"].ToString();
}
else
{
strSrc2 = "No Match Found";
}
ListViewItem item1 = new ListViewItem(str_col_1.ToS tring());
item1.SubItems.Add(strDest 1.ToString ());
item1.SubItems.Add(cnt_cou nt.ToStrin g());
item1.SubItems.Add(str_col _2.ToStrin g());
item1.SubItems.Add(strSrc2 .ToString( ));
listView1.Items.Add(item1) ;
}
}
catch(Exception erro)
{
MessageBox.Show(erro.ToStr ing());
}
finally
{
SqlReader2.Close();
cn2.Close();
listView1.EndUpdate();
}
}
if(dv.Count > 0) // Match found...................
{
strDest1 = dv[0]["value"].ToString();
Q. What is wrong with my dataview???
*** Full Code Below***
private void Load()
{
DataSet ds = new DataSet();
string SQL1 = "SELECT col_1, col_2 FROM table2";
SqlConnection cn1 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da1 = new SqlDataAdapter(SQL1,cn1);
cn1.Open();
da1.Fill(ds);
cn1.Close();
dt = ds.Tables[0];
string SQL2 = "SELECT col_1, col_2, COUNT(col_2)AS 'count' FROM table1 GROUP BY col_1, col_2 HAVING COUNT (*) >5 ORDER BY count DESC";
SqlConnection cn2 = new SqlConnection("integrated security=SSPI;data source=office; persist security info=False;initial catalog=master");
SqlDataAdapter da2 = new SqlDataAdapter(SQL2,cn2);
cn2.Open();
SqlDataReader SqlReader2 = da2.SelectCommand.ExecuteR
listView1.Items.Clear();
listView1.BeginUpdate();
try
{
while(SqlReader2.Read())
{
string str_col_1 = SqlReader2.GetString(0);
string str_col_2 = SqlReader2.GetString(1);
int cnt_count = SqlReader2.GetInt32(2);
DataView dv = new DataView(ds.Tables["table2
dv = dt.DefaultView;
dv.RowFilter = "col_1 LIKE ' * '";
dv.RowStateFilter = DataViewRowState.CurrentRo
if(dv.Count > 0) // Match found..............
{
strDest1 = dv[0]["value"].ToString();
}
else
{
strDest1 = "No Match Found";
}
if(dv.Count > 0) // Match found..........
{
strSrc2 = dv[0]["value"].ToString();
}
else
{
strSrc2 = "No Match Found";
}
ListViewItem item1 = new ListViewItem(str_col_1.ToS
item1.SubItems.Add(strDest
item1.SubItems.Add(cnt_cou
item1.SubItems.Add(str_col
item1.SubItems.Add(strSrc2
listView1.Items.Add(item1)
}
}
catch(Exception erro)
{
MessageBox.Show(erro.ToStr
}
finally
{
SqlReader2.Close();
cn2.Close();
listView1.EndUpdate();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
NOTE: I also changed the dv.Count to dv.Equals hoping for a match and changed "value" to "col_2" , but it did not work... However, no compiling or run-time errors either..... still getting "No Match Found" in colums 2 and 5 of the listview..............
Q. Is the DataView the right tool for the job???
DataView dv = new DataView(ds.Tables[0]);
dv.RowFilter = "col_IP LIKE ' ' ";
dv.RowStateFilter = DataViewRowState.CurrentRo
if(dv.Equals (str_col_1)) // col_1 of table1 or data to match the key in col_1 of table2 (key-value pair concept)
{
strDest1 = dv[0]["col_2"].ToString();
strSrc2 = dv[0]["col_2"].ToString();
}
else
{
strDes1 = "No Match Found";
strSrc2 = "No Match Found";