Search code examples
c#datetimeexceptionado.netinsertion

inertion of date time combobox value into database


 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;
        string x = richTextBox1.Text;
        string y = richTextBox2.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 + "','" + x + "','" + y + "' '"+ 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();
    }

I am working in a windows forms project with c# and ado. I get and exception thrown saying cannot convert date and or time from character to string, i specified the datatype in my database as time(7) and date as date....do i need to do something extra??? and this is the meesage "Conversion failed when converting date and/or time from character string"under label sql exception was unhandled.


Solution

  • find this

    namespace First_Csharp_app
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            String gender; //we have to define this
            private void button1_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                String query = "insert into data (E.id,name,surname,age,gender,DOB) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"' , '"+this.gender+"' , '"+this.DateTimePicker1.Text+"')";
                SqlCommand cmd = new sqComamnd(query,con);
                SqlDataReader dbr;
                try
                {
                    con.open();
                    dbr = cmd.ExecuteReader();
                    MessageBox.Show("saved");
                    while(dbr.read())
                    {
                    }
                }
                catch (Exception es)
                {
                    MessageBox.Show(es.Message);
                }
            }
            private void rediobutton1.checked(object sender, EventArgs e)
            {
                gender = "male";
            }
            private void rediobutton1.checked(object sender, EventArgs e)
            {
                gender = "female";
            }
        }
    }