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

    Yes I was able to load my csv file into a table and do a query against both table and got the results I was looking for. I haven't done any new C# code.

    K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #6

    Use Linq-To-SQL

    If it's not broken, fix it until it is

    1 Reply Last reply
    0
    • N Norris Chappell

      Yes I was able to load my csv file into a table and do a query against both table and got the results I was looking for. I haven't done any new C# code.

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

      Alright. I would suggest: 1) Your original code looks like you not only want the names of the persons but also their total hours and "FTE". At least the total hours are easy to calculate in the same query, using a GROUP BY-clause and the Sum()-function. Tune that query in SQL Server Management Studio (or whatever you're using) so that you get exactly the query result you want to display, possibly except those "FTE". (If you're not familiar with GROUP BY, please take a look at the MSDN online documentation, there should be a sample that also shows the Sum()-function.) 2) Either have that temporary table as a permanent table in your DB (which you would clear after use) or write some C#-code that creates an actual temporary table. SQL Server Management Studio can produce a CREATE TABLE-script for you from your current test-table which you would practically only have to execute from C# with an SqlCommand and ExecuteNonQuery(). (Just add a # to the start of the table name to make it a temp table.) 3) Get that CSV-Reader-library that I linked in my first reply running and read the CSV-file (that's a piece of cake). 4) Write the code that inserts the CSV-records into the (temp) table. 5) Write the code that executes the query from point 1 and read the result into a DataTable. 6) If you didn't calculate the "FTE" with the SQL-Query, add a new DataColumn to the DataTable and "fill" it. 7) Set the DataTable as DataSource for your DataGrid and you're done. One more Hint: Temp tables are valid throughout the course of a connection. So you will have to use the same open connection from creating the temp table to reading the query result. (I'm packing up for today - if you have follow-up questions, maybe someone else will be able to answer them, else I'll get back to you tomorrow.) Good luck!

      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. I would suggest: 1) Your original code looks like you not only want the names of the persons but also their total hours and "FTE". At least the total hours are easy to calculate in the same query, using a GROUP BY-clause and the Sum()-function. Tune that query in SQL Server Management Studio (or whatever you're using) so that you get exactly the query result you want to display, possibly except those "FTE". (If you're not familiar with GROUP BY, please take a look at the MSDN online documentation, there should be a sample that also shows the Sum()-function.) 2) Either have that temporary table as a permanent table in your DB (which you would clear after use) or write some C#-code that creates an actual temporary table. SQL Server Management Studio can produce a CREATE TABLE-script for you from your current test-table which you would practically only have to execute from C# with an SqlCommand and ExecuteNonQuery(). (Just add a # to the start of the table name to make it a temp table.) 3) Get that CSV-Reader-library that I linked in my first reply running and read the CSV-file (that's a piece of cake). 4) Write the code that inserts the CSV-records into the (temp) table. 5) Write the code that executes the query from point 1 and read the result into a DataTable. 6) If you didn't calculate the "FTE" with the SQL-Query, add a new DataColumn to the DataTable and "fill" it. 7) Set the DataTable as DataSource for your DataGrid and you're done. One more Hint: Temp tables are valid throughout the course of a connection. So you will have to use the same open connection from creating the temp table to reading the query result. (I'm packing up for today - if you have follow-up questions, maybe someone else will be able to answer them, else I'll get back to you tomorrow.) Good luck!

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

        Hi Sascha, You have been quite helpful. I have redone my code and now using "where not exists" code and it is working. However, the temporary table is not updated with the csv file yet. I read the article you recommended. I not sure how to put it in my code? I don't need the FTE only the name. I don't think I will need the hours but since the file have several rows with the same name it wouldn't be bad to sum up the hours too. Once I can update the table with the csv file, I think I will be done. Thanks, Norris I forgot to include my code:

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

        namespace StaffingWebParts.VisualWebPart1
        {
        public partial class VisualWebPart1UserControl : UserControl
        {
        public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
        if (!Page.IsPostBack)
        {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");

                    SqlDataReader myReader = null;
                    SqlCommand cmd = new SqlCommand("select Name FROM \[SP2010\_EDCStaffing\_AppDB\].\[dbo\].\[Tempname\] where not exists (select \* FROM \[SP2010\_EDCStaffing\_AppDB\].\[dbo\].StaffTracking WHERE tempname.Name = Stafftracking.ResourceName)");            
                             
                    cmd.Connection = conn;
                    conn.Open();
                    myReader = cmd.ExecuteReader();
                    while (myReader.Read())
                    {
                        DataRow dr = dt.NewRow();
                        dr\[0\] = myReader\["Name"\].ToString();
                                                     
                        dt.Rows.Add(dr);
                    }
                              
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    conn.Close();
                }
            }
           
          }
        }
        
        S 1 Reply Last reply
        0
        • N Norris Chappell

          Hi Sascha, You have been quite helpful. I have redone my code and now using "where not exists" code and it is working. However, the temporary table is not updated with the csv file yet. I read the article you recommended. I not sure how to put it in my code? I don't need the FTE only the name. I don't think I will need the hours but since the file have several rows with the same name it wouldn't be bad to sum up the hours too. Once I can update the table with the csv file, I think I will be done. Thanks, Norris I forgot to include my code:

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

          namespace StaffingWebParts.VisualWebPart1
          {
          public partial class VisualWebPart1UserControl : UserControl
          {
          public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLStaffingConn"].ConnectionString);
          protected void Page_Load(object sender, EventArgs e)
          {
          if (!Page.IsPostBack)
          {
          DataTable dt = new DataTable();
          dt.Columns.Add("Name");

                      SqlDataReader myReader = null;
                      SqlCommand cmd = new SqlCommand("select Name FROM \[SP2010\_EDCStaffing\_AppDB\].\[dbo\].\[Tempname\] where not exists (select \* FROM \[SP2010\_EDCStaffing\_AppDB\].\[dbo\].StaffTracking WHERE tempname.Name = Stafftracking.ResourceName)");            
                               
                      cmd.Connection = conn;
                      conn.Open();
                      myReader = cmd.ExecuteReader();
                      while (myReader.Read())
                      {
                          DataRow dr = dt.NewRow();
                          dr\[0\] = myReader\["Name"\].ToString();
                                                       
                          dt.Rows.Add(dr);
                      }
                                
                      GridView1.DataSource = dt;
                      GridView1.DataBind();
                      conn.Close();
                  }
              }
             
            }
          }
          
          S Offline
          S Offline
          Sascha Lefevre
          wrote on last edited by
          #9

          Hi Norris, what difficulty do you have with integrating the CSV-Reader? Basically you download it (either just the binaries or the source-zip), reference the DLL in your project and then use the first code-example from the article as a starting point. Your options for inserting the records into the database: 1) Loop through the CSV-records and build an INSERT-batch-statement (batch-statement: multiple INSERT-statements, separated by ; in one String.) To do this properly with SqlParameters is a tiny bit more effort than option 2. 2) Load the CSV-records into a DataTable: Either with DataTable.Load(csvReader) or with csvReader.ReadIntoDataTable() or (since you only need one of the columns) similar to what you do in your current code by looping through the CSV-records and adding DataRows to a DataTable "manually". Then use an SqlDataAdapter to insert the records from the DataTable into the temp table in the database. You would either have to provide the Insert-statement[^] to the SqlDataAdapter yourself (no big deal) or use this "trick": http://stackoverflow.com/a/1631133/4320056[^] (there a DataSet is used instead of a DataTable - just use the DataTable instead). (Prerequisite: adding the Create-Temp-Table-part to your code as described earlier.) Your current code looks alright, except: You should use the SqlCommand and SqlDataReader and probably also the SqlConnection in a using-block. /Sascha

          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

            Hi Norris, what difficulty do you have with integrating the CSV-Reader? Basically you download it (either just the binaries or the source-zip), reference the DLL in your project and then use the first code-example from the article as a starting point. Your options for inserting the records into the database: 1) Loop through the CSV-records and build an INSERT-batch-statement (batch-statement: multiple INSERT-statements, separated by ; in one String.) To do this properly with SqlParameters is a tiny bit more effort than option 2. 2) Load the CSV-records into a DataTable: Either with DataTable.Load(csvReader) or with csvReader.ReadIntoDataTable() or (since you only need one of the columns) similar to what you do in your current code by looping through the CSV-records and adding DataRows to a DataTable "manually". Then use an SqlDataAdapter to insert the records from the DataTable into the temp table in the database. You would either have to provide the Insert-statement[^] to the SqlDataAdapter yourself (no big deal) or use this "trick": http://stackoverflow.com/a/1631133/4320056[^] (there a DataSet is used instead of a DataTable - just use the DataTable instead). (Prerequisite: adding the Create-Temp-Table-part to your code as described earlier.) Your current code looks alright, except: You should use the SqlCommand and SqlDataReader and probably also the SqlConnection in a using-block. /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
            #10

            Sascha, Would something like this code work ? I'm really very new to C#. I think option 2 would be more in line with what I could do. Thanks, Noris

            DataTable dt = new DataTable();

            string line = null;

            int i = 0;

            using (StreamReader sr = File.OpenText(@"c:\temp\table1.csv"))

            {

              while ((line = sr.ReadLine()) != null)
            
              {
            
                    string\[\] data = line.Split(',');
            
                    if (data.Length > 0)
            
                    {
            
                          if (i == 0)
            
                          {
            
                          foreach (var item in data)
            
                          {
            
                                dt.Columns.Add(new DataColumn());
            
                          }
            
                          i++;
            
                     }
            
                     DataRow row = dt.NewRow();
            
                     row.ItemArray = data;
            
                     dt.Rows.Add(row);
            
                     }
            
              }
            

            }

            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleApplication3.Properties.Settings.daasConnectionString"].ConnectionString))

            {

              cn.Open();
            
              using (SqlBulkCopy copy = new SqlBulkCopy(cn))
            
              {
            
                    copy.ColumnMappings.Add(0, 0);
            
                    copy.ColumnMappings.Add(1, 1);
            
                    copy.ColumnMappings.Add(2, 2);
            
                    copy.ColumnMappings.Add(3, 3);
            
                    copy.ColumnMappings.Add(4, 4);
            
                    copy.DestinationTableName = "Censis";
            
                    copy.WriteToServer(dt);
            
              }
            

            }

            For option 2 this is what I came up with for the SqlDataAdapter part: public static SqlDataAdapter CreateCustomerAdapter( SqlConnection connection) { SqlDataAdapter adapter = new SqlDataAdapter(); // Create the InsertCommand. command = new SqlCommand( "INSERT INTO Tempname(ResourceName) " + "VALUES (@ResourceName)", connection); // Add the parameters for the InsertCommand. command.Parameters.Add("@ResoureName", SqlDbType.NVarChar, 50, "ResourceName"); adapter.InsertCommand = command; return adapter; } I not able to download/integrate the CSV-Reader on this server. I will need to pursuit another route.

            S 1 Reply Last reply
            0
            • N Norris Chappell

              Sascha, Would something like this code work ? I'm really very new to C#. I think option 2 would be more in line with what I could do. Thanks, Noris

              DataTable dt = new DataTable();

              string line = null;

              int i = 0;

              using (StreamReader sr = File.OpenText(@"c:\temp\table1.csv"))

              {

                while ((line = sr.ReadLine()) != null)
              
                {
              
                      string\[\] data = line.Split(',');
              
                      if (data.Length > 0)
              
                      {
              
                            if (i == 0)
              
                            {
              
                            foreach (var item in data)
              
                            {
              
                                  dt.Columns.Add(new DataColumn());
              
                            }
              
                            i++;
              
                       }
              
                       DataRow row = dt.NewRow();
              
                       row.ItemArray = data;
              
                       dt.Rows.Add(row);
              
                       }
              
                }
              

              }

              using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConsoleApplication3.Properties.Settings.daasConnectionString"].ConnectionString))

              {

                cn.Open();
              
                using (SqlBulkCopy copy = new SqlBulkCopy(cn))
              
                {
              
                      copy.ColumnMappings.Add(0, 0);
              
                      copy.ColumnMappings.Add(1, 1);
              
                      copy.ColumnMappings.Add(2, 2);
              
                      copy.ColumnMappings.Add(3, 3);
              
                      copy.ColumnMappings.Add(4, 4);
              
                      copy.DestinationTableName = "Censis";
              
                      copy.WriteToServer(dt);
              
                }
              

              }

              For option 2 this is what I came up with for the SqlDataAdapter part: public static SqlDataAdapter CreateCustomerAdapter( SqlConnection connection) { SqlDataAdapter adapter = new SqlDataAdapter(); // Create the InsertCommand. command = new SqlCommand( "INSERT INTO Tempname(ResourceName) " + "VALUES (@ResourceName)", connection); // Add the parameters for the InsertCommand. command.Parameters.Add("@ResoureName", SqlDbType.NVarChar, 50, "ResourceName"); adapter.InsertCommand = command; return adapter; } I not able to download/integrate the CSV-Reader on this server. I will need to pursuit another route.

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

              Right, SqlBulkCopy would be a third option, but actually I've never worked with it myself. I wrote a method for you which should work for you. You should only have to adjust some minor stuff :)

              using System;
              using System.Configuration;
              using System.Data;
              using System.Data.SqlClient;
              using System.Globalization;
              using System.IO;
              using System.Windows.Forms;

              private static char[] Colon = new char[] { ',' };
              private DataTable QueryStaff()
              {
              const int nameColumnIndex = 1;
              const int hoursColumnIndex = 2;

              using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["ConsoleApplication3.Properties.Settings.daasConnectionString"\].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:\\temp\\table1.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\[nameColumnIndex\];
                              row\["Hours"\] = Decimal.Parse(columns\[hoursColumnIndex\], NumberFormatInfo.InvariantInfo);
                              dataTable.Rows.Add(row);
                          }
                      }
                  }
                  dataTable.EndLoadData();
              
                  // insert the records from the DataTable into the temporary table
                  dataAdapter.Update(dataTable);
              
                  // load the result of the "main purpose" query into the DataTable
                  dataTable.Clear();
                  cmd.CommandText = "SELECT Tmp.Name, SUM(Tmp.Hours) FROM #TempTable AS Tmp WHERE NOT EXISTS (SELECT \* FROM Stafftracking AS ST WHERE Tmp.Name = ST.Name) GROUP BY Tmp.Name;";
                  da
              
              N 1 Reply Last reply
              0
              • S Sascha Lefevre

                Right, SqlBulkCopy would be a third option, but actually I've never worked with it myself. I wrote a method for you which should work for you. You should only have to adjust some minor stuff :)

                using System;
                using System.Configuration;
                using System.Data;
                using System.Data.SqlClient;
                using System.Globalization;
                using System.IO;
                using System.Windows.Forms;

                private static char[] Colon = new char[] { ',' };
                private DataTable QueryStaff()
                {
                const int nameColumnIndex = 1;
                const int hoursColumnIndex = 2;

                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["ConsoleApplication3.Properties.Settings.daasConnectionString"\].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:\\temp\\table1.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\[nameColumnIndex\];
                                row\["Hours"\] = Decimal.Parse(columns\[hoursColumnIndex\], NumberFormatInfo.InvariantInfo);
                                dataTable.Rows.Add(row);
                            }
                        }
                    }
                    dataTable.EndLoadData();
                
                    // insert the records from the DataTable into the temporary table
                    dataAdapter.Update(dataTable);
                
                    // load the result of the "main purpose" query into the DataTable
                    dataTable.Clear();
                    cmd.CommandText = "SELECT Tmp.Name, SUM(Tmp.Hours) FROM #TempTable AS Tmp WHERE NOT EXISTS (SELECT \* FROM Stafftracking AS ST WHERE Tmp.Name = ST.Name) GROUP BY Tmp.Name;";
                    da
                
                N Offline
                N Offline
                Norris Chappell
                wrote on last edited by
                #12

                Sascha, I really appreciate you taking your valuable time to write this method. I will make the adjustments and let you know how it comes out. I was going to be working on this all weekend. I am going to ask my Manager to send me to some C# classes. Thank you again, Norris

                S 1 Reply Last reply
                0
                • N Norris Chappell

                  Sascha, I really appreciate you taking your valuable time to write this method. I will make the adjustments and let you know how it comes out. I was going to be working on this all weekend. I am going to ask my Manager to send me to some C# classes. Thank you again, Norris

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

                  Norris Chappell wrote:

                  I was going to be working on this all weekend.

                  Maybe invest a part of the time I (hopefully) saved you by investigating how it works and trying to understand things that are new for you :) ... F1, F12 and debugging step by step are your friends ;)

                  Norris Chappell wrote:

                  and let you know how it comes out

                  Yes please do.

                  Norris Chappell wrote:

                  I am going to ask my Manager to send me to some C# classes.

                  You obviously have to know for yourself but if you'd ask me, I'd say let him give you the money to buy some good books and some time to work them through. More than one book because one may explain something in a way that's better understandable than the other and vice versa. If then still something is unclear, Google is the next step. And if you can't find the answer with Google, you could ask here. cheers, Sascha edit: Just realized there's a small flaw in my code because it's not 100% the same as the version I tested. But I'm sure you can fix it :)

                  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

                    Norris Chappell wrote:

                    I was going to be working on this all weekend.

                    Maybe invest a part of the time I (hopefully) saved you by investigating how it works and trying to understand things that are new for you :) ... F1, F12 and debugging step by step are your friends ;)

                    Norris Chappell wrote:

                    and let you know how it comes out

                    Yes please do.

                    Norris Chappell wrote:

                    I am going to ask my Manager to send me to some C# classes.

                    You obviously have to know for yourself but if you'd ask me, I'd say let him give you the money to buy some good books and some time to work them through. More than one book because one may explain something in a way that's better understandable than the other and vice versa. If then still something is unclear, Google is the next step. And if you can't find the answer with Google, you could ask here. cheers, Sascha edit: Just realized there's a small flaw in my code because it's not 100% the same as the version I tested. But I'm sure you can fix it :)

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

                    Here is my code and it still don't work?

                    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
                    {
                    private static char[] Colon = new char[] { ',' };
                    private DataTable QueryStaff()
                    {
                    const int nameColumnIndex = 1;
                    const int hoursColumnIndex = 2;

                            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\\FinalLabor2015.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\[nameColumnIndex\];
                                            row\["Hours"\] = Decimal.Parse(columns\[hoursColumnIndex\], NumberFormatInfo.InvariantInfo);
                                            dataTable.Rows.Add(row);
                                            
                                        }
                                        
                                    }
                                }
                                dataTable.EndLoadData();
                                
                                // insert the records fr
                    
                    S 1 Reply Last reply
                    0
                    • N Norris Chappell

                      Here is my code and it still don't work?

                      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
                      {
                      private static char[] Colon = new char[] { ',' };
                      private DataTable QueryStaff()
                      {
                      const int nameColumnIndex = 1;
                      const int hoursColumnIndex = 2;

                              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\\FinalLabor2015.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\[nameColumnIndex\];
                                              row\["Hours"\] = Decimal.Parse(columns\[hoursColumnIndex\], NumberFormatInfo.InvariantInfo);
                                              dataTable.Rows.Add(row);
                                              
                                          }
                                          
                                      }
                                  }
                                  dataTable.EndLoadData();
                                  
                                  // insert the records fr
                      
                      S Offline
                      S Offline
                      Sascha Lefevre
                      wrote on last edited by
                      #15

                      Take a look at the very first code you posted. I don't know to which method it belonged as you didn't include the "method head". At the end you had these lines:

                      gvDataCenterLabor.DataSource = dt;
                      gvDataCenterLabor.DataBind();

                      Supposedly in the same method you would now remove all the code you originally posted and just write instead:

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

                      dt was the variable for the DataTable you originally created. Now QueryStaff() returns "that" DataTable. Please verify that const int hoursColumnIndex = 2; is correct for you (I think it's not). In the CSV you originally posted, the hours were in the last column. For my testing purposes I created a CSV with just 3 columns and the hours were in the last of it, so index 2 for me.

                      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

                        Take a look at the very first code you posted. I don't know to which method it belonged as you didn't include the "method head". At the end you had these lines:

                        gvDataCenterLabor.DataSource = dt;
                        gvDataCenterLabor.DataBind();

                        Supposedly in the same method you would now remove all the code you originally posted and just write instead:

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

                        dt was the variable for the DataTable you originally created. Now QueryStaff() returns "that" DataTable. Please verify that const int hoursColumnIndex = 2; is correct for you (I think it's not). In the CSV you originally posted, the hours were in the last column. For my testing purposes I created a CSV with just 3 columns and the hours were in the last of it, so index 2 for me.

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

                        You are correct it should be gvNewResource.DataSourse = QueryStaff(); gvNewResource,Databind(); Yes the Name is in column 2 which is 1 and hours is on column 10 which is 9.

                        S N 2 Replies Last reply
                        0
                        • N Norris Chappell

                          You are correct it should be gvNewResource.DataSourse = QueryStaff(); gvNewResource,Databind(); Yes the Name is in column 2 which is 1 and hours is on column 10 which is 9.

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

                          Just spotted: Seems like you haven't yet adjusted the column names in the last query. Please tell if it works then.

                          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

                            You are correct it should be gvNewResource.DataSourse = QueryStaff(); gvNewResource,Databind(); Yes the Name is in column 2 which is 1 and hours is on column 10 which is 9.

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

                            Here is my code that I changed. It doesn't even so me anything when I set breakpoints.

                            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
                            {
                            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\[nameColumnIndex\];
                                                    row\["Hours"\] = Decimal.Parse(columns\[hoursColumnIndex\], NumberFormatInfo.InvariantInfo);
                                                    dataTable.Rows.Add(row);
                                                    
                                                }
                                                
                                            }
                                        }
                                        dataTable.EndLoadData();
                            
                            N 1 Reply Last reply
                            0
                            • N Norris Chappell

                              Here is my code that I changed. It doesn't even so me anything when I set breakpoints.

                              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
                              {
                              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\[nameColumnIndex\];
                                                      row\["Hours"\] = Decimal.Parse(columns\[hoursColumnIndex\], NumberFormatInfo.InvariantInfo);
                                                      dataTable.Rows.Add(row);
                                                      
                                                  }
                                                  
                                              }
                                          }
                                          dataTable.EndLoadData();
                              
                              N Offline
                              N Offline
                              Norris Chappell
                              wrote on last edited by
                              #19

                              I changed the hours to 9 the name is 1.

                              N 1 Reply Last reply
                              0
                              • N Norris Chappell

                                I changed the hours to 9 the name is 1.

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

                                Sascha, It is still not working for me. Norris

                                S 1 Reply Last reply
                                0
                                • N Norris Chappell

                                  Sascha, It is still not working for me. Norris

                                  S Offline
                                  S Offline
                                  Sascha Lefevre
                                  wrote on last edited by
                                  #21
                                  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 1 Reply Last reply
                                  0
                                  • 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
                                          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