Click here to Skip to main content
15,891,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
total_seat = tot_seat.ExecuteScalar().ToString();

An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '1'.

What I have tried:

C#
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string w = Request.QueryString["q"];
            total_cost = Convert.ToInt16(lbl_total_price.Text);
            if (DropDownList1.SelectedValue == "1")
                ticket_price = 130;
            if (DropDownList1.SelectedValue == "2")
                ticket_price = 110;
            int s = Convert.ToInt16(DropDownList2.SelectedValue);
            ticket_price = ticket_price * s;
            txt_ticket_price.Text = ticket_price.ToString(); ;
            txt_ticket_price.Enabled = false;
            txt_combo_price.Text = total_cost.ToString();
            txt_combo_price.Enabled = false;
            txt_service_fees.Text = "10";
            txt_service_fees.Enabled = false;
            int net_payable;
            net_payable = total_cost + ticket_price;
            txt_net_payable.Text = net_payable.ToString();
            txt_net_payable.Enabled = false;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dataConnectionString"].ConnectionString);
            con.Open();
            int a = Convert.ToInt16(txt_combo_price.Text);
            int b = Convert.ToInt16(txt_ticket_price.Text);
            string c = Convert.ToString(DropDownList1.SelectedItem);
            int d = Convert.ToInt16(DropDownList2.SelectedValue);
            int f = Convert.ToInt16(txt_net_payable.Text);
            string seat_typ = "1", total_seat = "1";
            if (DropDownList1.SelectedValue == "1")
            {
                seat_typ = "select no_of_seats from booking where type='gold'";
                total_seat = "select gold from seats";
            }
            if (DropDownList1.SelectedValue == "2")
            {
                seat_typ = "select no_of_seats from booking where type='silver'";
                total_seat = "select silver from seats";
            }

            SqlCommand seat = new SqlCommand(seat_typ, con);
            SqlCommand tot_seat = new SqlCommand(total_seat, con);
            total_seat = tot_seat.ExecuteScalar().ToString();

            int t_seat = Convert.ToInt16(total_seat);
            string seat_info = seat.ExecuteScalar().ToString();
            int drp = DropDownList2.SelectedIndex;
            int s_info = Convert.ToInt16(seat_info);
            s_info = s_info + drp;
            string sql, no_of_seats, type;
            if (s_info < t_seat)
            {
                if (DropDownList1.SelectedValue == "1")
                {
                    type = "update booking set no_of_seats='" + s_info + "',seat_booked_now='gold' where type='gold'";
                    sql = "insert into booking_info (username,gold_booked,combo_cost,ticket_cost,seat_type,no_of_seats,net_payable) values('" + w + "','" + s_info + "','" + a + "','" + b + "','" + c + "','" + d + "','" + f + "')";
                }
                else
                {
                    type = "update booking set no_of_seats='" + s_info + "',seat_booked_now='silver' where type='silver'";
                    sql = "insert into booking_info (username,silver_booked,combo_cost,ticket_cost,seat_type,no_of_seats,net_payable) values('" + w + "','" + s_info + "','" + a + "','" + b + "','" + c + "','" + d + "','" + f + "')";
                }
                SqlCommand typ = new SqlCommand(type, con);
                SqlCommand cmd = new SqlCommand(sql, con);
                typ.ExecuteNonQuery();
                cmd.ExecuteNonQuery();
            }
            else
                Response.Write("not enough seats available");

        }


this is the code..please help me
Posted
Updated 15-Apr-16 22:28pm
v2
Comments
F-ES Sitecore 17-Apr-16 8:08am    
What is DropDownList1.SelectedValue? If it isn't "1" or "2" then neither of your "if" statements will change total_seat from its initial value of "1", so "1" is the SQL you're executing which isn't valid.

Learn to debug your code and step through it.

1 solution

its validation issue
change it to this

C#
SqlCommand seat = new SqlCommand(seat_typ, con);
          SqlCommand tot_seat = new SqlCommand(total_seat, con);
          string seat_typ = "1", total_seat = "1";
          if (DropDownList1.SelectedValue == "1")
          {
              seat_typ = "select no_of_seats from booking where type='gold'";
              total_seat = "select gold from seats";
              total_seat = tot_seat.ExecuteScalar().ToString();
          }
          if (DropDownList1.SelectedValue == "2")
          {
              seat_typ = "select no_of_seats from booking where type='silver'";
              total_seat = "select silver from seats";
              total_seat = tot_seat.ExecuteScalar().ToString();
          }


the dropdownlist value is different so it is taking the command as "1", so the error has occurred in sql server
 
Share this answer
 
Comments
Member 12462960 16-Apr-16 4:32am    
from my code which part should i change and add the part which u posted?

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