[asp.net][vb] Checking a textbox contains an integer

Associate
Joined
2 Apr 2004
Posts
674
Location
Melbourne
In asp.net using vb how would I go about checking that a textbox contains an integer?

also how do I use the value of a dropdownlist web control, because when I try the following code:

Code:
emp_id = employee.selecteditem.value

it says the following:

Object reference not set to an instance of an object.

when I want to return an integer

I'm confused :confused:
 
that error means that your employee reference (ie the variable of name employee) is not yet instantiated, it that the name of the drop down box?

if employee is of type System.Web.UI.WebControls.DropDownList then SelectedValue will be an easier property to use rather than selectedItem.Value

If you know it will be of type integer than you can cast it, otherwise you ill need to check it's type and deal with it appropriately

HT
 
Code:
<asp:listbox id="Employee" runat="server" DataValueField="EmployeeId" DataTextField="fullname" Rows="1"></asp:listbox>

I have that as my dropdownlist web control. I'm trying to have a button that when clicked will insert some data into a database, I need to get EmployeeId back to put in the SQL query. I have the following code to do it.

Code:
Sub Button2_Click(sender As Object, e As EventArgs)

    dim emp_id as new integer
    dim sales_no as new integer
    dim salesdate as new date

    emp_id = employee.selecteditem.value
    sales_no = sales.text
    salesdate = calendar1.selecteddate

    'TO DO: build SQL query

End Sub

can you see where I've gone wrong?
 
Actually, you are trying to set a string to an int too, that will cause your next problem. Id also suggest usign a regular expression validator to check the value of the text box is correct, and opnly proceed with your code is the page IsValid
 
Back
Top Bottom