0
我想通過將其轉換爲dateTime將日期插入到Session DateValue的數據庫中。我面臨的問題是它接受四月份的價值,但是當我輸入三月份的價值錯誤時。 請幫幫忙,我已經使用了查詢和代碼如下:SQL查詢插入數據到數據庫
string a = Session["Date_Value"].ToString();
DateTime date= DateTime.Parse(a);
foreach (GridViewRow g1 in grdData.Rows)
{
//string Status = (g1.FindControl("TextBox1") as TextBox).Text;
//int Status = Convert.ToInt32(Sta);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Real_Attendance"].ConnectionString);
SqlCommand cmd = new SqlCommand("Insert into Attendanc(Stu_id,Status,time,Date,Sub_id) values('" + g1.Cells[3].Text + "',@Status,'" + Session["Time_Value"].ToString() + "', @Date ,'" + Session["Sub_id"].ToString() + "')", con);
//SqlCommand cmd = new SqlCommand("Insert into Attendanc(Stu_id,Status,time,Date,Sub_id) values('" + g1.Cells[3].Text + "','"+ g1.Cells[1].Text +"','" + Session["Time_Value"].ToString() + "','" + Session["Date_Value"].ToString() + "','" + Session["Sub_id"].ToString() + "')", con);
//cmd.Parameters["@Status"].Value = ((Label)(g1.FindControl("Label1"))).Text;
cmd.Parameters.AddWithValue("@Status", ((Label)(g1.FindControl("Label1"))).Text);
cmd.Parameters.AddWithValue("@Date", date.ToShortDateString());
// cmd.Parameters.AddWithValue("@Date", date.ToShortDateString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
這是我用來創建會話的代碼。我在webform3創建會話,並在webform4
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Calendar1.Visible = false;
System.DateTime myDate = new System.DateTime();
myDate = Calendar1.SelectedDate;
txtDate.Text = myDate.ToString("dd/MM/yyyy");
Date = txtDate.Text;
day = Calendar1.SelectedDate.DayOfWeek.ToString();
Response.Write(day);
txtday.Text = day;
Session["Date_Value"] = Date;
//SelectTime();
}
你在哪裏以及如何在會話中設置日期值?請提供代碼 –
你得到哪個錯誤? – Legends
從字符串轉換日期和/或時間時轉換失敗。 – user2747666