[英]ASP.net Calendar Binding
i am trying to set the selected date of a calendar from SQL table. 我正在尝试从SQL表设置日历的选定日期。 just displaying the date from the table onto the Calendar .
只是将表格中的日期显示在Calendar上。 My problem is in the last line.
我的问题在最后一行。
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();
}
}
}
}
}
}
this code shows an error : 此代码显示错误:
"Cannot convert Type string to datetime"
“无法将类型字符串转换为日期时间”
Thank you 谢谢
try this 尝试这个
Calendar2.SelectedDate = DateTime.Parse(dt.Rows[0]["Symptom"].ToString());
and i guess you are using the wrong field here, its a typo: 我猜你在这里使用了错误的字段,这是一个错字:
dt.Rows[0]["Symptom"].ToString()
i think is not the Symptom
field you want to set a SelectedDate
. dt.Rows[0]["Symptom"].ToString()
我认为这不是您要设置SelectedDate
的Symptom
字段。
使用Calendar2.SelectedDate = DateTime.Now.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.