I need to insert a cobobox value ,date value into database but am not sure if this is the method...i keep getting an exception saying 'Incorrect syntax near '@Numberpassengers'.'

       private void button1_Click(object sender, EventArgs e)
        {
            string c = "";
            string s = "";
            string d = "";
            string t = "";
            if (CarNameCombo.SelectedIndex >= 0 && SourceCombo.SelectedIndex>=0 && DestinationCombo.SelectedIndex>=0&&comboBox7.SelectedIndex>=0)
                c = CarNameCombo.Items[CarNameCombo.SelectedIndex].ToString();
                s = SourceCombo.Items[SourceCombo.SelectedIndex].ToString();
                d = SourceCombo.Items[DestinationCombo.SelectedIndex].ToString();
                t = comboBox7.Items[comboBox7.SelectedIndex].ToString();

            string date=dateTimePicker1.Text;
            string date1=dateTimePicker2.Text;
            SqlConnection conn = new SqlConnection("Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert into BookDetails(CarName,Source,Destination,Date,FromAddress,ToAddress,Time,Numberpassengers)VALUES(@CarName,@Source,@Destination,@Date,@FromAddress,@ToAddress,@Time,@Numberpassengers");
            cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@CarName", c);
                cmd.Parameters.AddWithValue("@Source", s);
                cmd.Parameters.AddWithValue("@Destination",d);
                cmd.Parameters.AddWithValue("@Date",date);

                cmd.Parameters.AddWithValue("@FromAddress", richTextBox1.Text);
                cmd.Parameters.AddWithValue("@ToAddress", richTextBox2.Text);

                cmd.Parameters.AddWithValue("@Time", date1);
                cmd.Parameters.AddWithValue("@Numberpassengers",t);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

someone do help me !!!

ANYONE?

pLEEEEEEEEEEEEEEEEEEEEEEZ?

IF I COMPLETE THIS....MY PROJECT IS COOMPLETED

is there any syntatical mistake ?

An easy way to see exactly what's going wrong is inserting a breakpoint on line 29 on the above code extract. Then investigate the value of the cmd query by using the text visualizer. Copy the code from there directly into sql and parsing it to sql standards. Run the query and see where the syntax error is. but just for the fun of it you're missing a ")" at the end of your values statement.

hey thanks Fenror :) i found the problem i didnt pass conn object into insert command :)

actually i thot that would work...it didnt work

Are you still getting the same exception? Did you add the ) to the following line

"insert into BookDetails(CarName,Source,Destination,Date,FromAddress,ToAddress,Time,Numberpassengers)VALUES(@CarName,@Source,@Destination,@Date,@FromAddress,@ToAddress,@Time,@Numberpassengers)"  <----

there is something wrong with my insert query....but i dont no what

I hoped this would work....but didnt either

string c = "";
            string s = "";
            string d = "";
            string t = "";
            if (CarNameCombo.SelectedIndex >= 0 && SourceCombo.SelectedIndex >= 0 && DestinationCombo.SelectedIndex >= 0 && NumberOfPassengers.SelectedIndex >= 0)
            c = CarNameCombo.Items[CarNameCombo.SelectedIndex].ToString();
            s = SourceCombo.Items[SourceCombo.SelectedIndex].ToString();
            d = SourceCombo.Items[DestinationCombo.SelectedIndex].ToString();
            t = NumberOfPassengers.Items[NumberOfPassengers.SelectedIndex].ToString();
            MessageBox.Show(""+c+s+d+t);
            string date = dateTimePicker1.Text;
            string date1 = dateTimePicker2.Text;
            MessageBox.Show("" +date +date1);
            SqlConnection conn = new SqlConnection("Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert into BookDetails(CarName,Source,Destination,Date,FromAddress,ToAddress,Time,Numberpassengers)VALUES('" + c + "','" + s + "','" + d + "','" + date + "',richTextBox1.Text,richTextBox2.Text,'" + date1 + "','" + t + "'", conn);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            //cmd.Parameters.AddWithValue("@CarName", c);
            //cmd.Parameters.AddWithValue("@Source", s);
            //cmd.Parameters.AddWithValue("@Destination", d);
            //cmd.Parameters.AddWithValue("@Date", date);
            //cmd.Parameters.AddWithValue("@FromAddress", richTextBox1.Text);
            //cmd.Parameters.AddWithValue("@ToAddress", richTextBox2.Text);
            //cmd.Parameters.AddWithValue("@Time", date1);
            //cmd.Parameters.AddWithValue("@Numberpassengers", t);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }

now i tried putting richtextbox in variables x and y now it points syntax error near time

If this is the error you are still getting 'Incorrect syntax near '@Numberpassengers'.' then remember INSERT INTO(Columns) VALUES (Values) Remember your Parenthesis ( ) ( ) you are missing the last one for VALUES you have VALUES (... add an ")"

i deserved to be hit with a book no a rock....but now there is this date time conversion error from character to string

Could you please post your question on a new thread seeing as you've marked this one as solved.

hey bro found out the solution,i just changed line 11 and 12 to this

            DateTime date = dateTimePicker1.Value;
            DateTime date1 = dateTimePicker2.Value;

and tnx a million bro

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.