开发者

Checking for duplicates in Gridview and display error message if there are?

I have an upload function which will upload csv files into a gridview. I want to check if there are any duplicate records records in the first column of the Gridview only. If there are, I want to display an error message like "Duplicated record!" and make a button called btnimport1 invisible. Can anyone help me? This are my codes so far:

    //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 an开发者_如何学编程d 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();
       }

I want to check and display error message only for duplicates in the first column, which is column 0. Thanks!


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

Based on above result perform the resultant actions.Hope it helps


here is the best example for that,

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜