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
  1. Home
  2. General Programming
  3. C#
  4. Insert Data in to DB Table after verification [modified]

Insert Data in to DB Table after verification [modified]

Scheduled Pinned Locked Moved C#
databasedata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skanless
    wrote on last edited by
    #1

    I have written the code belowin anticipation of verifying, assigning and lastly insert the Value into a DB table. The ver ification work as inteded but if I can all it in the Insert Method or in the Submit click event it does not work. if the field contains data it saves it to the DB but no everification is done. The Validation itself works fine but when called in the insert method it does not validate and the inserts execut without validating the values. Any assistance will be greatlly apprciated.

      private bool isUserIDEmpty()
    	{
    		return(txtUserID.Text.Length==0);
    	}
    	
    private bool isFNameEmpty()
    	{
    		return(txtFName.Text.Length==0);
    	}
    
    private bool isMidNameEmpty()
    	{
    		return(txtMidInitial.Text.Length==0);
    	}
    
    private bool isLNameEmpty()
    	{
    			return(this.txtLName.Text.Length==0);
    	}
    
    private bool isSSNEmpty()
    	{
    		return(this.txtSSN.Text.Length == 0);
    	}
    
    	
          internal void ValidateAndAssignValues()
    	{
    		Queue messages = new Queue();
    		Control focusControl = null;
    
    	if(!isUserIDEmpty())
    		{
    			EmpID = this.txtUserID.Text;
    		}
    	else
    	        {
    			messages.Enqueue("Please Enter a User ID");
    			focusControl = this.txtUserID;
    		}
    
    	if (!isFNameEmpty())
    		{
    			FName = this.txtFName.Text;
    		}	
    	else
    		{
    			messages.Enqueue("Please Enter a First Name.");
    			if(focusControl == null)
    				focusControl = this.txtFName;
    		}
               if(!isSSNEmpty())
    		{//validating
    			//validate integer
    			if(isSSNNineDigits()!= true)
    			  {
    				  messages.Enqueue("You have not enter nine characters");
    				if (focusControl == null)
    					focusControl = this.txtSSN;
    			    }
    			else 
    			    {
    				try
    				   {
    					  SSN = int.Parse(this.txtSSN.Text);	
    				    }
    				catch
    				     {
    					messages.Enqueue("Please Enter a valid SSN.                                         Please ensure that you have entered numbers only.");
    				
    					if (focusControl == null)
    						focusControl = this.txtSSN;
    				}
    			      }
    		            }
    
                    if (messages.Count > 0)
    		{
    			StringBuilder sb = new StringBuilder();
    			bool first = true;
    			foreach (string message in messages)
    			{
    				if (first)
    					first = false;
    				else
    					sb.Append('\\n');
    				sb.Append(message);
    			}
    
    			lblError.Visible = true;
    			lblError.Text = sb.ToString();
    		}
    		else
    		     {
    			     lblError.Visible = false;
    			       lblError.T
    
    P 1 Reply Last reply
    0
    • S Skanless

      I have written the code belowin anticipation of verifying, assigning and lastly insert the Value into a DB table. The ver ification work as inteded but if I can all it in the Insert Method or in the Submit click event it does not work. if the field contains data it saves it to the DB but no everification is done. The Validation itself works fine but when called in the insert method it does not validate and the inserts execut without validating the values. Any assistance will be greatlly apprciated.

        private bool isUserIDEmpty()
      	{
      		return(txtUserID.Text.Length==0);
      	}
      	
      private bool isFNameEmpty()
      	{
      		return(txtFName.Text.Length==0);
      	}
      
      private bool isMidNameEmpty()
      	{
      		return(txtMidInitial.Text.Length==0);
      	}
      
      private bool isLNameEmpty()
      	{
      			return(this.txtLName.Text.Length==0);
      	}
      
      private bool isSSNEmpty()
      	{
      		return(this.txtSSN.Text.Length == 0);
      	}
      
      	
            internal void ValidateAndAssignValues()
      	{
      		Queue messages = new Queue();
      		Control focusControl = null;
      
      	if(!isUserIDEmpty())
      		{
      			EmpID = this.txtUserID.Text;
      		}
      	else
      	        {
      			messages.Enqueue("Please Enter a User ID");
      			focusControl = this.txtUserID;
      		}
      
      	if (!isFNameEmpty())
      		{
      			FName = this.txtFName.Text;
      		}	
      	else
      		{
      			messages.Enqueue("Please Enter a First Name.");
      			if(focusControl == null)
      				focusControl = this.txtFName;
      		}
                 if(!isSSNEmpty())
      		{//validating
      			//validate integer
      			if(isSSNNineDigits()!= true)
      			  {
      				  messages.Enqueue("You have not enter nine characters");
      				if (focusControl == null)
      					focusControl = this.txtSSN;
      			    }
      			else 
      			    {
      				try
      				   {
      					  SSN = int.Parse(this.txtSSN.Text);	
      				    }
      				catch
      				     {
      					messages.Enqueue("Please Enter a valid SSN.                                         Please ensure that you have entered numbers only.");
      				
      					if (focusControl == null)
      						focusControl = this.txtSSN;
      				}
      			      }
      		            }
      
                      if (messages.Count > 0)
      		{
      			StringBuilder sb = new StringBuilder();
      			bool first = true;
      			foreach (string message in messages)
      			{
      				if (first)
      					first = false;
      				else
      					sb.Append('\\n');
      				sb.Append(message);
      			}
      
      			lblError.Visible = true;
      			lblError.Text = sb.ToString();
      		}
      		else
      		     {
      			     lblError.Visible = false;
      			       lblError.T
      
      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      First of all, your code is really difficult to read. You should post it using PRE tags. Also consider indenting your code, as it is difficult to follow the flow. The method ValidateAndAssignValues() does not return a value. How does the method InsertIntoDatabase know whether the validation was successful or not? From what I can see, the database insert will occur regardless of the result of the validation. Your validation method could return a bool value that indicates whether validation was successful or not. Your calling code would then need to check this value and use it to determine whether to proceed with adding the record. One more thing, some of your error messages have spelling mistakes in them. You should consider checking your spelling before releasing your application to your users.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      S 1 Reply Last reply
      0
      • P pmarfleet

        First of all, your code is really difficult to read. You should post it using PRE tags. Also consider indenting your code, as it is difficult to follow the flow. The method ValidateAndAssignValues() does not return a value. How does the method InsertIntoDatabase know whether the validation was successful or not? From what I can see, the database insert will occur regardless of the result of the validation. Your validation method could return a bool value that indicates whether validation was successful or not. Your calling code would then need to check this value and use it to determine whether to proceed with adding the record. One more thing, some of your error messages have spelling mistakes in them. You should consider checking your spelling before releasing your application to your users.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        S Offline
        S Offline
        Skanless
        wrote on last edited by
        #3

        If you notice I wrote a bool isFieldEmpty() for every field since I need to ensure that the field has a value. If a field does not have a value then an error message is displeyd with the error message on submit. The ValidateAssign() does two thing throws the error message and assigne that value id the boolean is false. How I am not sure why these Values are not being passed to the Insert method. I am not sure hwo to crrect this. Help? Anyone?

        Skan If you knew it would not compile why didn't you tell me?!?!?!

        P 1 Reply Last reply
        0
        • S Skanless

          If you notice I wrote a bool isFieldEmpty() for every field since I need to ensure that the field has a value. If a field does not have a value then an error message is displeyd with the error message on submit. The ValidateAssign() does two thing throws the error message and assigne that value id the boolean is false. How I am not sure why these Values are not being passed to the Insert method. I am not sure hwo to crrect this. Help? Anyone?

          Skan If you knew it would not compile why didn't you tell me?!?!?!

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #4

          Skanless wrote:

          I wrote a bool isFieldEmpty() for every field since I need to ensure that the field has a value. If a field does not have a value then an error message is displeyd with the error message on submit.

          Yes, but you don't raise any exceptions if invalid data is found. The Insert still gets executed regardless of whether the validation was successful. If you don't want the Insert to be executed if the validation fails, you need to either raise an exception within the validation code or modify the calling code to check whether the validation was successful before proceeding with the Insert.

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          S 1 Reply Last reply
          0
          • P pmarfleet

            Skanless wrote:

            I wrote a bool isFieldEmpty() for every field since I need to ensure that the field has a value. If a field does not have a value then an error message is displeyd with the error message on submit.

            Yes, but you don't raise any exceptions if invalid data is found. The Insert still gets executed regardless of whether the validation was successful. If you don't want the Insert to be executed if the validation fails, you need to either raise an exception within the validation code or modify the calling code to check whether the validation was successful before proceeding with the Insert.

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            S Offline
            S Offline
            Skanless
            wrote on last edited by
            #5

            You are totally right and that's what I thought was happening but was not sure how to validate a "Void" method since nothing is ever returned. What is the best practice and how would you re-write this code to evaluate if all values have been assigned?

            Skan If you knew it would not compile why didn't you tell me?!?!?!

            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