Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to correct this error and why it comes.

Posted on 2012-04-06
3
Medium Priority
?
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

721 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