Click here to Skip to main content
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 22:13pm
Edited 30-Apr-13 22: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>
 

 
thanks 
 
and also closed connection before return because it will un-reachable code.
if problem persist please give me your reviews.,...
  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 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2014
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