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. C# code is only inserting a few records.

C# code is only inserting a few records.

Scheduled Pinned Locked Moved C#
csharpdatabase
23 Posts 6 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

    That worked but if no record is found for someone can I just skip it and continue? Your revised sql does exactly what I wanted to do except it gives an error and the users wouldn't know what to do with it. The problem is that we have some employee that are suppliers and others direct and vise versa. this is what I am getting. No staff tracking record found for resource 'ALISON GREEN'. Cannot insert the value NULL into column 'StaffTrackingID', table 'SP2010_EDCStaffing_AppDB.dbo.StaffTrackingFTEData'; column does not allow nulls. INSERT fails. The statement has been terminated. The Field in StaffTracking to determine if a person is a direct or Supplier is PersonnelResourceType. I changed the above code to include in the where clause SELECT @StaffTrackingID = ID FROM StaffTracking WHERE CATWResourceName = @Name and PersonnelResourceType = 'Supplier'; but still getting that message.

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #21

    const string Query = @"DECLARE @StaffTrackingID int;

    SELECT @StaffTrackingID = ID FROM StaffTracking WHERE CATWResourceName = @Name;

    If @@ROWCOUNT != 0 And NOT EXISTS (SELECT 1 FROM StaffTrackingFTEData WHERE StaffTrackingID = @StaffTrackingID And Period = @Period)
    INSERT INTO StaffTrackingFTEData ([StaffTrackingID], [EstimateHours], [EstimateFTE], [ActualHours], [ActualFTE],[Comment], [CommentBy], [Period])
    VALUES (@StaffTrackingID, @Estimatehours, @EstimateFTE, @ActualHours, @ActualFTE, @Comment, @CommentBy, @Period)
    ";


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    N 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      const string Query = @"DECLARE @StaffTrackingID int;

      SELECT @StaffTrackingID = ID FROM StaffTracking WHERE CATWResourceName = @Name;

      If @@ROWCOUNT != 0 And NOT EXISTS (SELECT 1 FROM StaffTrackingFTEData WHERE StaffTrackingID = @StaffTrackingID And Period = @Period)
      INSERT INTO StaffTrackingFTEData ([StaffTrackingID], [EstimateHours], [EstimateFTE], [ActualHours], [ActualFTE],[Comment], [CommentBy], [Period])
      VALUES (@StaffTrackingID, @Estimatehours, @EstimateFTE, @ActualHours, @ActualFTE, @Comment, @CommentBy, @Period)
      ";


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

      Thanks that worked. Really appreciated it.

      1 Reply Last reply
      0
      • N Norris Chappell

        Can you provide a snippet of what you are talking about? So this is not correct?

        protected void SubmitButton_Click(object sender, EventArgs e)
        {

                    const string Query = "INSERT INTO StaffTrackingFTEData (\[StaffTrackingID\], \[EstimateHours\], \[EstimateFTE\], \[ActualHours\], \[ActualFTE\],\[Comment\], \[CommentBy\], \[Period\]) VALUES ((Select StaffTracking.ID From StaffTracking where StaffTracking.CATWResourceName = @Name), @Estimatehours, @EstimateFTE, @ActualHours, @ActualFTE, @Comment, @CommentBy, @Period)";
                    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLStaffingConn"\].ConnectionString);
        
                    using (SqlCommand command = new SqlCommand(Query, conn))
                    {
                        conn.Open();
                        command.Parameters.AddWithValue("@StaffTrackingID", SqlDbType.Int).Direction = ParameterDirection.Output;
                        foreach (GridViewRow row in gvCATW.Rows)
                        {
        
        
                            command.Parameters.AddWithValue("@Name", ((TextBox)row.FindControl("txtName")).Text); 
        

        .....
        .....
        .....

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #23

        Yes, that is not corect.

        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