Tuesday, December 15, 2015

How to read a text file, with asp.net and then execute a string comapre out of SQl database

private void codecompare(string state_in)
        {
            string fileName = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath) + @"\institutioncodes.txt";

            if (!string.IsNullOrEmpty(fileName))
            {
                string lineOfText = "";
                var filestream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                var file = new System.IO.StreamReader(filestream, System.Text.Encoding.UTF8, true, 128);

                string institution_name = "";
                string institution_city = "";
                string institution_state = "";

                string fice = "";
                string ipedsunit = "";
                string actcode = "";

                string colleage_id = "";
                string name = "";
                string city = "";

                int i = 0;
                int count = 0;

                string database = GetRecord(state_in);
                string[] sqlrows = database.Split('~');

                while ((lineOfText = file.ReadLine()) != null) //Looping each line of file
                {
                    if (i != 0)
                    {
                        string[] datacolumn = lineOfText.Split('|');

                        institution_name = datacolumn[0];
                        institution_city = datacolumn[3];
                        institution_state = datacolumn[4];

                        fice = datacolumn[7];
                        ipedsunit = datacolumn[8];
                        actcode = datacolumn[9];
                      
                        if (institution_state == state_in)
                        {
                           
                            //if (institution_city != "")
                            //{

                                if (database != "")
                                {
                                    
                                    foreach (string row in sqlrows)
                                    {

                                        string[] school_row = row.Split('|');

                                        colleage_id = school_row[0]; //Colleague ID
                                        name = school_row[1].ToUpper(); //School Name
                                        city = school_row[2].ToUpper(); //City
                                                                               //string state = school_row[4]; //State

                                       // if (city == institution_city)
                                        //{
                                            if (name == institution_name)
                                            {
                                                //Here's what to do if we have a match
                                                Response.Write(colleage_id);
                                                Response.Write("|");
                                                Response.Write(name);
                                                Response.Write("|");
                                                Response.Write(city);
                                                Response.Write("|");
                                                Response.Write(institution_state);
                                                Response.Write("|");
                                                Response.Write(fice);
                                                Response.Write("|");
                                                Response.Write(ipedsunit);
                                                Response.Write("|");
                                                Response.Write(actcode);
                                                Response.Write("|");
                                                Response.Write("<br />");
                                              
                                                count++;

                                            }
                                        //} //bottom of if city
                                    } //end foreach
                                }
                           // }//Blank City
                        }

                    } //end if

                    i++;

                    institution_name = "";
                    institution_city = "";
                    institution_state = "";

                    fice = "";
                    ipedsunit = "";
                    actcode = "";

                    colleage_id = "";
                    name = "";
                    city = "";

                } //end while

                file.Close();
                string results = "Of the total " + i.ToString() + " Records, There were a total of " + count.ToString() + " matches found.";
                Response.Write(results);
            }
        } //end function



        public string GetRecord(string institution_state) //Getting record info from database
        {

            string strSQL = "";
            string output = "";

            string ConnectString = ConfigurationManager.ConnectionStrings["ICTVIEWS"].ConnectionString;
            SqlConnection cn = new SqlConnection(ConnectString);
            cn.Open();

            strSQL = "SELECT [Colleague_Institution_ID], [Institution_Name], [Institution_City], [Institution_State] FROM Institution_Lookup ";
            strSQL += "WHERE Institution_State=@Institution_State";

            SqlCommand cmd = new SqlCommand(strSQL, cn);

            SqlParameter Institution_State = new SqlParameter();
            Institution_State.ParameterName = "@Institution_State";
            Institution_State.Value = institution_state;
            cmd.Parameters.Add(Institution_State);

            SqlDataReader rdr = cmd.ExecuteReader();

            if (rdr.HasRows == true)
            {
                int k = 0;
                while (rdr.Read())
                {
                    if (k != 0)
                    {
                        output += "~";
                    }
                    output += rdr["Colleague_Institution_ID"].ToString();
                    output += "|";
                    output += rdr["Institution_Name"].ToString();
                    output += "|";
                    output += rdr["Institution_City"].ToString();
                    //output += "|";
                    //output += rdr["Institution_State"].ToString();
                    k++;

                }
                rdr.Close();
                cn.Close();
            }
            else
            {
                rdr.Close();
                cn.Close();
            } //rdr.HasRows

            return output;

        } //GetRecord

    }
}

No comments:

Post a Comment