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.
  • 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
            • N Norris Chappell

              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 Offline
              S Offline
              Sascha Lefevre
              wrote on last edited by
              #42

              Just pass FileUpload1.FileContent as an argument to QueryStaff :)

              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

                Just pass FileUpload1.FileContent as an argument to QueryStaff :)

                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
                #43

                I tried to pass as an argument but getting errors: c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\StaffingWebParts\VisualWebPart1\VisualWebPart1UserControl.ascx(13): error CS0117: 'ASP._controltemplates_staffingwebparts_visualwebpart1_visualwebpart1usercontrol_ascx' does not contain a definition for 'ImportButton_Click' I need protected void ImportButton_Click(object sender, EventArgs e) somewhere but not sure where? using ( StreamReader reader = new StreamReader(NewResourceFileUpload.FileContent))

                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
                           conn.Open();
                           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 = new StreamR
                
                S 1 Reply Last reply
                0
                • N Norris Chappell

                  I tried to pass as an argument but getting errors: c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\StaffingWebParts\VisualWebPart1\VisualWebPart1UserControl.ascx(13): error CS0117: 'ASP._controltemplates_staffingwebparts_visualwebpart1_visualwebpart1usercontrol_ascx' does not contain a definition for 'ImportButton_Click' I need protected void ImportButton_Click(object sender, EventArgs e) somewhere but not sure where? using ( StreamReader reader = new StreamReader(NewResourceFileUpload.FileContent))

                  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
                             conn.Open();
                             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 = new StreamR
                  
                  S Offline
                  S Offline
                  Sascha Lefevre
                  wrote on last edited by
                  #44

                  I'm sorry, I can't really help there, as I'm unfamiliar with ASP.NET. If I recall correctly, you took over this project from somebody else, right? I imagine there should be some code handling button-clicks in other parts of the application. Maybe you can take that as a kind of template how to do it. Else I would suggest you to open a new question in the ASP.NET forum so someone else can hopefully help you there.

                  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

                    I'm sorry, I can't really help there, as I'm unfamiliar with ASP.NET. If I recall correctly, you took over this project from somebody else, right? I imagine there should be some code handling button-clicks in other parts of the application. Maybe you can take that as a kind of template how to do it. Else I would suggest you to open a new question in the ASP.NET forum so someone else can hopefully help you there.

                    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
                    #45

                    Okay thanks. I going to submit what I have to my Director. They will just need to name the csv what I have in the code. I never got the hours to work.

                    1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      I'm sorry, I can't really help there, as I'm unfamiliar with ASP.NET. If I recall correctly, you took over this project from somebody else, right? I imagine there should be some code handling button-clicks in other parts of the application. Maybe you can take that as a kind of template how to do it. Else I would suggest you to open a new question in the ASP.NET forum so someone else can hopefully help you there.

                      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
                      #46

                      Sascha, The Director is happy with code but would like to see the hours. I looped thru the code with debug and see that the #temptable is showing the hours but are not summing then. Would this select statement be where I need to focus? SELECT Tmp.Name, SUM(Tmp.Hours) FROM TempTable AS Tmp WHERE NOT EXISTS (SELECT * FROM StaffTracking AS ST WHERE Tmp.Name = ST.ResourceName) GROUP BY Tmp.Name;"; I have tried several different scenarios but no results. Thanks, Norris

                      S 1 Reply Last reply
                      0
                      • N Norris Chappell

                        Sascha, The Director is happy with code but would like to see the hours. I looped thru the code with debug and see that the #temptable is showing the hours but are not summing then. Would this select statement be where I need to focus? SELECT Tmp.Name, SUM(Tmp.Hours) FROM TempTable AS Tmp WHERE NOT EXISTS (SELECT * FROM StaffTracking AS ST WHERE Tmp.Name = ST.ResourceName) GROUP BY Tmp.Name;"; I have tried several different scenarios but no results. Thanks, Norris

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

                        Hi Norris, there's no # in front of "TempTable" there. If it's the same in your code, that might be the problem. If it's not that I have no further ideas because it works for me. I hope you'll be able to fix it - good luck! /Sascha

                        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

                          Hi Norris, there's no # in front of "TempTable" there. If it's the same in your code, that might be the problem. If it's not that I have no further ideas because it works for me. I hope you'll be able to fix it - good luck! /Sascha

                          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
                          #48

                          Ok. Thank you for your help.

                          1 Reply Last reply
                          0
                          • S Sascha Lefevre

                            Hi Norris, there's no # in front of "TempTable" there. If it's the same in your code, that might be the problem. If it's not that I have no further ideas because it works for me. I hope you'll be able to fix it - good luck! /Sascha

                            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
                            #49

                            Okay got it figure out for the Hours. I needed to add to sum(Tmp.Hours) as Hours. :) ;)

                            S 1 Reply Last reply
                            0
                            • N Norris Chappell

                              Okay got it figure out for the Hours. I needed to add to sum(Tmp.Hours) as Hours. :) ;)

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

                              Cheers! :-D

                              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
                              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