Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Note it is web application.


Validating the student id is exists in database or not for that code as follows;

C#
SCon.Con.Open();
  string str = "Select  count(*)  from Studdet where studid = " + txt_Studid.Text.ToString() + "";
  SqlCommand cmd = new SqlCommand(str, SCon.Con);
  cmd.Parameters.AddWithValue("studid", txt_Studid.Text);
   int count = 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 i enter the student id in the txt_Studid(textbox) and click the show button error as follows;

Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?


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

Regards,
Narasiman P.
Posted
Updated 1-May-13 0:21am
v2

Assuming the actual SQL statement you are trying to run is correct I would change your code as follows:

C#
SCon.Con.Open();
  string str = "Select  count(*)  from Studdet where studid = @studid";
  SqlCommand cmd = new SqlCommand(str, SCon.Con);
  cmd.Parameters.AddWithValue("@studid", Convert.ToInt(txt_Studid.Text.Trim()));


This is:
a) uses parameters to reduce changes of SQL injection,
b) allows the SQL query to actually work with the given parameter.
c) Converts the text entered to an integer value

I would also add additional checking on the information entered into the txt_Studid text box using the validating event to ensure only positive integer values have been entered but that is addition.

I would also do the following as suggested by nitin bhoyate in his solution.

C#
int count =0;
object obj = cmd.ExecuteScalar();
if(obj!=null)
{
count =convert.toint(obj.tostring());

}
 
Share this answer
 
v3
You haven't set up your parameters correctly have a read of this
http://stackoverflow.com/questions/2701506/sql-inline-query-with-parameters-parameter-is-not-read-when-the-query-is-execut[^].

Answer 1 will give you a good example on how to do this.
 
Share this answer
 
Hello,
You Just Add this code as below you problem is solve.

C#
string str = "Select count(*) from Studdet where studid = " + txt_Studid.Text.ToString() + "";
            SqlCommand cmd = new SqlCommand(str, cn);
            cn.Open();
            int count = 0;
            object obj = cmd.ExecuteScalar();
            count = Convert.ToInt32(obj);
            if (count > 0)
            {
                Label4.Text = "Correct Student id";
                return;
            }
            else
            {
                Label4.Text = "InCorrect student id is there";
                return;
            }
            cn.Close();
 
Share this answer
 
Why dont u try this...

SCon.Con.Open();
string str = "Select count(*) from Studdet where studid = " + txt_Studid.Text.ToString();
SqlCommand cmd = new SqlCommand(str, SCon.Con);
cmd.Parameters.AddWithValue("studid", Convert.ToInt32(txt_Studid.Text));
int count = cmd.ExecuteScalar();
if (count > 0)
{
Label4.Text = "Correct Student id";
return;
}
else
{
Label4.Text = "InCorrect student id is there";
return;
}
SCon.Con.Close();
 
Share this answer
 
instrad of this
C#
string str = "Select count(*) from Studdet where studid = " + txt_Studid.Text.ToString() + "";
Use
C#
string str = "Select count(*) from Studdet where studid = '" + txt_Studid.Text.ToString() + "'";

also remove
C#
cmd.Parameters.AddWithValue("studid", txt_Studid.Text);

also replace
C#
int count = cmd.ExecuteScalar();
by
C#
int count =0;
object obj = cmd.ExecuteScalar();
if(obj!=null)
{
count =convert.toint(obj.tostring());

}

here you are not using any parameter to pass the value you are passing direct value to the query.
this will solve problem
 
Share this answer
 
v4
Comments
Simon_Whale 1-May-13 6:08am    
from what you are suggesting, you are leaving the query open to SQL Injection it is something that I wouldn't recommend

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900