Click here to Skip to main content
15,886,769 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
During csv file uploading (Fatal error encountered attempting to read the resultset).
C#
protected void btn_save(object sender, EventArgs e)
       {
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 LOCAL  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();

               }
           }
Posted
Updated 23-Apr-13 19:30pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Apr-13 1:18am    
This is not an error, this is exception, even if it is fatal. You need to provide comprehensive exception information. Do you know how?
—SA
Member 9937209 24-Apr-13 4:29am    
Sir i don't know.Please explain
Member 9937209 24-Apr-13 4:29am    
Sir i don't know.Please explain
Sergey Alexandrovich Kryukov 24-Apr-13 9:21am    
Use the debugger, run into exception and get all the information. More formally, catch all exceptions in the top stack frame of each thread and log it all: message, type, parameters, inner exceptions, importantly — StackTrace, it will show where exception is propagated. In source code, indicate the line where the exception was thrown.
—SA
AmitGajjar 24-Apr-13 1:32am    
on which line you have fatal error ?

1 solution

Such an error may be caused by your uploadQry: the file does not fit the specifications you gave. Can you load the file into the table when you use MySQL GUI tools?
LINES TERMINATED BY '/n' looks wrong what about LINES TERMINATED BY '\\n'?
 
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