Click here to Skip to main content
16,016,770 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i m using sql connection i get the the error while i m executing the following code.
C#
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into ticket values(@pnrno,@tno,@seat)";
cmd.Connection = conn;
cmd.Parameters.Add(new SqlParameter("@pnrno", SqlDbType.Int));
cmd.Parameters["@pnrno"].Value = pnr;
cmd.Parameters.Add(new SqlParameter("@tno", SqlDbType.Int));
cmd.Parameters["@tno"].Value = tno;
 int seat = 0;
 if (clas == "firstac")
   {
  string query = "select firstac from vacseat where tno='" + tno + "'";
  SqlCommand cmd2 = new SqlCommand(query, conn);
  SqlDataReader dr = cmd2.ExecuteReader();
  if (dr.Read())
   {
   seat = int.Parse(dr["firstac"].ToString());
    }
  }
cmd.Parameters.Add(new SqlParameter("@seatno", SqlDbType.Int));
cmd.Parameters["@seatno"].Value = seat;
int no=cmd.ExecuteNonQuery();

plz look at this
Posted
Updated 9-Nov-11 6:48am
v2

1 solution

Close the reader.
C#
 if (clas == "firstac")
   {
  string query = "select firstac from vacseat where tno='" + tno + "'";
  SqlCommand cmd2 = new SqlCommand(query, conn);
  SqlDataReader dr = cmd2.ExecuteReader();
  if (dr.Read())
   {
   seat = int.Parse(dr["firstac"].ToString());
    }
  dr.Close();
  }
 
Share this answer
 
Comments
Himu from Orissa 9-Nov-11 22:30pm    
dr is closed but still i get the same exception

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