1

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

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();
            }
4

2 回答 2

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();
        }
于 2013-11-11T11:50:31.150 回答
0

您应该将日期参数转换为日期时间格式字符串 date = Convert.toDateTime(ddlYear.SelectedValue.ToString() + "/" + ddlMonth.SelectedValue.ToString() + "/" + ddlDate.SelectedValue.ToString()); 或字符串日期 = DateTime.parse(ddlYear.SelectedValue.ToString() + "/" + ddlMonth.SelectedValue.ToString() + "/" + ddlDate.SelectedValue.ToString());

于 2014-03-24T21:10:32.290 回答