472,364 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

trying to input xml data type as string, get error "Incorrect syntax near '<'."

abehm
35
Hey everyone,
I'm trying to input a string as an xml data type into sql server, but i keep getting this error. Am I doing this incorrectly, or do i need to format the string?


Expand|Select|Wrap|Line Numbers
  1. XmlDocument mydoc = new XmlDocument();
  2. SqlCommand sqlCommand;
  3.  
  4. mydoc.Load(xmlDir + "temp.xml");
  5.  
  6. string tempXML = mydoc.OuterXml;
  7.  
  8. string sqlcom = "INSERT INTO Config.TestBooth  (TestBoothID,TestBoothName,Config) "
  9. + "VALUES (" + testBoothID + ", " + testBoothName + ", " + tempXML + ")";
  10. sqlCommand = new SqlCommand(sqlcom);
  11. sqlCommand.Connection = sql;
  12. sql.Open();
  13. sqlCommand.ExecuteNonQuery();
  14. sqlCommand.Connection.Close();
  15.  
Jun 29 '07 #1
6 3913
jhardman
3,406 Expert 2GB
the only problem is that there must be some special characters (for sql) in your string. a quote mark or parenthese or some such. print out your sqlcon to make sure. You may need to run some kind of replace function on the string before you pass it.

Let me know if this helps.

Jared
Jun 29 '07 #2
abehm
35
So I added a 'Response.Write(sqlcom);' line, and it took me to a new page that says "The XML page cannot be displayed "
"Invalid at the top level of the document. Error processing resource 'http://localhost:1322/WebSite3/Default.aspx'. Line 1,...

INSERT INTO Config.TestBooth (TestBoothID,TestBoothName,Config) VALUES (1, .326, <?xml version="1.0" encoding="utf-8"?>..."
Jun 29 '07 #3
abehm
35
if i type sqlcom in the Immediate Window it reads:
Expand|Select|Wrap|Line Numbers
  1. ""INSERT INTO Config.TestBooth (TestBoothID,TestBoothName,Config) VALUES (1, .326, <?xml version=\"1.0\" encoding=\"utf-8\"?><Config><Testbooth> <TestBoothID>1</TestBoothID><Vertical>0.340999990701675</Vertical> <Horizontal>5777</Horizontal><Diagonal>193</Diagonal> <AreaIN>0.32600000500679</AreaIN><AreaFT>0.340999990701675</AreaFT> <ScreenGain>0</ScreenGain><MeasurementType>2</MeasurementType> </Testbooth></Config>)"
  2.  
Jun 29 '07 #4
jhardman
3,406 Expert 2GB
OK, I often suggest that coders switch from "Insert" to "addnew" and I think that may help in this case, it looks like the quote marks in the xml string are messing up your insert statement. syntax for addnew is:
Expand|Select|Wrap|Line Numbers
  1. query="SELECT * FROM config.testBooth"
  2. objRS.open query, objConn, adOpenDynamic, adLockOptimistic
  3. objRS.addNew
  4. objRS("TestBoothID") = 1
  5. objRS("TestBoothName") = 0.326
  6. objRS("config") = xmlConfigString
  7. objRS.update
I know it looks longer, but it executes at the same speed. Let me know if this helps.

Jared
Jun 29 '07 #5
abehm
35
OK, I often suggest that coders switch from "Insert" to "addnew" and I think that may help in this case, it looks like the quote marks in the xml string are messing up your insert statement. syntax for addnew is:
Expand|Select|Wrap|Line Numbers
  1. query="SELECT * FROM config.testBooth"
  2. objRS.open query, objConn, adOpenDynamic, adLockOptimistic
  3. objRS.addNew
  4. objRS("TestBoothID") = 1
  5. objRS("TestBoothName") = 0.326
  6. objRS("config") = xmlConfigString
  7. objRS.update
I know it looks longer, but it executes at the same speed. Let me know if this helps.

Jared
Thanks for your help Jared, but I ended up solving the issue when I changed the sql statement to this (with the apostrophes):
Expand|Select|Wrap|Line Numbers
  1.  string sqlcom = "INSERT INTO Config.TestBooth (TestBoothID,TestBoothName,Config) " + "VALUES (" + testBoothID.ToString() + ", '" + testBoothName.ToString() + "', '" + tempXML + "')";
  2.  
Jul 3 '07 #6
try that :)
Expand|Select|Wrap|Line Numbers
  1. xml String
  2.  
  3.  
  4.  
  5. using System;
  6. using System.Collections;
  7. using System.ComponentModel;
  8. using System.Configuration;
  9. using System.Data;
  10. using System.Linq;
  11. using System.Web;
  12. using System.Web.Security;
  13. using System.Web.SessionState;
  14. using System.Web.UI;
  15. using System.Web.UI.HtmlControls;
  16. using System.Web.UI.WebControls;
  17. using System.Web.UI.WebControls.WebParts;
  18. using System.Xml.Linq;
  19. using System.Data.OleDb;
  20. using System.Data.SqlClient;
  21. using System.Data.ProviderBase;
  22. using System.Web.Mail;
  23. using System.Xml;
  24. using System.Xml.Serialization;
  25. using System.Xml.Schema;
  26. using System.Text;
  27. using System.Text.RegularExpressions;
  28. using System.IO;
  29.  
  30.  
  31.  
  32. namespace WebApplication3
  33. {
  34.     public partial class _Default : System.Web.UI.Page
  35.     {
  36.         protected void Page_Load(object sender, EventArgs e)
  37.         {
  38.  
  39.         }
  40.  
  41.  
  42.         protected void Button1_Click1(object sender, EventArgs e)
  43.         {
  44.  
  45.  
  46.  
  47.             //for List 1
  48.             for (int i = 0; i < ListSecA.Items.Count; i++)
  49.             {
  50.                 if (ListSecA.Items[i].Selected)
  51.                 {
  52.                     ListSecAValues.Text += ListSecA.Items[i].Value.ToString() + "\n";
  53.                 }
  54.             }
  55.             // For List 2
  56.             for (int x = 0; x < ListSecB.Items.Count; x++)
  57.             {
  58.                 if (ListSecB.Items[x].Selected)
  59.                 {
  60.                     ListSecBValues.Text += ListSecB.Items[x].Value.ToString() + "\n";
  61.                 }
  62.             }
  63.             // For list 3
  64.             for (int z = 0; z < ListBoxHearAbout.Items.Count; z++)
  65.             {
  66.                 if (ListBoxHearAbout.Items[z].Selected)
  67.                 {
  68.                     ListHearValues.Text += ListBoxHearAbout.Items[z].Value.ToString() + "\n";
  69.                 }
  70.             }
  71.  
  72.  
  73.  
  74.  
  75.             string DetailData = "";
  76.             DetailData += TextBoxDetail.Text + TextBoxAddress.Text + TextBoxAddress2.Text + TextBoxCity.Text + TextBoxProvince.Text + TextBoxPostalCode.Text + TextBoxFax.Text + TextBoxWebsite.Text + TextBoxAttendee.Text + ListSecAValues.Text + ListSecBValues.Text + TextBoxSecADate.Text + TextBoxSecBDate.Text + TextBoxNumberAtt.Text + TextBoxSubTotal.Text + TextBoxGst.Text + TextBoxTotalDue.Text + DropdownPayment.SelectedItem.Text + TextBoxCreditCardMonth.Text + TextBoxCreditCardYear.Text + TextBoxCreditCardName.Text + ListHearValues.Text + TextBoxComments.Text;
  77.             System.Xml.Serialization.XmlSerializer serializ = new System.Xml.Serialization.XmlSerializer(DetailData.GetType());
  78.             StringBuilder sb = new StringBuilder();
  79.             TextWriter writer = new StringWriter(sb);
  80.             serializ.Serialize(writer, DetailData);
  81.  
  82.  
  83.                 string str = "";
  84.                 SqlConnection conn = new SqlConnection(
  85.                     "Data source=SYED-PC;Initial Catalog=baass;Integrated Security=True");
  86.  
  87.                 conn.Open();
  88.                 str = "insert into Registrations (EventCode,EventDate,EventTime,EventLocation,Name,Company,Phone,Email,Details) values ('Event of Toronto','03/15/2008','09:30:00 AM','Toronto','" + TextBoxName.Text.ToString() + "','" + TextBoxCompany.Text.ToString() + "','" + TextBoxPhone.Text + "','" + TextBoxEmail.Text.ToString() + "','" + writer.ToString()+ "')";
  89.  
  90.  
  91.                 //sr = "insert into Registrations (EventCode,EventDate,EventTime,EventLocation,Name,Company,Phone,Email,Details) values ('Event of Toronto','03/15/2008','09:30:00 AM','Toronto','" + Request.Form.Get("TextBoxName.text").ToString() + "','" + Request.Form.Get("TextBoxCompany.text").ToString() + "','" + Request.Form.Get("TextBoxPhone.text") + "','" + Request.Form.Get("TextBoxEmail.text").ToString() + "','" + Request.Form.Get("TextBoxDetail.text").ToString() + "')";
  92.  
  93.  
  94.  
  95.                 SqlCommand md = new SqlCommand(str, conn);
  96.  
  97.                 //int y = md.ExecuteNonQuery();
  98.                 int g = md.ExecuteNonQuery();
  99.  
  100.  
  101.                 conn.Close();
  102.                 MailMessage mails = new MailMessage();
  103.  
  104.                 mails.To = TextBoxEmail.Text;
  105.                 //mails.Bcc = "abcdef@gmail.com";
  106.                 mails.From = "WebRegistration@BAASS.accdoc.com";
  107.                 mails.Subject = "Welcome: " + TextBoxName;
  108.  
  109.                 mails.Body = "Hello " + TextBoxName.Text +
  110.                     "\n \n \n Thank you for your registration request. You have successfully registered \n for the following BAASS Training Course(s). \n \n Series A Course(s):"
  111.                     + ListSecAValues.Text + "\n \n Date(s) for Series A Course(s): " + TextBoxSecADate.Text + " \n Time(s) for Series A Course(s): \n \n Series B Course(s):" 
  112.                     + ListSecBValues.Text + " \n \n Date(s) for Series B Course(s):" + TextBoxSecBDate.Text + "\n Time(s) for Series B Course(s): \n \n \n Registrant Name(s):" 
  113.                     + TextBoxName.Text + " \n \n \n Company Name:" + TextBoxCompany.Text + " \n Payment Method:" + DropdownPayment.SelectedItem.Text + " \n Total Amount:" 
  114.                     + TextBoxTotalDue.Text + " \n Location: BAASS Training Centre,  1200 Centre Street, Thornhill, ON L4J 3M9 \n \n \n Please visit www.baass.com/baass01.nsf/pages/Contact.html#map "+
  115.                     "for a map to the location. \n \n \n Please do not hesitate to contact us if you require further information. \n \n \n Thank you, \n \n \n Susie Pedota \n Consultant Office Manager "+
  116.                     "\n BAASS Business Solutions Inc.\n Tel: 905.660.1285 Ext. 237 \n Toronto Tel: 416.777.1285 \n Fax: 905.660.3823 \n spedota@baass.com \n www.baass.com \n Bringing Technology to Business";
  117.  
  118.                 SmtpMail.SmtpServer = "localhost";
  119.                 SmtpMail.Send(mails);
  120.  
  121.                 MailMessage myself = new MailMessage();
  122.                 myself.To = "sshah@baass.com";
  123.                 myself.From = "WebRegistration@BAASS.accdoc.com";
  124.                 myself.Subject = "BAASS Thornhill Online Course Registration Confirmation";
  125.                 myself.Body = "Hello " + TextBoxName.Text +
  126.                     "\n \n \n Thank you for your registration request. You have successfully" +
  127.                     "registered \n for the following BAASS Training Course(s). \n \n Series A Course(s):" + ListSecAValues.Text +
  128.                     "\n \n Date(s) for Series A Course(s): " + TextBoxSecADate.Text + 
  129.                     " \n Time(s) for Series A Course(s): \n \n Series B Course(s):"
  130.                     + ListSecBValues.Text + " \n \n Date(s) for Series B Course(s):" + TextBoxSecBDate.Text +
  131.                     "\n Time(s) for Series B Course(s): \n \n \n Registrant Name(s):"
  132.                     + TextBoxName.Text + " \n \n \n Company Name:" + TextBoxCompany.Text +
  133.                     " \n Payment Method:" + DropdownPayment.SelectedItem.Text + " \n Total Amount:" + TextBoxTotalDue.Text +
  134.                     " \n Location: BAASS Training Centre,  1200 Centre Street, Thornhill, ON L4J 3M9 \n \n \n Please visit "+
  135.                     "www.baass.com/baass01.nsf/pages/Contact.html#map for a map to the location. \n \n \n Please do not hesitate"+
  136.                     " to contact us if you require further information. \n \n \n Thank you, \n \n \n Susie Pedota \n Consultant Office Manager "+
  137.                     "\n BAASS Business Solutions Inc.\n Tel: 905.660.1285 Ext. 237 \n Toronto Tel: 416.777.1285 \n Fax: 905.660.3823 \n spedota@baass.com "+
  138.                     "\n www.baass.com \n Bringing Technology to Business \n \n \n \n ----------------------------------------------------------------------------------- "+
  139.                     "\n The following message was sent as a Online Course Registration request. \n Clicking on reply or reply with history will allow you to respond to this \n request. \n \n Registrant Name(s):" 
  140.                     + TextBoxName.Text + "\n \n \n Company Name: " + TextBoxCompany.Text + " \n Address 1: " + TextBoxAddress.Text + "Address 2: " + TextBoxAddress2.Text + " \n City: " + TextBoxCity.Text + " \n Province: " 
  141.                     + TextBoxProvince.Text + " \n Postal Code: " + TextBoxPostalCode.Text +
  142.                     " \n Phone Number: " + TextBoxPhone.Text + " \n Fax Number: " + TextBoxFax.Text + " \n Person's Email: " + TextBoxEmail.Text + " \n Website: " + TextBoxWebsite.Text +
  143.                     " \n Number of Attendees: " + TextBoxNumberAtt.Text + " \n \n Series A Course(s):"
  144.                     + ListSecAValues.Text + "\n \n Date(s) for Series A Course(s): " + TextBoxSecADate.Text + " \n \n Series B Course(s):" + ListSecBValues.Text + " \n \n Date(s) for Series B Course(s):" + TextBoxSecBDate.Text + " \n Subtotal Amount: " 
  145.                     + TextBoxSubTotal.Text + " \n GST Amount: " + TextBoxGst.Text + " \n Total Due: " + TextBoxTotalDue.Text + " \n Payment Method: " + DropdownPayment.SelectedItem.Text + " \n Credit Card Number: " + TextBoxCreditCard.Text + " \n Expiry Date: MONTH: "
  146.                     + TextBoxCreditCardMonth.Text + " Year: " + TextBoxCreditCardYear.Text + " \n Card Holder Name: " + TextBoxCreditCardName.Text + " \n How did you hear about this course: " + ListHearValues.Text + " \n Message: " + TextBoxComments.Text +
  147.                     " \n -----------------------------------------------------------------------------------\n";
  148.  
  149.  
  150.                 SmtpMail.SmtpServer = "Exchange";
  151.  
  152.                 SmtpMail.Send(myself);
  153.                 Response.Redirect("Thanks.aspx");
Mar 3 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
8
by: Wescotte | last post by:
The error message Parse error: syntax error, unexpected $end in FILE on line X is one I run into frequently and I know the cause is I missed an ending quote. Is there an easy way to determine...
1
mikeinspain
by: mikeinspain | last post by:
Hi Guys I am getting the following error: Parse error: syntax error, unexpected '}' in /home/9144/domains/cbweb.co.uk/html/propertyEnquiry.php on line 5 This is the referring PHP code... ...
2
by: Shrutisinha | last post by:
I am getting this error in unix .. syntax error near unexpected token `(' when i am running my parser , can anybody help me plzz .. i am new to all this thanks
1
by: fredie108 | last post by:
I've installed a link request script and when testing the link request form I get this: Parse error: syntax error, unexpected '@' in /home/mybusiness/public_html/output.php on line 91 I then...
3
by: eros | last post by:
ALTER TABLE public.postcodes ALTER COLUMN machi TYPE varchar(100); Error: ERROR: syntax error at or near "TYPE"; Error while executing the query (State:42601, Native Code: 7) I am using...
36
by: rhys | last post by:
My Gurus and Angels -- Please pardon this old-school programmer, only recently enlightened to open-source, having been trapped in the convenience of proprietary lingos for way too long. My...
3
by: silambu | last post by:
hi,can anybody tell reason for getting syntax error near , while either updating or inserting records in table
3
by: SilvaZodiac | last post by:
Hi everyone, I'm still rather new to PHP code, and I have a syntax error. I've tried several different solutions, but it won't fix. It seems to suggest that I need a new bracket somewhere in the...
10
by: benicio | last post by:
Parse error: syntax error, unexpected T_STRING, expecting '(' in C:\wamp\www\study_group\includes\functions.php on line 19 I got this error and this syntax is from 8 to 19th line. <?php ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.