0

Hi Guys , I am trying to load a xml file as follows and i am getting file not found exception even though the file is on the desktop.I have checked the file name and extension they are correct , can anyonehelp please

private void button1_Click(object sender, EventArgs e)
        {  string myString;
            string sXMLFilePath = @"C:\\Dict0.xml";
            XmlDocument document = new XmlDocument();
           
           document.Load(sXMLFilePath);
           XmlNodeList nodelist = document.SelectNodes("string");
           SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
           SqlDataAdapter da = new SqlDataAdapter();

           foreach (XmlNode node in nodelist)

               //cn.ConnectionString = "Server=server;Database=RTLUser;UID=sa;";
               cn.Open();
           myString = @"INSERT INTO wordTbl(word,level)  Values('" + nodelist + "','" + "0" + "')";

           SqlCommand myCmd = new SqlCommand(myString, cn);
           myCmd.ExecuteNonQuery();

           cn.Close();

        }
    }
2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by CFusion
0

sdhawan,

if you are trying to access a file on the desktop, then the error lies in your code. Your current file location is pointing directly at the root of the C drive, not the desktop. If the file is in fact in the root of C, then there is another issue.

0

ya thanks for the reply i did figure it out. I am also getting an exception error that ExecuteNonQuery: CommandText property has not been initialized.Do you have any idea why am i getting this error .Thanks

0

My best bet, is that it is because you are using the nodelist variable instead of the node.innerText, so it's not properly creating the string...

other than that I have no idea...Have you tried setting a break point inside your foreach to make sure that all variables are properly being created?

0

I got it
I was not usinf the brackets after for each loop.ITs working now .It is suppose to be like this.

string myString = "";
            string sXMLFilePath = "C:\\Dict1.txt";
            XmlDocument document = new XmlDocument();

            document.Load(sXMLFilePath);
            XmlNodeList nodelist = document.SelectNodes("ArrayOfString/string");
            SqlConnection cn = new SqlConnection(@"Server=208.124.179.197;Database=RTLUser;UID=sa;");
            SqlDataAdapter da = new SqlDataAdapter();

            cn.Open();
            foreach (XmlNode node in nodelist)
            {
                string coll = node.InnerText.Replace("'","''");
                myString = @"INSERT INTO levelTbl(word,level)  Values('" + coll + "','" + "1" + "')";

                SqlCommand myCmd = new SqlCommand(myString, cn);
                myCmd.ExecuteNonQuery();

            }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.