0

I'm trying to write a tool to compare a database with a textfile. It should get the data from the PostgreSQL database and check if the textfile contains the value of the database coloumn.

It should Show all values from the database, if they are contained in the Textfile. Example: If the Name "a" is in the textfile, it should Show "a" and other data from the database in the wpf datagrid.

Here is my Code for the ceck up:

string connStr = "Server=" + Globals.server + ";Port=" + Globals.port + "; Database=" + Globals.db + ";User Id=" + Globals.user + ";Password=" + Globals.passwd + ";";
NpgsqlConnection conn = new NpgsqlConnection(connStr);
conn.Open();
NpgsqlCommand nda = new NpgsqlCommand("select name,ip_address,location from public.\"smartq_devices\"", conn);
using (StreamReader sr = File.OpenText(Globals.sFilename))
{
    string[] lines = File.ReadAllLines(Globals.sFilename);
    for (int x = 0; x < lines.Length - 1; x++)
    {
        NpgsqlDataReader myReader = nda.ExecuteReader();
        while (myReader.Read())
        {
            if (lines[x].Contains(myReader["name"].ToString()))
            {
                //MessageBox.Show(lines[x]);
                DataRow newRow = dtResult1.NewRow();
                newRow["Hostname"] = myReader["name"].ToString();
                newRow["Ip address"] = myReader["ip_address"].ToString();
                newRow["Location"] = myReader["location"].ToString();
                dtResult1.Rows.Add(newRow);
            }
        }
    }
    dgViewDevices.ItemsSource = dtResult1.AsDataView();
}

When i execute this code i get some data in the datagrid but there are some blank lines in between. I added a Picture of the result.

Datagrid with blank rows

I don't know where I have the error or even the wrong Approach.

Thank you in advance for your help.

2
  • I'm sure you must've done this, but I must ask: Are you sure there are no unnecessary newlines in your textfile? Commented Sep 21, 2016 at 9:38
  • Yes, i've done this before. But even if there are empty lines they shouldn't be processed because the name is not contained in these lines? Commented Sep 21, 2016 at 10:11

1 Answer 1

0

To prevent empty lines i made a if-clause where i check if the specific string is empty.

if (newRow["Hostname"].ToString() != "")
{
    dtResult1.Rows.Add(newRow);
}

With this lines added there are no empty lines in the datagrid.

Full Code:

string connStr = "Server=" + Globals.server + ";Port=" + Globals.port + "; Database=" + Globals.db + ";User Id=" + Globals.user + ";Password=" + Globals.passwd + ";";
NpgsqlConnection conn = new NpgsqlConnection(connStr);
conn.Open();
NpgsqlCommand nda = new NpgsqlCommand("select name,ip_address,location from public.\"smartq_devices\"", conn);
using (StreamReader sr = File.OpenText(Globals.sFilename))
{
    string[] lines = File.ReadAllLines(Globals.sFilename);
    for (int x = 0; x < lines.Length - 1; x++)
    {
        NpgsqlDataReader myReader = nda.ExecuteReader();
        while (myReader.Read())
        {
            if (lines[x].Contains(myReader["name"].ToString()))
            {   
                //MessageBox.Show(lines[x]);
                DataRow newRow = dtResult1.NewRow();
                newRow["Hostname"] = myReader["name"].ToString();
                newRow["Ip address"] = myReader["ip_address"].ToString();
                newRow["Location"] = myReader["location"].ToString();
                if (newRow["Hostname"].ToString() != "")
                {
                    dtResult1.Rows.Add(newRow);
                }
            }
        }
    }
    dgViewDevices.ItemsSource = dtResult1.AsDataView();
}

Maybe this will help other, who have the same Problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.