ASP.net 日历绑定
ASP.net Calendar Binding
我正在尝试设置从 SQL table 中选择的日历日期。只是将 table 中的日期显示到 Calendar 上。我的问题在最后一行。
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();
}
}
}
}
}
}
此代码显示错误:
"Cannot convert Type string to datetime"
谢谢
试试这个
Calendar2.SelectedDate = DateTime.Parse(dt.Rows[0]["Symptom"].ToString());
我猜你在这里使用了错误的字段,这是一个拼写错误:
dt.Rows[0]["Symptom"].ToString()
我认为不是 Symptom
字段你想设置一个 SelectedDate
.
使用Calendar2.SelectedDate = DateTime.Now.ToString();
我正在尝试设置从 SQL table 中选择的日历日期。只是将 table 中的日期显示到 Calendar 上。我的问题在最后一行。
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();
}
}
}
}
}
}
此代码显示错误:
"Cannot convert Type string to datetime"
谢谢
试试这个
Calendar2.SelectedDate = DateTime.Parse(dt.Rows[0]["Symptom"].ToString());
我猜你在这里使用了错误的字段,这是一个拼写错误:
dt.Rows[0]["Symptom"].ToString()
我认为不是 Symptom
字段你想设置一个 SelectedDate
.
使用Calendar2.SelectedDate = DateTime.Now.ToString();