Solved

How to correct this error and why it comes.

Posted on 2012-04-06
3
223 Views
Last Modified: 2012-04-24
I have a windows form and coding in vb.net.
A textbox txttid is there.
When i enter any numbers if fetches the data based on that tid and returns the resultset.
If no data is available then it gives invalid TID as message
empties the txttid.But simultenously raises the following exception

The Query is :

s = "select isnull(sname,''),isnull(gender,''),isnull(slandline,''),isnull(smobile,''),isnull(sstate,'') from mststudents where id=" & Convert.ToInt32(TXTTID.Text.Trim()) & " and sname is  null"
            da = New SqlDataAdapter(s, con)
            dt = New DataTable
            da.Fill(dt)
            If dt.Rows.Count = 0 Then
                MessageBox.Show("Invalid TID")
                TXTTID.Text = ""
                TXTTID.Focus()
                txtuin.Text = ""
                txtuin.Enabled = False

            Else
                txtsname.Text = dt.Rows(0)(0).ToString()
            end if

Exception is System.Format  Exception. Input string was not in correct format.
0
Comment
Question by:searchsanjaysharma
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37815630
The reason you would get that error is if your call to Integer.ConvertToInt32 was passed a non-numeric string. Check that the data you are passing is correct--even a blank or empty string will raise this exception.

See #2 here:  http://www.experts-exchange.com/Programming/Languages/.NET/A_5389-5-Common-Exceptions-in-NET-and-How-to-Resolve-Them.html
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 500 total points
ID: 37815726
try this approach:

Dim ID as String
If  Int32.TryConvert(TXTTID.Text.Trim(), ID) Then

s = "select isnull(sname,''),isnull(gender,''),isnull(slandline,''),isnull(smobile,''),isnull(sstate,'') from mststudents where id=" & ID & " and sname is  null"
            da = New SqlDataAdapter(s, con)
            dt = New DataTable
            da.Fill(dt)
            If dt.Rows.Count = 0 Then
                MessageBox.Show("Invalid TID")
                TXTTID.Text = ""
                TXTTID.Focus()
                txtuin.Text = ""
                txtuin.Enabled = False

            Else
                txtsname.Text = dt.Rows(0)(0).ToString()
            end if
else
     MessageBox.Show(TXTTID.Text & " is not a valid integer)
end if

this will catch an Invalid value in the Text Box.

AW
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37885169
ok
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - For Loop Error 5 37
Can Amazon AppStream 2.0 be used for VB.NET and VB6 applications? 2 81
Visual Studio 2013 Shortcut (VB) 4 35
Put window Form inside tab page 10 26
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now