0
StringBuilder Description = new StringBuilder();
        StringBuilder Title = new StringBuilder();
        StringBuilder URL = new StringBuilder();
        SqlConnection cs = new SqlConnection("Data Source = MyPC\\SQLEXPRESS; Initial Catalog = Youtube; Integrated Security =True;");
        SqlDataAdapter da = new SqlDataAdapter();

        string url = TextBox1.Text.ToString();
        var doc = XDocument.Load(url);
        var items = doc.Descendants("item");

        XNamespace nsContent = "http://purl.org/rss/1.0/modules/content/";
        XNamespace nsfeedburner = "http://rssnamespace.org/feedburner/ext/1.0";


        cs.Open();
        foreach (var item in items)
        {
            Title.Append( item.Element("title").Value); //For Title
            var encodedContent = (string)item.Element(nsContent + "encoded");
            var decodedContent = System.Net.WebUtility.HtmlDecode(encodedContent); 
            var html = new HtmlDocument();
            html.LoadHtml(decodedContent);
            var ps = html.DocumentNode.Descendants("p");

            foreach (var p in ps)
            {
                var textContent = p.InnerText;
                Description.Append(textContent.Trim().ToString());
            }

           URL.Append((string)item.Element(nsfeedburner + "origLink"));

          da.InsertCommand = new SqlCommand("INSERT INTO datalinks VALUES (@URL, @Title, @Content)", cs);
            da.InsertCommand.Parameters.Add(new SqlParameter("@URL", SqlDbType.VarChar,250)).Value = URL.ToString();
            da.InsertCommand.Parameters.Add(new SqlParameter("@Title", SqlDbType.VarChar, 200)).Value = Title.ToString();
            da.InsertCommand.Parameters.Add(new SqlParameter("@Content", SqlDbType.VarChar, 2000)).Value = Description.ToString();


            da.InsertCommand.ExecuteNonQuery();

        }
        cs.Close();

I am using this code to insert some fetched values in my database from : http://feeds.feedburner.com/TechCrunch

The content at the source have 20 different data (urls, titles, descriptions etc.)

Its working fine but its adding the first data values in all the 20 rows every time.

What it sould really do is fetch and add all the different 20 rows of (urls, titles, descriptions) but its adding 1st row all the time. (20 times)

please help Thanks

2
  • 1
    The error is exactly what it says. Check the actual column sizes in your database. Step through in a debugger and look at the values. Try slapping a .Trim() on them to see if maybe there's extra whitespace you weren't expecting. Also, it's a good practice to wrap your SqlConnection in a using block. Commented Feb 5, 2012 at 7:36
  • 1
    You've specified the size of the parameters - but how big are the fields? (You should also consider using StringBuilder instead of repeated string concatenation... and indenting your code more clearly.) Commented Feb 5, 2012 at 7:36

1 Answer 1

3

Check to see that the field order in the table matches the order you are inserting in. Or add the field names as part of the input statement

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.