我想将三个不同的下拉列表值绑定到数据库中的单个列

时间:2013-11-11 11:05:11

标签: c# asp.net

我想将三个不同的下拉列表值绑定到数据库中的单个列中。

protected void BookNow_Click(object sender, EventArgs e)
        {
            //Session["Date"]=ddlDate.SelectedItem+ ddlMonth.SelectedItem+ddlYear.SelectedItem;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            conn.Open();
            string date = ddlYear.SelectedValue.ToString() + "/" + ddlMonth.SelectedValue.ToString() + "/" + ddlDate.SelectedValue.ToString();
            SqlCommand cmd = new SqlCommand(@"insert into OnlineBookingEvent(BookingEvent,BookeventDate,cdt,udt)values 
                             (@BookingEvent,@BookeventDate,@cdt,@udt)", conn);
                cmd.Parameters.AddWithValue("@BookingEvent", ddlEventName.SelectedItem);
                cmd.Parameters.AddWithValue("@BookeventDate",date);
                cmd.Parameters.AddWithValue("@cdt", System.DateTime.Now);
                cmd.Parameters.AddWithValue("@udt", System.DateTime.Now);
                cmd.ExecuteNonQuery();
            }

2 个答案:

答案 0 :(得分:2)

我认为您需要以数据格式传递“@ BookeventDate”参数,请尝试以下代码:

 protected void BookNow_Click(object sender, EventArgs e)
        {
            //Session["Date"]=ddlDate.SelectedItem+ ddlMonth.SelectedItem+ddlYear.SelectedItem;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            conn.Open();

            //string date = ddlYear.SelectedValue.ToString() + "/" + ddlMonth.SelectedValue.ToString() + "/" + ddlDate.SelectedValue.ToString();
            int year = Convert.ToInt32(ddlYear.SelectedValue.ToString());
            int month = Convert.ToInt32(ddlMonth.SelectedValue.ToString());
            int day = Convert.ToInt32(ddlDate.SelectedValue.ToString());
            DateTime date = new DateTime(year, month, day);

            SqlCommand cmd = new SqlCommand(@"insert into OnlineBookingEvent(BookingEvent,BookeventDate,cdt,udt)values 
                             (@BookingEvent,@BookeventDate,@cdt,@udt)", conn);
            cmd.Parameters.AddWithValue("@BookingEvent", ddlEventName.SelectedItem);
            cmd.Parameters.AddWithValue("@BookeventDate", date);
            cmd.Parameters.AddWithValue("@cdt", System.DateTime.Now);
            cmd.Parameters.AddWithValue("@udt", System.DateTime.Now);
            cmd.ExecuteNonQuery();
        }

答案 1 :(得分:0)

您应该以datetime格式转换日期参数 string date = Convert.toDateTime(ddlYear.SelectedValue.ToString()+" /" + ddlMonth.SelectedValue.ToString()+" /" + ddlDate.SelectedValue.ToString() ); 要么 string date = DateTime.parse(ddlYear.SelectedValue.ToString()+" /" + ddlMonth.SelectedValue.ToString()+" /" + ddlDate.SelectedValue.ToString() );