Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please look at the following code:
C#
protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(constring);
        
        SqlCommand cmd = new SqlCommand();
       
        if (DropDownList3.SelectedItem.Text == "Economy")
        {
            seats = Convert.ToInt32(DropDownList1.SelectedItem.Text);

            cmd.Connection = con;
            con.Open();
            cmd.CommandText = "select easeats from flight where fno='" + fn + "'";
           int eds = Convert.ToInt32(cmd.ExecuteScalar());
            
           if (eds > seats)
            {
                Panel2.Visible = true;                //seats available
                cl = DropDownList3.SelectedItem.Text;  
                seat = seats.ToString();
                seats = eds;
            }

i am getting an error in the line
int eds = Convert.ToInt32(cmd.ExecuteScalar());
as:
Conversion failed when converting the varchar value to data type int.
Posted
Updated 22-Apr-12 3:55am
v2
Comments
[no name] 22-Apr-12 10:03am    
what is "fn" and what is "fno"?
Pramod Harithsa 23-Apr-12 9:22am    
Use parameterized query instead of concatination, it may lead to SQL injection.

Look at the value in fn - it is not converting to a integer to compare against fno.

BTW: Don't do it that way. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Try:
C#
cmd.CommandText = "select easeats from flight where fno=" + fn ;


Based on the error it looks like 'fno' is a int type field and so the value passed should not be in single quotes.
 
Share this answer
 
cmd.CommandText = "select easeats from flight where fno='" + fn + "'";
int eds = Convert.ToInt32(cmd.ExecuteScalar());

variable easeats might have been declared as varchar so may be the cause for error.
 
Share this answer
 
v2
ExecuteScalar returns first column of the first row in the result set returned by the query.

In your case it may be that column "easeats" data type is VARCHAR.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx[^]
 
Share this answer
 

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