2011-09-06 3 views
0

CSV 파일을 gridview에 업로드하는 업로드 기능이 있습니다. Gridview의 첫 번째 열에 만 중복 된 레코드 레코드가 있는지 확인하고 싶습니다. 있다면 "중복 된 레코드!"와 같은 오류 메시지를 표시하고 싶습니다. btnimport1이라는 버튼을 보이지 않게하십시오. 누구든지 나를 도울 수 있습니까? 지금까지 내 코드는Gridview에서 중복을 확인하고있을 경우 오류 메시지가 표시됩니까?

//A method to display errors in the gridview 
    private string ErrorMessage(string input) 

     { 
      { 
       //if there are null values, error message will be displayed. 
       if (!string.IsNullOrEmpty(input)) 
        return input; 
       //making the button invisble, so that the user will be forced to cancel import and re-upload new file 
       BtnImport1.Visible = false; 
      } 
      return "No value entered!"; 

     } 

    protected void btnUpload_Click(object sender, EventArgs e) 
    { 

     //get the uploaded file name 
     string strFileNameOnServer = fileUpload.PostedFile.FileName; 
     //get the uploaded file's extension 
     string fileExt = 
     System.IO.Path.GetExtension(fileUpload.FileName); 



     // if the uploaded file is not null and the file extension is csv, do the try 
     if (fileUpload.PostedFile != null && fileExt == ".csv") 
     { 

      try 
      { 


       fileUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads")); 

       //to display the contents of file 
       Label1.Text = "File name: " + 
         fileUpload.PostedFile.FileName + "<br>" + 
         fileUpload.PostedFile.ContentLength + " kb<br>" + 
         "Content type: " + 
         fileUpload.PostedFile.ContentType; 
      } 
      catch (Exception ex) 
      { 
       Label1.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>. " + ex.Message; 
      } 

      //to make the import and cancel import button visible so that users can either choose to import or cancel 
      BtnImport1.Visible = true; 
      Cancel.Visible = true; 

      //to make the upload button invisible 
      fileUpload.Visible = false; 
      btnUpload.Visible = false; 

     } 
     else 
     { 
      //if the user does not select anything or the file is extension is not .csv, the error message will be displayed 
      Label1.Text = "Error - a file name must be specified/only csv files are allowed"; 
      return; 

     } 

     // to read all lines of the posted csv file and put the lines in the grid view 
     var data = File.ReadAllLines(Server.MapPath("~/Uploads")) 
      // to split the lines according to commas 
      .Select(line => line.Split(',')) 
      .Select(columns => new { GuestID =ErrorMessage(columns.Length<=8?"":columns[0]), IC_No = ErrorMessage(columns[1]), Grouping = ErrorMessage(columns[2]), Remarks = ErrorMessage(columns[3]), GuestName = ErrorMessage(columns[4]), Class_Group = ErrorMessage(columns[5]), Staff = ErrorMessage(columns[6]), Attendance_Parents_Only = ErrorMessage(columns[7]), Registration = ErrorMessage(columns.Length<=8?"":columns[8]) }); 



     myGridView.DataSource = data; 
     myGridView.DataBind(); 
     } 

첫 번째 열 (0 열)의 오류 메시지 만 확인하고 표시하고 싶습니다. 감사합니다! 위의 결과를 바탕으로

+0

을 결과 actions.Hope을 수행 삽입 및 변경이 가능하므로 사실 이후에 오류를 표시하는 대신 중복 된 레코드가 처음에는 발생하지 않습니다. –

+0

업로드는 실제로 사용자가 업로드를 허용하므로 제한 할 수 없으므로 오류 검사가 필요합니다. – Mark20

답변

0
private bool CheckDuplicates(string stringtobeChecked) 
{ 
    foreach (GridViewRow row in GridView_FtpStatus.Rows) 
    { 
     if ((row.Cells[0].Text) == stringtobeChecked) 
      return true; 
     else 
      return false; 
    } 
    return false; 
} 

나는 당신이 당신의 데이터를 관리하는 것이 훨씬 더 좋을 것이라고 생각, 그것은 여기

0

그것을위한 가장 좋은 예는 도움이

For index As Integer = 0 To GridView1.Rows.Count - 1 
      If GridView1.Rows(index).Cells(0).Text = "value" Then 
       ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "", "<script>alert('duplicate records');</script>", False) 
      End If 
     Next 
관련 문제