开发者

How to Validate a textbox+dropdowns in vb for a asp.net form

Previous question which links onto this and has any addition code ref should I forget to link any, I have set it up to email me should someone submit this form and an error occur and right now should that occur for most integer or datetime fields if they fail to validate then it will show me which fields in the email failed and what was input into them.

Problem I'm having now is to validate the drop downs and the textboxs in a similar way to what I with integer and datetime fields so I can display those also in the email in case they error.

present integer and datetime validation

Catch ex As Exception
            lblInformation.Text = ("<h4>Unable to save data in database</h4>" + vbNewLine + "The error was '" + ex.Message + "'" + vbNewLine + vbNewLine + vbNewLine + "The SQL Command which falied was:" + vbNewLine + "<strong>" + mySQL + "</strong>" + vbNewLine).Replace(vbNewLine, "<br />" + vbNewLine)

Dim dtb As DateTime
            If Not DateTime.TryParse(DateOfBirth, dtb) Then
                strEMessageBody.Append("<strong>Date Of Birth:</strong> " & DateOfBirth开发者_开发知识库Year.SelectedItem.Value & "-" & DateOfBirthMonth.SelectedItem.Value & "-" & DateOfBirthDay.SelectedItem.Value & vbCrLf)
                strEMessageBody.Append("<br/>" & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab)
            End If
Dim iao As Integer
            If Not Integer.TryParse(AnyOther, iao) Then
                strEMessageBody.Append("<strong>Any Other:</strong> " & rblAnyOther.Text & vbCrLf)
                strEMessageBody.Append("<br/>" & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab)
            End If

then below the final validation I have the Dim for the email setting but that I sorted out in the other question.

The problem is much earlier in the page I have

Sub Upload_Click(ByVal source As Object, ByVal e As EventArgs)
    If (Page.IsValid) Then
        Dim Name As String

Which prevents me just using there names as shown above where I would instead call them something else but that doesn't work with strings so my main issue is having some bit of code to check if the strings are valid and for the dropdowns which would either work but always show the data in the email or would hiccup in the code,

Dim imd As Integer
            If Not Integer.TryParse(dept, imd) Then
                strEMessageBody.Append("<strong>Department:</strong> " & dept.Text & vbCrLf)
                strEMessageBody.Append("<br/>" & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab)
            End If

below was how it had been setup to record the department Department = dept.SelectedItem.Value Department = Replace(Department, "'", "''")

Summary:- Need vb code to validate if strings and dropdowns are valid and the use of try/catch block is another possible solution but I wasn't able to figure out how to implement validation for that either.


Log your values into your database. Setup a logging table called "tblLog" or something else. Record the value of ex.Message or possibly even InnerException (if it exists).


Going hand in hand with Matt's answer, there is a tool that can help you with automatically logging errors to a DB.

It's called ELMAH.

EDIT

Here are 2 validations that you might want to use:

Dim s As String = "some user input in here" 
If [String].IsNullOrEmpty(s) Then
    ' Watch out, string is null or it is an empty string
End If

Dim cb As New ComboBox()    
If cb.SelectedItem Is Nothing Then
    ' Watch out, combo has no item selected
End If

NOTE ComboBox is a WinForm control in this example, but the idea is the same for the ASP.NET counterpart


Since everybodies given up trying to find a solution then I'm just gona close this topic with this post as the answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜