I need to build a program which will upload a .txt file to the server, will read it using StreamReader and the populate an existing SQL table with the concent.

The first Page (profiles) does exactly that and works perfectly.. It has a text delimiter and splits the text into different columns. The second page should just read the entire thing with no delimiters and print it all in a single column named 'Info'.

I feel that I got a minor error in the syntax, so please be kind and help me with it. I still got 24 hours to finish this.

Thanks in advance!

Greetings from Macedonia!

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;

namespace TabelaUpdate

    public partial class Oglasi : System.Web.UI.Page
        public string file_name
                if (ViewState["file_name"] == null)
                    return "";
                return (string)ViewState["file_name"];
            set { ViewState["file_name"] = value; }

        protected void PageLoad(object sender, EventArgs e)

        protected void btnUpload_Click(object sender, EventArgs e)
            if (FileUpload1.HasFile)

                string filepath = FileUpload1.PostedFile.FileName;

                string pat = @"\\(?:.+)\\(.+)\.(.+)";

                Regex r = new Regex(pat);


                Match m = r.Match(filepath);

                string file_ext = m.Groups[2].Captures[0].ToString();

                string file_n = m.Groups[1].Captures[0].ToString();

                file_name = file_n + "." + file_ext;

                //save the file to the server 

                FileUpload1.PostedFile.SaveAs(Server.MapPath(".\\") + file_name);

                lblStatus.Text = "File Saved to: " + Server.MapPath(".\\") + file_name;

                btnUpload.Visible = false;

                FileUpload1.Visible = false;


        protected void btnTextInput_Click(object sender, EventArgs e)
            string OpenPath;
            string koloni;
            string line;

            Label1.Text = "";

                OpenPath = Server.MapPath(".") + @"\" + file_name;
                //OpenPath = Server.MapPath(".\\") + file_name;
                string filename = OpenPath;

                StreamReader objStreamReader = File.OpenText(filename);

                while ((line = objStreamReader.ReadLine()) != null)
                    //char[] textdelimiter = { '*' };

                    //koloni = line.Split(textdelimiter);

                    //Label1.Text += koloni[0].ToString() + "-" + koloni[1].ToString();

                    //populateTable(Convert.ToInt32(koloni[0].ToString()), koloni[1].ToString(), " ");


                    btnTextInput.Visible = false;

                    lblStatus.Visible = false;

            catch (Exception ex)
                Label1.Text += "Ne vnesovte podatok" + ex.Message;

        //void populateTable(int v_sifra, string v_text, string v_image)
        void populateTable(string v_info)

            Label1.Text += v_info + "<--- Vmetnato!";

            string cnnString = @"Data Source=\\dragan-pc\Sqlexpress;Initial Catalog=hotxxx;Integrated Security=SSPI;";
            cnnString = @"Data Source=DRAGAN-PC\SQLEXPRESS;Initial Catalog=hotxxx;Integrated Security=SSPI";

            SqlConnection connSql = new SqlConnection(cnnString);

            Label1.Text += "pomina konekcijata";


            Label1.Text += "pomina open ";

            string insertSql;

            insertSql = "INSERT INTO Oglasi (info) VALUES (@p_info)";

            SqlCommand commSql;

            commSql = new SqlCommand(insertSql, connSql);

            Label1.Text += " pomina SqlCommand <br>";

            SqlParameter novred1 = new SqlParameter();
            novred1.ParameterName = "@p_info";
            novred1.SqlDbType = SqlDbType.NVarChar;
            novred1.Value = v_info;


            catch (Exception ex)
                Label1.Text = "greshka kaj zapishuvanjeto" + ex.Message;





Edited by __avd: Corrected [code] tags.

6 Years
Discussion Span
Last Post by __avd

it executes the whole program with no problems but does not read file_name. The table is not populated. I think we should look for the error in the TextInput event. ObjStreamReader is where it's not working I think.


Please remove try..catch and see what happen.

filename = MapPath("~/" + file_name);

StreamReader objStreamReader = File.OpenText(filename);

Hah it works perfectly now.. That was odd since we did not change anything to the code.. Anyhow, thank you so very much!

I hope I won't need your help anymore :) but if I do, I'll know where to turn to! Bye and thanks again!


Thanks Lazarevski. Don't forget to mark this thread as "Solved" whenever you get proper solution.

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.