Click here to Skip to main content
11,705,993 members (58,187 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i am validating whether the user enters in the textbox1 student id is there in the database or not.

for that code as follows;


SCon.Con.Open();
       string str = "Select  count(*)  from Studdet where studid = '" + txt_Studid.Text.ToString() + "'";
       SqlCommand cmd = new SqlCommand(str, SCon.Con);
       int a = Convert.ToInt16(str);
       int count = (int)cmd.ExecuteScalar();
       if (count > 0)
       {
           Label4.Text = "Correct Student id";
           return;
       }
       else
       {
           Label4.Text = "InCorrect student id is there";
           return;
       }
          SCon.Con.Close();

in the run mode when i enters the student id in the textbox1 and click the button shows error as follows;

input string was not in a correct format


what is the problem in my above code please help me.

Regards,
Narasiman P.
Posted 30-Apr-13 21:13pm
Edited 30-Apr-13 21:21pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Remove that Single Quote
SCon.Con.Open();
        string str = "Select  count(*)  from Studdet where studid = " + txt_Studid.Text.ToString() ;
        SqlCommand cmd = new SqlCommand(str, SCon.Con);
        int a = Convert.ToInt16(str);
        int count = (int)cmd.ExecuteScalar();
        if (count > 0)
        {
            Label4.Text = "Correct Student id";
            return;
        }
        else
        {
            Label4.Text = "InCorrect student id is there";
            return;
        }
               SCon.Con.Close();
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello
If you debug your project you find error on this line
int a = Convert.ToInt16(str);
because str have "
Select count(*) from Studdet where studid
"
which is never convert to int
please remove this line and execute this you get result
..



SCon.Con.Open();
string str = "Select count(*) from Studdet where studid = '" + txt_Studid.Text.ToString() + "'";
SqlCommand cmd = new SqlCommand(str, SCon.Con);
int a = Convert.ToInt16(str);
int count = (int)cmd.ExecuteScalar();
SCon.Con.Close();
if (count > 0)
{
Label4.Text = "Correct Student id";
return;
}
else
{
Label4.Text = "InCorrect student id is there";
return;
}
</pre>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Before converting the string to int, make sure that the string is not null...
use like this
int i = String.IsNullOrEmpty(str) ? -1 : Convert.ToInt16(str);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 208
1 Sergey Alexandrovich Kryukov 142
2 Inimicos 60
3 ppolymorphe 57
4 ProgramFOX 50
0 OriginalGriff 9,037
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100