Solved

How to correct this error and why it comes.

Posted on 2012-04-06
3
227 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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