Solved

C# DataView and the 'if' statement???

Posted on 2004-08-28
3
612 Views
Last Modified: 2011-09-20
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.ExecuteReader();

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.CurrentRows;
                                                            
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.ToString());
item1.SubItems.Add(strDest1.ToString());
item1.SubItems.Add(cnt_count.ToString());
item1.SubItems.Add(str_col_2.ToString());
item1.SubItems.Add(strSrc2.ToString());            
listView1.Items.Add(item1);
}
}
catch(Exception erro)
{
MessageBox.Show(erro.ToString());
}
finally
{
SqlReader2.Close();
cn2.Close();
                                                
listView1.EndUpdate();
}
}
0
Comment
Question by:kvnsdr
3 Comments
 

Accepted Solution

by:
fpasquier earned 125 total points
ID: 11925010
I don't know what is the exact purpose of this code, but their are 2 things that are strange for me:

First thing:
I am not sure to understand the purpose of the two lines:
DataView dv = new DataView(ds.Tables["table2"]);
dv = dt.DefaultView;
Why don't you write smth like:
DataView dv = dt.DefaultView;

Because the first line "dv = new DataView(ds.Tables["table2"]);" has no contribution in your program. It is immediately overwritten by the second line.

And second thing:
The two if-blocks can be grouped in only one!
if(dv.Count > 0)   // Match found..............
{
strDest1 = dv[0]["value"].ToString();
strSrc2 = dv[0]["value"].ToString();    
}
else
{
strDest1 = "No Match Found";
strSrc2 = "No Match Found";
}

0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11925834
Changes well noted. The basic purpose is looking for a match in table1 (col_1) for table2 (col_1 or col_3) and the result is table1 (col_2) which returns a value for one of the listview columns.

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.CurrentRows;

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(); // col_2 of table2 or value needed to display in listview
strSrc2 = dv[0]["col_2"].ToString();  // col_2 of table2 or value needed to display in listview
}
else
{
strDes1 = "No Match Found";
strSrc2 = "No Match Found";








0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 125 total points
ID: 11926170
dv.RowFilter = "col_IP LIKE ' ' ";   ... im confused about this ...


DataView dv = new DataView(ds.Tables[0]); //ok you get your dataview
dv.RowFilter = "col_IP LIKE ' * ' "; //you set a rowfilter to like ' * ', why are you doing a like this ? you actually want a space before and after and anything in between ? shouldnt you be setting the rowfilter to the record that you are looking for i.e. the values you are using in the loop ?
dv.RowStateFilter = DataViewRowState.CurrentRows; //makes sense

if(dv.Rows.Count == 1)  {
strDest1 = dv[0]["col_2"].ToString(); // col_2 of table2 or value needed to display in listview
strSrc2 = dv[0]["col_2"].ToString();  // col_2 of table2 or value needed to display in listview
} else if (dv.Rows.Count > 1) {
strDes1 = "Multiple Matches Found";
strSrc2 = "Multiple Matches Found";
}
else
{
strDes1 = "No Match Found";
strSrc2 = "No Match Found";
}

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hey Audio! 9 45
SqlDependency to get update from sql to my c# app 2 66
Sort GridView by ID Descending 1 17
How to read text with RegEx... 2 26
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question