I want this code to work as if [flag==0] then set seatcount to 1 else provide me a message that seat is booked but it does not give me proper response


it works for seatcount==1 & shows meassage that its booked
but for seatcount==0 then also it provide same message


what should be improved so it would work properly

protected void btn1_Click(object sender, EventArgs e)
    {
        SqlDataReader sdr;
        SqlConnection con = new SqlConnection(" server = localhost;database = booking;Integrated Security=SSPI");
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT seatcount FROM seat where seatid='1'";
        SqlCommand cd = new SqlCommand(sqlUserName, con);
        //SqlCommand com = new SqlCommand(sqlUserName, Con);
        sdr = cd.ExecuteReader();
        if (sdr.Read() == false)
        {
            flag = 1;
            if (flag == 1)
            {
                SqlConnection conn = new SqlConnection(" server = localhost;database = booking;Integrated Security=SSPI");
                conn.Open();

                string updateString = @"
     update seat
     set seatcount = '1'
     where seatid = '1'";
                SqlCommand cmd = new SqlCommand(updateString);
                cmd.Connection = conn;

                cmd.ExecuteNonQuery();
                Response.Write("Record Updated");

            }
        }
        else
            Response.Write("Seat is booked");
    }

Recommended Answers

All 8 Replies


else
Response.Write("Seat is booked");

move the else a bit up....

and judging from your code... it'll never be 0...cause just before you test it against 1... you give it the value 1...

which i dont know why, unless you using it for debuggin


else
Response.Write("Seat is booked");

move the else a bit up....

and judging from your code... it'll never be 0...cause just before you test it against 1... you give it the value 1...

which i dont know why, unless you using it for debuggin

1.
      protected void btn1_Click(object sender, EventArgs e)
   2.
      {
   3.
      SqlDataReader sdr;
   4.
      SqlConnection con = new SqlConnection(" server = localhost;database = booking;Integrated Security=SSPI");
   5.
      con.Open();
   6.
      string sqlUserName;
   7.
      sqlUserName = "SELECT seatcount FROM seat where seatid='1'";
   8.
      SqlCommand cd = new SqlCommand(sqlUserName, con);
   9.
      //SqlCommand com = new SqlCommand(sqlUserName, Con);
  10.
      sdr = cd.ExecuteReader();
  11.
      if (sdr.Read() == false)//hear false means 0 test for it true then exicute rest
  12.
      {
  13.
      flag = 1;
  14.
      if (flag == 1)
  15.
      {
  16.
      SqlConnection conn = new SqlConnection(" server = localhost;database = booking;Integrated Security=SSPI");
  17.
      conn.Open();
  18.
       
  19.
      string updateString = @"
  20.
      update seat
  21.
      set seatcount = '1'
  22.
      where seatid = '1'";
  23.
      SqlCommand cmd = new SqlCommand(updateString);
  24.
      cmd.Connection = conn;
  25.
       
  26.
      cmd.ExecuteNonQuery();
  27.
      Response.Write("Record Updated");
  28.
       
  29.
      }
  30.
      }
  31.
      else
  32.
      Response.Write("Seat is booked");
  33.
      }
on debugging did u get any error...?
on debugging did u get any error...?

No I haven't receive any error but this code directly goes for else statement without cheeking if condition i want to check first the count of seat's whether its 0 so it will update to 1 & if its 1 already then show else part that's all i want to do here.
Thaks

if it is going 2 else part then there is no data in your table...

at SeatID = 1

i think you should try this query first in your sqlserver..

if it does not return any row then you're busted

else if there is data then there is some problem in coding

and instead of using rd.Read()...

always use rd.HasRows in good practices

UPDATE seat SET seatcount='1' where seatid='10';
i have tested it in sql server it works fine there.

So [rd.HasRows] to use but how
will it effect my code help by modifieng the code with [rd.HasRows]

protected void btn1_Click(object sender, EventArgs e)
    {
        
        SqlConnection con = new SqlConnection(" server = localhost;database = booking;Integrated Security=SSPI");
        con.Open();
        string sqlUserName;
        sqlUserName = "SELECT seatcount FROM seat where seatid='1'";
        SqlCommand cd = new SqlCommand(sqlUserName, con);
        //SqlCommand com = new SqlCommand(sqlUserName, Con);
        object result = cd.ExecuteScalar();
        if (result!=null)
        {
            int flag = int.Parse(result.ToString());
            if (flag == 0)
            {
                string updateString = "update seat  set seatcount = '1' where seatid = '1'";
               cd.CommandText = updateString;
               cd.ExecuteNonQuery();
                Response.Write("Record Updated");
             }
            else 
                    Response.Write("Seat is booked");
        }
        con.Open(); 
}

try the adapost's code
hope that helps..
it works same ...but holds the parallel line..!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.