how can validate empty data for text fields... i want to validate fields before inserting into database

Dani AI

Generated

Good question, and one small clarification first: your tags say ASP.NET, but the replies so far use WinForms-style code (MsgBox). The approach is a little different depending on UI. Building on , , and , here are two clean patterns you can drop in today.

ASP.NET Web Forms
Use the built-in validators so you do not repeat checks in every click handler. Group them with the Save button and still re-check on the server before insert.

<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="rfvName" runat="server"
    ControlToValidate="txtName"
    ErrorMessage="Name is required."
    Display="Dynamic" ValidationGroup="Save" />

<asp:Button ID="btnSave" runat="server" Text="Save"
    ValidationGroup="Save" OnClick="btnSave_Click" />
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Page.Validate("Save")
    If Not Page.IsValid Then Return
    ' TODO: insert with parameterized SQL here
End Sub

WinForms
Instead of chaining a bunch of OR checks (as noted by ), centralize validation and give the user precise feedback per field. Also prefer String.IsNullOrWhiteSpace (a stricter version of ’s suggestion) so inputs like " " are treated as empty.

Private Function ValidateRequired(err As ErrorProvider, ParamArray boxes() As TextBox) As Boolean
    Dim ok = True
    For Each tb In boxes
        If String.IsNullOrWhiteSpace(tb.Text) Then
            err.SetError(tb, "Required")
            ok = False
        Else
            err.SetError(tb, "")
        End If
    Next
    Return ok
End Function

Private Sub btnSave_Click(...) Handles btnSave.Click
    If Not ValidateRequired(ErrorProvider1, txtName, txtEmail, txtPhone) Then Return
    ' TODO: insert with parameterized SQL here
End Sub

Quick tips:

  • Always validate again on the server before inserting, even if you use client-side checks.
  • Trim server-side and reject whitespace-only values.
  • Use parameterized commands to avoid SQL injection while you are at it.

Recommended Answers

All 6 Replies

Plz explain the question in more detail..Only then somebody will able to answer u..Ur question is not clear

Plz explain the question in more detail..Only then somebody will able to answer u..Ur question is not clear

I have some text filed on forms. i want that when a new record is to be inserted into database and user clicks on add/save button then validation check should be made to see that all necessary fileds are filled and not empty. If any one is empty then a message is needed to be displayed..
Thanks

ok.It so easy frnd..Supose i have one textbox and on button click i m inserting the data..I want that when textbox is blank,Then msg is there..Please enter the text and record is not inserted into the database.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Trim(TextBox1.Text) = "" Then
            MsgBox("Please Enter the text")
            Exit Sub
        End If

        Call InsertData()
    End Sub

    Private Sub InsertData()
 'Do Ur Coding Here
    End Sub

Now if textbox is blank .when user clicks on button,msg is dere....

maheen plz dont post same question more than once....U put this same question twice.

ok.It so easy frnd..Supose i have one textbox and on button click i m inserting the data..I want that when textbox is blank,Then msg is there..Please enter the text and record is not inserted into the database.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Trim(TextBox1.Text) = "" Then
            MsgBox("Please Enter the text")
            Exit Sub
        End If

        Call InsertData()
    End Sub

    Private Sub InsertData()
 'Do Ur Coding Here
    End Sub

Now if textbox is blank .when user clicks on button,msg is dere....

Also, if you're looking for text in multiple textboxes then you can use an AND operator :

If txtBox1.text = ""  [b]or[/b] txtBox2.text = "" Then
            MsgBox("Please Enter the text")
            Exit Sub
        End If

        Call InsertData()

if either textbox.text is empty, the messagebox will be displayed

Comparing a string to "" actually compares with a string which has a length of zero. This is not the same thing as an empty string. Use instead String.IsNullOrEmpty() to test both cases

If String.IsNullOrEmpty(TextBox1.Text) OrElse String.IsNullOrEmpty(TextBox2.Text) Then
  MsgBox("Please Enter the text")
  Exit Sub
End If

If you want to compare more than one strings at the same time, use OrElse instead of Or. OrElse uses a C-style "shortcut evaluation" i.e. if the first value is true then it won't evaluate next expression. Ok, that's a small "optimization" and in practice has no noticeable effect in the speed of your code. But using OrElse sometimes reveals logical errors in you code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.