Click here to Skip to main content
15,997,917 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Guys,

Check this code,
C#
string file = FileUpload1.PostedFile.FileName;
        file = Path.GetFileName(file);
        string filepath = Server.MapPath("~/csv/")+file;
        FileUpload1.SaveAs(filepath); 
        StreamReader sr = new StreamReader(filepath);
        string line = sr.ReadLine();
        string[] value = line.Split(';');
        DataTable dt = new DataTable();
        DataRow row;
        foreach (string dc in value)
        {
            dt.Columns.Add(new DataColumn(dc));
        }
        while (!sr.EndOfStream )
        {
            value = sr.ReadLine().Split(';');
            if(value.Length == dt.Columns.Count)
            {
                row = dt.NewRow();
                row.ItemArray = value;
                dt.Rows.Add(row);
            }
        }

        string sql = "Insert into company(CompanyName,ContactPerson,Email,Tel,Address,DemurrageOffrered)values(@CompanyName,@ContactPerson,@Email,@Tel,@Address,@DemurrageOffered)";
        sc.Open();
        foreach (DataRow r in dt.Rows)
        {
            MySqlCommand cmd = sc.CreateCommand();
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@CompanyName", r["CompanyName"]);
            cmd.Parameters.AddWithValue("@ContactPerson", r["ContactPerson"]);
            cmd.Parameters.AddWithValue("@Email", r["Email"]);
            cmd.Parameters.AddWithValue("@Tel", r["Tel"]);
            cmd.Parameters.AddWithValue("@Address", r["Address"]);
            cmd.Parameters.AddWithValue("@DemurrageOffered", r["DemurrageOffered"]);

        }
        sc.Close();
        Response.Write("ook");

It is executing with "NO ERRORS" but the data is not inserted into mysql database.
It is reading the the csv file into datatable but not inserting to mysqldatabase.

Please help!!! :)

Thanks and regards
Harsha
Posted
Updated 4-Jul-12 23:37pm
v2

1 solution

You never executed the command. Please see:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx[^].

Please see the section "Methods". You need to call one of the methods with the name containing the sub-string "Execute". As your command is "Insert", you can use System.Data.SqlClient.SqlCommand.ExecuteNonQuery in your particular case, pleas see:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx[^].

—SA
 
Share this answer
 
v5
Comments
Harsha Dev 5-Jul-12 2:42am    
ooops how can i miss that... :( thank you sir :)
Sergey Alexandrovich Kryukov 5-Jul-12 2:43am    
We all do such mistakes sometimes.
You are welcome.
Good luck, call again.
--SA
Harsha Dev 5-Jul-12 2:56am    
yeah roger that ;)
hmmm sure sir :)

Working perfectly!!! :)
Sergey Alexandrovich Kryukov 5-Jul-12 12:20pm    
No doubt...
--SA
Prasad_Kulkarni 5-Jul-12 5:46am    
My 5

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