CracLock
CracLock

Reputation: 663

Error while adding data in SQL Server in c#?

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

Upvotes: 0

Views: 318

Answers (1)

Sparky
Sparky

Reputation: 15105

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

Upvotes: 3

Related Questions