1
Answer

How to insert the XML file in DB

Jieha Lee

Jieha Lee

7y
150
1
How to insert this XML file in SQL Server? I have done one but cannot output. The error said "String was not recognized as a valid DateTime"
 
Here the code that I done:
  1. SqlDataAdapter adpter = new SqlDataAdapter();  
  2. DataSet ds = new DataSet();  
  3. int appointment_ID = 0;  
  4. string nric = null;  
  5. string client_Name = null;  
  6. DateTime? appointment_Date = null;  
  7. DateTime? start_Time = null;  
  8. SqlConnection conn = new SqlConnection(@"Data Source=USER1-PC\SQLEXPRESS;Integrated Security=true;Database=ProductDB");  
  9. XmlReader xmlFile = XmlReader.Create("G://appointment.xml"new XmlReaderSettings());  
  10. ds.ReadXml(xmlFile);  
  11. int i = 0;  
  12. conn.Open();  
  13. for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)  
  14. {  
  15. appointment_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);  
  16. appointment_Date = DateTime.Parse(ds.Tables[0].Rows[i].ItemArray[1].ToString());  
  17. start_Time = DateTime.Parse(ds.Tables[0].Rows[i].ItemArray[2].ToString());  
  18. nric = ds.Tables[0].Rows[i].ItemArray[3].ToString();  
  19. client_Name = ds.Tables[0].Rows[i].ItemArray[4].ToString();  
  20. SqlCommand command = new SqlCommand("insert into Appointment values(" + appointment_ID + ",'" + appointment_Date + "'," + start_Time + "'," + nric + "'," + client_Name + ")", conn);  
  21. adpter.InsertCommand = command;  
  22. adpter.InsertCommand.ExecuteNonQuery();  
  23. }  
  24. conn.Close();  
  25. MessageBox.Show("Done .. ");  
Here is appointment.xml:
  1. <Interface>  
  2. <Appointment>  
  3. <Appointment_ID> 000000087508</Appointment_ID>  
  4. <Appointment_Date>2018-01-25</Appointment_Date>  
  5. <Appointment_Start_Time>13:15:00</Appointment_Start_Time>  
  6. <Client>  
  7. <NRIC>456987123</NRIC>  
  8. <Client_Name>JAMES</Client_Name>  
  9. </Client>  
  10. </Appointment>  
  11. <Appointment>  
  12. <Appointment_ID>000000087501</Appointment_ID>  
  13. <Appointment_Date>2018-01-25</Appointment_Date>  
  14. <Appointment_Start_Time>14:30:00</Appointment_Start_Time>  
  15. <Client>  
  16. <NRIC>963258741</NRIC>  
  17. <Client_Name>Bill Tan</Client_Name>  
  18. </Client>  
  19. <Client>  
  20. <NRIC>123456789</NRIC>  
  21. <Client_Name>David Ang</Client_Name>  
  22. </Client>  
  23. </Appointment>  
  24. <Checksum>2</Checksum>  
  25. </Interface>
Answers (1)
0
Master  Billa

Master Billa

NA 2.7k 0 15y
Hi

Just open visual s 2008 and navigate to code file and Press Ctrl+Alt+E

And see it will  bring the Exceptions Dialog

thank you
0
Scott

Scott

NA 2 0 15y
I had this problem too -- for some reason, the "Exceptions" menu item was not present by default in the "Debug" menu. To add it, follow these steps:

1. select Tools-->Customize from the Visual Studio main menu
2. in the resulting dialog box, switch to the "Commands" tab
3. select "Debug" from the Categories listbox on the left side of the diloag
4. locate the "Exceptions" item in the "Commands" listbox on the right side of the dialog. Now drag and drop this "Exceptions" item into the "Debug" menu
0
Stefan

Stefan

NA 624 0 15y
Sorry, I'm using Visual Studio 2008 Professional and it shows up for any type of project. Maybe someone else can help you out...

Also, try posting your question here, it is a has a forum just for Visual Studio setup and installation, and a separate forum for Visual Studio debugging.

0
John Beeman

John Beeman

NA 15 0 15y
I am looking in the Debug menu on the top menu bar. This is Visual Web Developer 2008 Express Edition to be more specific. I note this because I do see Exceptions listed if I open Visual C# for creating forms applications.

Thanks, JB
0
Stefan

Stefan

NA 624 0 15y
Hello,

You said: "nor can I find it by customizing toolbar under the debug commands".
Are you looking in the right place? The exceptions dialog is in the debug menu at the top of the screen, not in the toolbar. It should be at in the Debug menu, right after the "Attach to Process" item.