I want the user to first enter a value into a textbox insert template before selecting a value from a dropdownlist, in order to avoid a
InvalidCastException. I needed help handling this exception. Maybe assigning a default 1 will help?? Please see the code below
Protected Sub DropDownList5_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Dim ddl As DropDownList = TryCast(sender, DropDownList) Dim row As DetailsView = TryCast(DetailsView1, DetailsView) Dim tbox1 As TextBox = TryCast(row.FindControl("TextBox1"), TextBox) Dim tbox2 As TextBox = TryCast(row.FindControl("TextBox2"), TextBox) Dim tbox3 As TextBox = TryCast(row.FindControl("TextBox3"), TextBox) Dim tbox4 As TextBox = TryCast(row.FindControl("TextBox4"), TextBox) Try If tbox4 IsNot Nothing Then tbox2.Text = ddl.SelectedItem.Text tbox3.Text = ddl.SelectedValue.ToString tbox1.Text = tbox4.Text * tbox3.Text TextBox5.Text = ddl.SelectedValue.ToString End If Catch ex As InvalidCastException Dim txt As String = "Enter Quantity Ordered First" MsgBox(txt) Catch ex As Exception Dim txt As String = "Exception" MsgBox(txt) End Try End Sub