Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2006, 04:03 AM
Registered User
 
Join Date: Feb 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML into SQL

i m new to C# and im trying to import a xml file from local disk and load all the elements into a sql table .. so here is my code and i keep gettin this error Update Object must implement IConvertible

Code:
public static int LoadExternal(string Filename)
        {
            int Sflag = 0;

            SqlConnection SecondSiteCon = new SqlConnection(GlobalConstants.SecondSiteConnectionString);

            XmlDocument AccountHoldersxd = new XmlDocument();
            XmlNode ndAccountHolders;

            AccountHoldersxd.Load(Filename);

            XPathNavigator nav;


            ndAccountHolders = AccountHoldersxd["AccountHolders"];

            nav = AccountHoldersxd.CreateNavigator();
            nav.MoveToRoot();
            nav.MoveToFirstChild();

            try
            {
                SecondSiteCon.Open();

                SqlCommand AddToDB = new SqlCommand("Exec AddNewAccountHolder @AccountHolder, @FirstName, @LastName, @Gender, @BirthDate, @CreditCardNumber, @ExpiryDate", SecondSiteCon);
                AddToDB.Parameters.Add("@AccountNumber", SqlDbType.Int);
                AddToDB.Parameters.Add("@FirstName", SqlDbType.VarChar);
                AddToDB.Parameters.Add("@LastName", SqlDbType.VarChar);
                AddToDB.Parameters.Add("@Gender", SqlDbType.Char);
                AddToDB.Parameters.Add("@BirthDate", SqlDbType.DateTime);
                AddToDB.Parameters.Add("@CreditCardNumber", SqlDbType.VarChar);
                AddToDB.Parameters.Add("@ExpiryDate", SqlDbType.DateTime);

                AddToDB.CommandType = CommandType.StoredProcedure;

                AddToDB.Transaction = SecondSiteCon.BeginTransaction();

                try
                {    
                    if (ndAccountHolders != null)
                    {
                        XmlNode ndAccountHolder;
                        ndAccountHolder = ndAccountHolders.FirstChild;


                        //                    foreach(XmlNode nd in ndAccountHolders.ChildNodes)
                        //                    {
                        //                    string mCreditCardNum;
                        //                    mCreditCardNum = nd["CreditCardNumber"].Value.ToString();
                        //                    string EncryptedCCNum;
                        //                    EncryptedCCNum = DESLibrary.ToBase64String(mCreditCardNum,"AABBCCDD");
                        while(ndAccountHolder != null)
                        {
                            AddToDB.Parameters["@AccountNumber"].Value = ndAccountHolder["AccountNumber"];
                            AddToDB.Parameters["@FirstName"].Value = ndAccountHolder["FirstName"];
                            AddToDB.Parameters["@LastName"].Value = ndAccountHolder["LastName"];
                            AddToDB.Parameters["@Gender"].Value = ndAccountHolder["Gender"];
                            AddToDB.Parameters["@BirthDate"].Value = ndAccountHolder["BirthDate"];
                            AddToDB.Parameters["@CreditCardNumber"].Value = ndAccountHolder["CreditCardNumber"];
                            AddToDB.Parameters["@ExpiryDate"].Value = ndAccountHolder["ExpiryDate"];

                            ndAccountHolder = ndAccountHolder.NextSibling;


                            AddToDB.ExecuteNonQuery();

                            AddToDB.Transaction.Commit();
                        }            

                                    //                    }
                    }


                }
                catch (Exception ex)
                {
                    AddToDB.Transaction.Rollback();
                    throw ex;
                }

            }
            finally
            {
                SecondSiteCon.Close();
            }

        return Sflag;

           }


 
Old February 9th, 2006, 10:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

This error is usually related to data types not being correct.

For example: ndAccountHolder["AccountNumber"] could be a string your trying to assign to an int. Check which are wrong and convert them over.



Hal Levy
Please do your own homework.
I am here to help you, not do it for you.
I do not have sample code for anything





Similar Threads
Thread Thread Starter Forum Replies Last Post
Relational SQL Data to XML - Vet SQL/ASP - New XML JimiTheJett XML 1 December 4th, 2008 06:06 PM
SQL Server 2005 XML: FOR XML PATH -> cdata? stoves SQL Server 2005 1 July 8th, 2008 02:40 AM
SQL to XML toddw607 SQL Server 2000 1 March 20th, 2007 03:39 AM
Export SQL To XML shillr SQL Server 2000 0 August 1st, 2006 10:14 AM
XML dataisland / sql XueXue XML 2 November 11th, 2004 02:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.