0

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
        {
            get
            {
                if (ViewState["file_name"] == null)
                    return "";
                return (string)ViewState["file_name"];
            }
            set { ViewState["file_name"] = value; }
        }

        protected void PageLoad(object sender, EventArgs e)
        {
            //Response.Redirect("Potvrda.aspx");
        }

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

                string filepath = FileUpload1.PostedFile.FileName;

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

                Regex r = new Regex(pat);

                //run

                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 = "";

            try
            {
                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(), " ");

                    populateTable("");

                    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";


            connSql.Open();

            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;
            commSql.Parameters.Add(novred1);



            try
            {
                commSql.ExecuteNonQuery();
            }

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

            connSql.Close();

        }

    }

}

Edited by __avd: Corrected [code] tags.

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by __avd
0

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.

0

Please remove try..catch and see what happen.

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

StreamReader objStreamReader = File.OpenText(filename);
0

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!

0

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.