Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre23-Apr-15 6:55
professionalSascha Lefèvre23-Apr-15 6:55 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell23-Apr-15 13:21
Norris Chappell23-Apr-15 13:21 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre24-Apr-15 3:00
professionalSascha Lefèvre24-Apr-15 3:00 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell24-Apr-15 5:25
Norris Chappell24-Apr-15 5:25 
AnswerRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre24-Apr-15 11:04
professionalSascha Lefèvre24-Apr-15 11:04 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell24-Apr-15 11:57
Norris Chappell24-Apr-15 11:57 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre24-Apr-15 12:56
professionalSascha Lefèvre24-Apr-15 12:56 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 13:28
Norris Chappell25-Apr-15 13:28 
Here is my code and it still don't work?
C#
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 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;";
                dataAdapter.Fill(dataTable);

             //   gvNewResource.DataSource = dt;
             //   gvNewResource.DataBind();
                return dataTable;
            }
            
        }

    }
}


I went ahead and create a perm table. It is not doing anything.

These two lines I don't know where they belong. If I put it anywhere it gives me a error.
gvNewResource.DataSource = dt;
gvNewResource.DataBind();
The name 'dt' does not exist in the current context.
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 13:42
professionalSascha Lefèvre25-Apr-15 13:42 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 13:50
Norris Chappell25-Apr-15 13:50 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 14:02
professionalSascha Lefèvre25-Apr-15 14:02 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 14:08
Norris Chappell25-Apr-15 14:08 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 14:11
Norris Chappell25-Apr-15 14:11 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 14:13
Norris Chappell25-Apr-15 14:13 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:11
professionalSascha Lefèvre25-Apr-15 15:11 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:28
Norris Chappell25-Apr-15 15:28 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:35
Norris Chappell25-Apr-15 15:35 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:41
professionalSascha Lefèvre25-Apr-15 15:41 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:47
Norris Chappell25-Apr-15 15:47 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:56
professionalSascha Lefèvre25-Apr-15 15:56 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 15:37
professionalSascha Lefèvre25-Apr-15 15:37 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 15:50
Norris Chappell25-Apr-15 15:50 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 16:02
Norris Chappell25-Apr-15 16:02 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Sascha Lefèvre25-Apr-15 16:06
professionalSascha Lefèvre25-Apr-15 16:06 
GeneralRe: How to compare a sql server table field with a csv field and display what is not in the sql table. Pin
Norris Chappell25-Apr-15 16:15
Norris Chappell25-Apr-15 16:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.