2015-07-06 53 views
0

我想从SQL表中设置日历的选定日期。只是将表格中的日期显示在日历上。我的问题在最后一行。ASP.net日历绑定

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!this.IsPostBack) 
    { 
     Calendar2.SelectedDate = DateTime.Now; 
     SVCID = (string)(Session["SVCID"]); 
     CustID = (string)(Session["CUSTID"]); 
     int SVC_ID = System.Convert.ToInt32(SVCID); 
     int Cust_ID = System.Convert.ToInt32(CustID); 
     DataTable dt = new DataTable(); 
     string constr = ConfigurationManager.ConnectionStrings["lg_db"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(constr)) 
     { 
      using (SqlCommand cmd = new SqlCommand("Select * From Customers where Cust_ID = @CID", con)) 
      { 
       SqlDataAdapter sqlDa = new SqlDataAdapter(cmd); 
       cmd.Parameters.AddWithValue("@CID", CustID); 
       sqlDa.Fill(dt); 
       if (dt.Rows.Count > 0) 
       { 
        TB_Cname.Text = dt.Rows[0]["Cust_Name"].ToString(); 
        TB_Cphone.Text = dt.Rows[0]["Cust_phone"].ToString(); 
        TB_Cmobile.Text = dt.Rows[0]["Cust_Mobile"].ToString(); 
        TB_Cadd.Text = dt.Rows[0]["Cust_Add"].ToString(); 
        DDL_PType.DataTextField = dt.Rows[0]["Cust_City"].ToString(); 
        DDL_City.DataTextField = dt.Rows[0]["Cust_City"].ToString(); 
        con.Close(); 
       } 
       using (SqlCommand cmd2 = new SqlCommand("Select * From SVC where SVC_ID = @SID", con)) 
       { 
        SqlDataAdapter sqlDa2 = new SqlDataAdapter(cmd); 
        cmd2.Parameters.AddWithValue("@SID", SVC_ID); 
        sqlDa2.Fill(dt); 
        if (dt.Rows.Count > 0) 
        { 
         DDL_PType.DataTextField = dt.Rows[0]["Product_type_ID"].ToString(); 
         DDL_Model.DataTextField = dt.Rows[0]["Product_ID"].ToString(); 
         TB_serial.Text = dt.Rows[0]["SerialNumber"].ToString(); 
         TB_Symptom.Text = dt.Rows[0]["Symptom"].ToString(); 
         Calendar2.SelectedDate = dt.Rows[0]["Symptom"].ToString(); 
        } 
       } 
      } 

     } 
    } 
} 

这段代码显示了一个错误:

“无法将类型字符串到日期时间”

谢谢

+0

错误很明显。你需要一个DateTime对象。 –

回答

0

试试这个

Calendar2.SelectedDate = DateTime.Parse(dt.Rows[0]["Symptom"].ToString()); 

和I G否则你在这里使用的是错误的字段,其错别字:

dt.Rows[0]["Symptom"].ToString()我想不是Symptom字段你想设置一个SelectedDate

0

使用Calendar2.SelectedDate = DateTime.Now.ToString();