Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It work fine and message show(File uploaded Successfully) but csv data are not found in database.

C#
protected void btn_save(object sender, EventArgs e)
         {
             //MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
             string path = FileUpload1.PostedFile.FileName;
             string ext = Path.GetExtension(path);
             string contenttype = string.Empty;
             if (!FileUpload1.HasFile)
             {

                 ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Please select a file')</script>");  //if file uploader has no file selected

             }
             else if (FileUpload1.HasFile)
             {
                 try
                 {
                     switch (ext)
                     {
                         case ".csv":

                             contenttype = "application/vnd.ms-excel";

                             break;
                     }
                     string year = ddlyear.SelectedItem.ToString();
                     string month = ddlmonth.SelectedItem.ToString();
                     MySqlConnection exclecon = new MySqlConnection("Server=Localhost;DataBase=password1;user=root;password=nectar");
                     string insquery = "INSERT INTO sla (month, year, contenttype) VALUES(@month, @year, @contenttype)";
                     string uploadQry = "LOAD DATA INFILE '" + path + "' INTO TABLE sla1 FIELDS TERMINATED  BY ',' LINES TERMINATED BY '/n' IGNORE 1 LINES ";
                     MySqlCommand mycom = new MySqlCommand(insquery, exclecon);
                     mycom.Parameters.AddWithValue("@month", month);
                     mycom.Parameters.AddWithValue("@year", year);
                     mycom.Parameters.Add("@contenttype", MySqlDbType.VarChar).Value = contenttype;

                     MySqlCommand myCUpload = new MySqlCommand(uploadQry, exclecon);
                     exclecon.Open();
                     mycom.ExecuteNonQuery();
                     myCUpload.ExecuteNonQuery();
            
                     ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('File uploaded Successfully ')</script>");
                    
                 }
                    
                 catch (Exception ex)
                 {
                     Label4.Text = "Error: " + ex.Message.ToString();

                 }
             }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 20-Apr-13 0:33am
v2

1 solution

Hello,

Since you are not using keyword LOCAL in your query, You need to transfer the file to the server on which MySQL is running and it should be in a folder readable by MySQL process. If you do not want to transfer the file to MySQL server machine then add LOCAL keyword just after the keyword DATA.

Please refer to MySQL documentation found here[^]

Regards,
 
Share this answer
 

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