Solved

How to correct this error and why it comes.

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
ok
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

12 Experts available now in Live!

Get 1:1 Help Now