0

I'm using this code.

 DataTable dst = new DataTable();
        dst = (DataTable)dataGridView1.DataSource;
 this.dataGridView1.Columns[2].ValueType = typeof(DateTime);

        this.dataGridView1.Columns[6].ValueType = typeof(DateTime);
        dst.TableName = "DataGridviewTpXml";
        dst.WriteXml("\\HomeCareVisit.xml", true);
        MessageBox.Show("downloaded to file");

to write from datagridview to XML and this code

for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            VisitRefNo = ds.Tables[0].Rows[i].ItemArray[0].ToString();
            PatientNo = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[1]);
            ScheduledDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[2]);
            TreatmentInstructions = ds.Tables[0].Rows[i].ItemArray[3].ToString();
            MedicalStaffID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[4]);
            Priority = ds.Tables[0].Rows[i].ItemArray[5].ToString();
            ActualVisitDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[6]);
            TreatmentProvided = ds.Tables[0].Rows[i].ItemArray[7].ToString();
            Prescription = ds.Tables[0].Rows[i].ItemArray[8].ToString();
            AdvisoryNotes = ds.Tables[0].Rows[i].ItemArray[9].ToString();

            FurtherVisitRequired = Convert.ToBoolean(ds.Tables[0].Rows[i].ItemArray[10]);
                                       FurtherVisitRequired = Convert.ToBoolean(ds.Tables[0].Rows[i].ItemArray[10]);




                            sql = "SET IDENTITY_INSERT HomeCareVisit ON update HomeCareVisit set PatientNo='" + PatientNo + "',ScheduledDateTime='" + ScheduledDateTime + "',TreatmentInstructions='" + TreatmentInstructions + "', MedicalStaffID='" + MedicalStaffID + "', Priority='" + Priority + "',ActualVisitDateTime='" + ActualVisitDateTime + "',TreatmentProvided='" + TreatmentProvided + "',Prescription='" + Prescription + "',AdvisoryNotes='" + AdvisoryNotes + "',FurtherVisitRequired='" + (FurtherVisitRequired ? "1" : "0") + "'where VisitRefNo='" + VisitRefNo + "';";

to read from xml to SQL. However it is giving me the following exception

"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated." How can this be fixed?

The application I'm trying to make stores certain details including timedate's.

IDE being used visual studio language ADO.Net c# server SQL server.

any help would be greatly appreciated, Thank you.

ADDITIONAL DETAIL I'm using the datagridview to add/edit and delete records. If that was unclear, i hope that little bit extra information helps?

4

1 回答 1

0

正如异常所说,您的字符串无法转换回日期时间,所以它可以在这个阶段

ActualVisitDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[6]);

或者在这一步

ScheduledDateTime = Convert.ToDateTime(ds.Tables[0].Rows[i].ItemArray[2]);

你检查过值吗?

为什么在再次将其转换为 sql 字符串时将其转换为日期时间?

于 2013-08-20T16:26:52.933 回答