प्रकार का मान कैसे डालें DateTime [डुप्लिकेट]

Nov 30 2020

मेरी तालिका में, मेरे पास दो कॉलम हैं StartedDateऔर EndDate, दोनों को Dateडेटाटाइप के रूप में परिभाषित किया गया है ।

मैं इन दो कॉलमों के लिए टाइप तिथि के पाठ क्षेत्र के माध्यम से मान डालने की कोशिश कर रहा हूं जैसा कि नीचे दिखाया गया है:

<asp:Textbox type="date" ID="startDate" runat="server"></asp:Textbox>
<asp:Textbox type="date" ID="endDate"  runat="server"></asp:Textbox>

मैं इन टेक्स्टबॉक्स से मान प्राप्त करने और उन्हें अपने डेटाबेस में सम्मिलित करने का प्रयास कर रहा हूं।

यह सम्मिलित करने के लिए मेरा कोड है:

// Insert a new row in the Task table
SqlDataSource1.InsertParameters["Task"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtTask")).Text;
SqlDataSource1.InsertParameters["StartedDate"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("startDate")).Text;
SqlDataSource1.InsertParameters["EndDate"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("endDate")).Text;
SqlDataSource1.InsertParameters["Done"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("DoneCbx")).Checked == true ? "true" : "false";
SqlDataSource1.InsertParameters["Priority"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("PriorityTxt")).Text;

// Method to execute the insert 
SqlDataSource1.Insert();

सम्मिलित दिनांक फ़ील्ड को छोड़कर, ठीक काम करता है क्योंकि टेक्स्टबॉक्स से प्राप्त मूल्य को परिवर्तित करने की आवश्यकता है DateTime

मैंने कोशिश की Convert.ToDateTimeऔर Datetime.Parseतरीके, लेकिन दोनों बार मुझे निम्नलिखित त्रुटि मिलती है:

.D System.DateTime ’को not स्ट्रिंग’ में परिवर्तित नहीं कर सकते

जवाब

Backs Nov 30 2020 at 11:00

DateTime.Parse विधि:

SqlDataSource1.InsertParameters["StartedDate"].DefaultValue = DateTime.Parse(((TextBox)GridView1.FooterRow.FindControl("startDate")).Text);