Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone , i m trying to do as :

1 > select top 1000 records (path of a html file) from sql.
2 > loop on sql result calling a thread for each row.
3 > my thread is calling a regex function passing arguments from selected rows.
4 > regex function is searching the href link from html file text and add them to data table
5 > now i need to insert data table to sql in another table using bulk copy


SO, the problem m facing is that not all records are inserted to sql.
for the same top 1000 records it some time insert 4000 rows , sometimes 2000 and some times much more.


M using threading first time .....
hope some one will help me .....with MUTEX.....Syncronise or Lock-Wait
googeled for all this but cant find worthy...


Here is my code below:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Threading;
using System.Data.SqlClient;

namespace District_Court_Threading
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        private void button1_Click(object sender, EventArgs e)
        {

        Next_records:
            string query = "select top 1000 * from [District_Court_Data] where refid not in (select distinct r_id from District_Status) order by dc_id ";
            DataTable dt = clsdatabase.FillTable(query, clsdatabase.MainDB_SqlCon);
            if (dt.Rows.Count == 0)
                return;

            foreach (DataRow dr in dt.Rows)
            {
                int DC_ID = Convert.ToInt32(dr["DC_ID"].ToString());
                string state = dr["State"].ToString().Replace(" ", " ");
                string district = dr["District"].ToString().Replace(" ", " ");
                string court = dr["Court"].ToString().Replace(" ", " ");
                string htmlfile = dr["Batch File Html Path"].ToString().Replace(" "," ");

                //Thread thread = new Thread(() => chkregex(DC_ID, state, district, court, htmlfile));
                //thread.Start();

                chkregex(DC_ID, state, district, court, htmlfile);
            }
            goto Next_records;
        }

        public void mark_status(int DC_ID, string status, int case_count)
        {
            clsdatabase.ExecuteNonQuery("insert into District_Status values ('" + DC_ID + "','" + status + "','" + case_count + "')", clsdatabase.MainDB_SqlCon);
        }
               
        public void chkregex(int ID, string st, string dt, string crt, string file_path)
        {
            if (File.ReadAllText(file_path).Contains("Total Number of cases:0"))
            {
                //search case result 0 html
                mark_status(ID, "Completed", 0);
                return;
            }
            else if (!File.ReadAllText(file_path).Contains("Total Number of cases"))
            {
                //search case result blank
                mark_status(ID, "Incomplete Html", 0);
                return;
            }
            else
            {
                string html_data = string.Empty;
                html_data = File.ReadAllText(file_path).ToString();               

                DataTable dt_cases = new DataTable();
                dt_cases.Columns.Add("RefID", Type.GetType("System.Int32"));
                dt_cases.Columns.Add("Case_No", Type.GetType("System.String"));
                dt_cases.Columns.Add("Href_Link", Type.GetType("System.String"));
                dt_cases.Columns.Add("Case_Html_Path", Type.GetType("System.String"));
                dt_cases.Columns.Add("Parent_Html_Path", Type.GetType("System.String"));
                dt_cases.Columns.Add("Case_Wget_Content", Type.GetType("System.String"));
                
            NextLink:
                string dir = @"L:\Legal\District Court - All\All_Record\" + st + "\\" + dt + "\\" + crt + "\\Cases\\";
                Match m = Regex.Match(html_data, @"<a\s*(.+?)\s*>", RegexOptions.IgnoreCase);
                if (m.Success)
                {
                    for (int i = 1; i < m.Groups.Count; i++)
                    {
                        String value = m.Groups[i].Value;
                        value = "<a hold=" />                        html_data = html_data.Replace(value, "><'));>
                        string caseno = temp;

                        dir = dir + caseno.Replace("/", "");
                        bool folderExists = Directory.Exists(dir);
                        if (!folderExists)
                            Directory.CreateDirectory(dir);

                        string casehtml=dir + "\\" + caseno.Replace("/", "") + "_.html";
                        string wget_c></a>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900