Insert Data in to DB Table after verification [modified]
-
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
-
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
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 methodInsertIntoDatabase
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 abool
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
-
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 methodInsertIntoDatabase
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 abool
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
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?!?!?!
-
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?!?!?!
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
-
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
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?!?!?!