Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to compare a sql server table field with a csv field and display what is not in the sql table.

How to compare a sql server table field with a csv field and display what is not in the sql table.

Scheduled Pinned Locked Moved C#
databasesql-serversysadmintutorialquestion
50 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sascha Lefevre
    1. I just forgot to remove the line using System.Windows.Forms; - you don't need it. 2) Seems like you haven't yet adjusted the column names in the last query. The reason why you don't get an exception because of that and why nothing happens at all is: 3) I'm pretty sure the method isn't called anywhere. These lines:

    gvNewResource.DataSource = QueryStaff();
    gvNewResource.DataBind();

    aren't supposed to be in QueryStaff(). At the moment it's a snake that bites its own tail - you "try" to call QueryStaff() from within itself but it's not called from anywhere "outside". The method that contained your original code is probably the place where you should move these two lines into (as I've already written in my second-last reply or so ;P ) or just into any method that is actually triggered by something (maybe a button-click).

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    N Offline
    N Offline
    Norris Chappell
    wrote on last edited by
    #22

    Okay I am confused. I'm suppose to put this method in my other code. I am in a loop now when I add this to my code. protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { } }

    N S 2 Replies Last reply
    0
    • N Norris Chappell

      Okay I am confused. I'm suppose to put this method in my other code. I am in a loop now when I add this to my code. protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { } }

      N Offline
      N Offline
      Norris Chappell
      wrote on last edited by
      #23

      Okay it is now working. Finally. However I need to work on the hours. Which I can do. Let me show you my final code.

      using System;
      using System.Configuration;
      using System.Data;
      using System.IO;
      using System.Data.Common;
      using System.Data.SqlClient;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Globalization;

      namespace StaffingWebParts.VisualWebPart1
      {
      public partial class VisualWebPart1UserControl : UserControl
      {
      protected void Page_Load(object sender, EventArgs e)
      {

              if (!Page.IsPostBack)
              {
                  this.QueryStaff();
                  gvNewResource.DataSource = QueryStaff();
                  gvNewResource.DataBind();
                             
              }
          }
       
          private static char\[\] Colon = new char\[\] { ',' };
          private DataTable QueryStaff()
          {
              const int nameColumnIndex = 1;
              const int hoursColumnIndex = 9;
      
              using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString))
              using (var cmd = new SqlCommand("", conn))
              using (var dataAdapter = new SqlDataAdapter(cmd))
              using (var cmdBuilder = new SqlCommandBuilder(dataAdapter))
              {
                  // create temporary table in database
                  //cmd.CommandText = "CREATE TABLE #TempTable(Name nvarchar(100) NOT NULL, Hours decimal(6, 2) NOT NULL);";
                  // cmd.ExecuteNonQuery();
      
                  // create a DataTable and let the DataAdapter create appropriate columns for it
                  DataTable dataTable = new DataTable();
                  cmd.CommandText = "SELECT \* FROM TempTable;";
                  dataAdapter.Fill(dataTable);
      
                  // read the CSV-records into the DataTable
                  dataTable.BeginLoadData();
              
                  using (StreamReader reader = File.OpenText(@"c:\\Users\\pzd74f\\Downloads\\TestLabor2015.csv"))
                  {
                      string line;
                      if (reader.ReadLine() != null) // skip first line (headers)
                      {
                          while ((line = reader.ReadLine()) != null)
                          {
                              string\[\] columns = line.Split(Colon, StringSplitOptions.None);
      
                              DataRow row = dataTable.NewRow();
                              row\["Name"\] = columns\[nam
      
      S 1 Reply Last reply
      0
      • N Norris Chappell

        Okay I am confused. I'm suppose to put this method in my other code. I am in a loop now when I add this to my code. protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { } }

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #24

        I'm sorry I can't help you with ASP.NET, I have next to no experience with that. But. If your original code, wherever it was, was executed (even though it didn't do what it should), then this should work when you remove these two lines from QueryStaff() and insert them where your original code was.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        N 1 Reply Last reply
        0
        • N Norris Chappell

          Okay it is now working. Finally. However I need to work on the hours. Which I can do. Let me show you my final code.

          using System;
          using System.Configuration;
          using System.Data;
          using System.IO;
          using System.Data.Common;
          using System.Data.SqlClient;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Globalization;

          namespace StaffingWebParts.VisualWebPart1
          {
          public partial class VisualWebPart1UserControl : UserControl
          {
          protected void Page_Load(object sender, EventArgs e)
          {

                  if (!Page.IsPostBack)
                  {
                      this.QueryStaff();
                      gvNewResource.DataSource = QueryStaff();
                      gvNewResource.DataBind();
                                 
                  }
              }
           
              private static char\[\] Colon = new char\[\] { ',' };
              private DataTable QueryStaff()
              {
                  const int nameColumnIndex = 1;
                  const int hoursColumnIndex = 9;
          
                  using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString))
                  using (var cmd = new SqlCommand("", conn))
                  using (var dataAdapter = new SqlDataAdapter(cmd))
                  using (var cmdBuilder = new SqlCommandBuilder(dataAdapter))
                  {
                      // create temporary table in database
                      //cmd.CommandText = "CREATE TABLE #TempTable(Name nvarchar(100) NOT NULL, Hours decimal(6, 2) NOT NULL);";
                      // cmd.ExecuteNonQuery();
          
                      // create a DataTable and let the DataAdapter create appropriate columns for it
                      DataTable dataTable = new DataTable();
                      cmd.CommandText = "SELECT \* FROM TempTable;";
                      dataAdapter.Fill(dataTable);
          
                      // read the CSV-records into the DataTable
                      dataTable.BeginLoadData();
                  
                      using (StreamReader reader = File.OpenText(@"c:\\Users\\pzd74f\\Downloads\\TestLabor2015.csv"))
                      {
                          string line;
                          if (reader.ReadLine() != null) // skip first line (headers)
                          {
                              while ((line = reader.ReadLine()) != null)
                              {
                                  string\[\] columns = line.Split(Colon, StringSplitOptions.None);
          
                                  DataRow row = dataTable.NewRow();
                                  row\["Name"\] = columns\[nam
          
          S Offline
          S Offline
          Sascha Lefevre
          wrote on last edited by
          #25

          Alright, one step further :) You can remove this line: this.QueryStaff(); In which way does the Hours-Stuff not work as expected yet?

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          N 1 Reply Last reply
          0
          • S Sascha Lefevre

            Alright, one step further :) You can remove this line: this.QueryStaff(); In which way does the Hours-Stuff not work as expected yet?

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            N Offline
            N Offline
            Norris Chappell
            wrote on last edited by
            #26

            It's not showing the hours. Just the name. The table has the hours but the sum in not working.

            S 1 Reply Last reply
            0
            • S Sascha Lefevre

              I'm sorry I can't help you with ASP.NET, I have next to no experience with that. But. If your original code, wherever it was, was executed (even though it didn't do what it should), then this should work when you remove these two lines from QueryStaff() and insert them where your original code was.

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              N Offline
              N Offline
              Norris Chappell
              wrote on last edited by
              #27

              No problem with asp.net. I took this project over from a person who left to go to another project and division. Java/C++/Oracle programming is what I did in the past. So I am very new to C#.

              N 1 Reply Last reply
              0
              • N Norris Chappell

                It's not showing the hours. Just the name. The table has the hours but the sum in not working.

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #28

                Please re-enable the code for the temporary table so that we can rule out that there's something wrong with your replacement by a permanent table: - two lines have to be un-commented - in the two query-strings put a # in front of "TempTable" -> #TempTable - delete or rename the permanent table "TempTable" in the database Unrelated: You can remove the line conn.Close(); , it happens automatically because of the using-statement.

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                1 Reply Last reply
                0
                • N Norris Chappell

                  No problem with asp.net. I took this project over from a person who left to go to another project and division. Java/C++/Oracle programming is what I did in the past. So I am very new to C#.

                  N Offline
                  N Offline
                  Norris Chappell
                  wrote on last edited by
                  #29

                  I'm getting this error: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

                  S 1 Reply Last reply
                  0
                  • N Norris Chappell

                    I'm getting this error: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

                    S Offline
                    S Offline
                    Sascha Lefevre
                    wrote on last edited by
                    #30

                    Yep, that's what I mentioned, what I forgot in the posted version of my code, because the code I tested was slightly different and didn't require explicit handling of this. You can fix this, right? ;)

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    N 1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      Yep, that's what I mentioned, what I forgot in the posted version of my code, because the code I tested was slightly different and didn't require explicit handling of this. You can fix this, right? ;)

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      N Offline
                      N Offline
                      Norris Chappell
                      wrote on last edited by
                      #31

                      So I will need to code an opening of the database and available connection since it closed?

                      N S 2 Replies Last reply
                      0
                      • N Norris Chappell

                        So I will need to code an opening of the database and available connection since it closed?

                        N Offline
                        N Offline
                        Norris Chappell
                        wrote on last edited by
                        #32

                        If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                        N S 2 Replies Last reply
                        0
                        • N Norris Chappell

                          So I will need to code an opening of the database and available connection since it closed?

                          S Offline
                          S Offline
                          Sascha Lefevre
                          wrote on last edited by
                          #33

                          Between the opening brace and the following comment "// create temporary table in database" insert a conn.Open();

                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                          N 1 Reply Last reply
                          0
                          • N Norris Chappell

                            If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                            N Offline
                            N Offline
                            Norris Chappell
                            wrote on last edited by
                            #34

                            One more thing My perm table is called TempTable maybe I should create the temp table with a different name?

                            1 Reply Last reply
                            0
                            • N Norris Chappell

                              If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                              S Offline
                              S Offline
                              Sascha Lefevre
                              wrote on last edited by
                              #35

                              Norris Chappell wrote:

                              If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                              What do you mean by "it" here?

                              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                              1 Reply Last reply
                              0
                              • S Sascha Lefevre

                                Between the opening brace and the following comment "// create temporary table in database" insert a conn.Open();

                                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                N Offline
                                N Offline
                                Norris Chappell
                                wrote on last edited by
                                #36

                                Okay didn't get the error. However, I don't see the table in SQL Server?

                                N 1 Reply Last reply
                                0
                                • N Norris Chappell

                                  Okay didn't get the error. However, I don't see the table in SQL Server?

                                  N Offline
                                  N Offline
                                  Norris Chappell
                                  wrote on last edited by
                                  #37

                                  I tried to query it and got an error?

                                  S 1 Reply Last reply
                                  0
                                  • N Norris Chappell

                                    I tried to query it and got an error?

                                    S Offline
                                    S Offline
                                    Sascha Lefevre
                                    wrote on last edited by
                                    #38

                                    Quote:

                                    If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                                    Sorry, I'm tired - now I think I understood what you meant: The DataAdapter on this line: dataAdapter.Fill(dataTable); opens the connection automatically if it isn't already, but cmd.ExecuteNonQuery(); doesn't, so after un-commenting that line it became neccessary to open the connection explicitly.

                                    Quote:

                                    One more thing My perm table is called TempTable maybe I should create the temp table with a different name?

                                    I think you're tired too ;) Yes, you should have renamed or deleted the perm table. (I actually don't know if it would be a problem, I just told you to do that to avoid a potential error.)

                                    Quote:

                                    Okay didn't get the error. However, I don't see the table in SQL Server?

                                    It's a temporary table. It exists only during the lifetime of the connection and is only visible to that connection.

                                    Quote:

                                    I tried to query it and got an error?

                                    Always tell the exact error message when you're trying to get help :)

                                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                    N 2 Replies Last reply
                                    0
                                    • S Sascha Lefevre

                                      Quote:

                                      If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                                      Sorry, I'm tired - now I think I understood what you meant: The DataAdapter on this line: dataAdapter.Fill(dataTable); opens the connection automatically if it isn't already, but cmd.ExecuteNonQuery(); doesn't, so after un-commenting that line it became neccessary to open the connection explicitly.

                                      Quote:

                                      One more thing My perm table is called TempTable maybe I should create the temp table with a different name?

                                      I think you're tired too ;) Yes, you should have renamed or deleted the perm table. (I actually don't know if it would be a problem, I just told you to do that to avoid a potential error.)

                                      Quote:

                                      Okay didn't get the error. However, I don't see the table in SQL Server?

                                      It's a temporary table. It exists only during the lifetime of the connection and is only visible to that connection.

                                      Quote:

                                      I tried to query it and got an error?

                                      Always tell the exact error message when you're trying to get help :)

                                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                      N Offline
                                      N Offline
                                      Norris Chappell
                                      wrote on last edited by
                                      #39

                                      Sorry again. You are right I am tired too. I think I am good for now. I can figure out the hours issue. You have been so helpful and I have learn a lot from you. Thank you for being so patience with me. What time zone are you in? Are you from France?

                                      S 1 Reply Last reply
                                      0
                                      • N Norris Chappell

                                        Sorry again. You are right I am tired too. I think I am good for now. I can figure out the hours issue. You have been so helpful and I have learn a lot from you. Thank you for being so patience with me. What time zone are you in? Are you from France?

                                        S Offline
                                        S Offline
                                        Sascha Lefevre
                                        wrote on last edited by
                                        #40

                                        You're welcome! But if you can't figure out the hours issue, feel free to ask. I'm from Germany, one of my ancestors came from France :) Good luck and good night! cheers, Sascha

                                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                        1 Reply Last reply
                                        0
                                        • S Sascha Lefevre

                                          Quote:

                                          If that is the case why didn't I need to have it when I did the select on the "where not exists" query?

                                          Sorry, I'm tired - now I think I understood what you meant: The DataAdapter on this line: dataAdapter.Fill(dataTable); opens the connection automatically if it isn't already, but cmd.ExecuteNonQuery(); doesn't, so after un-commenting that line it became neccessary to open the connection explicitly.

                                          Quote:

                                          One more thing My perm table is called TempTable maybe I should create the temp table with a different name?

                                          I think you're tired too ;) Yes, you should have renamed or deleted the perm table. (I actually don't know if it would be a problem, I just told you to do that to avoid a potential error.)

                                          Quote:

                                          Okay didn't get the error. However, I don't see the table in SQL Server?

                                          It's a temporary table. It exists only during the lifetime of the connection and is only visible to that connection.

                                          Quote:

                                          I tried to query it and got an error?

                                          Always tell the exact error message when you're trying to get help :)

                                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                          N Offline
                                          N Offline
                                          Norris Chappell
                                          wrote on last edited by
                                          #41

                                          I need to replace: using (StreamReader reader = File.OpenText(@"c:\Users\pzd74f\Downloads\TestLabor2015.csv")) with something like this: protected void ImportButton_Click(object sender, EventArgs e) { if (this.FileUpload1.HasFile) { var extension = Path.GetExtension(FileUpload1.FileName); if (extension == ".csv") { StreamReader csvreader = new StreamReader(FileUpload1.FileContent); I need to have the user to be able to import the file instead of the csv be hardcoded.

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups