C# 是否在Gridview中检查重复项并显示错误消息(如果有)?

C# 是否在Gridview中检查重复项并显示错误消息(如果有)?,c#,asp.net,gridview,error-handling,C#,Asp.net,Gridview,Error Handling,我有一个上传功能,可以将csv文件上传到gridview中。我只想检查Gridview的第一列中是否有重复的记录。如果有,我想显示一条错误消息,如“复制的记录!”并使名为btnimport1的按钮不可见。有人能帮我吗?这是我目前的代码: //A method to display errors in the gridview private string ErrorMessage(string input) { {

我有一个上传功能,可以将csv文件上传到gridview中。我只想检查Gridview的第一列中是否有重复的记录。如果有,我想显示一条错误消息,如“复制的记录!”并使名为btnimport1的按钮不可见。有人能帮我吗?这是我目前的代码:

    //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();
       }
//在gridview中显示错误的方法
私有字符串错误消息(字符串输入)
{
{
//如果存在空值,将显示错误消息。
如果(!string.IsNullOrEmpty(输入))
返回输入;
//使按钮不可见,这样用户将被迫取消导入并重新上载新文件
BtnImport1.Visible=false;
}
返回“未输入值!”;
}
受保护的void btnUpload\u单击(对象发送方,事件参数e)
{
//获取上传的文件名
字符串strFileNameOnServer=fileUpload.PostedFile.FileName;
//获取上传文件的扩展名
字符串文件扩展=
System.IO.Path.GetExtension(fileUpload.FileName);
//如果上载的文件不为空,且文件扩展名为csv,请尝试
如果(fileUpload.PostedFile!=null&&fileExt==“.csv”)
{
尝试
{
fileUpload.PostedFile.SaveAs(Server.MapPath(“~/Uploads”);
//显示文件内容的步骤
Label1.Text=“文件名:”+
fileUpload.PostedFile.FileName+“
”+ fileUpload.PostedFile.ContentLength+“kb
”+ “内容类型:”+ fileUpload.PostedFile.ContentType; } 捕获(例外情况除外) { Label1.Text=“错误保存”+strFileNameOnServer+“
”+ex.消息; } //使“导入”和“取消导入”按钮可见,以便用户可以选择导入或取消 BtnImport1.Visible=true; Cancel.Visible=true; //使上载按钮不可见的步骤 fileUpload.Visible=false; btnUpload.Visible=false; } 其他的 { //如果用户未选择任何内容或文件扩展名不是.csv,将显示错误消息 Label1.Text=“错误-必须指定文件名/仅允许csv文件”; 返回; } //读取已发布csv文件的所有行并将这些行放入网格视图 var data=File.ReadAllLines(Server.MapPath(“~/Uploads”)) //按逗号分隔行的步骤 .Select(line=>line.Split(',')) .Select(columns=>new{GuestID=ErrorMessage(columns.Length
根据以上结果执行相应的操作。希望对您有所帮助

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
对于GridView1.Rows.Count-1,索引为整数=0的

如果GridView1.Rows(index).Cells(0).Text=“value”,则
ScriptManager.RegisterClientScriptBlock(Me,GetType(第页),“”,“警报(‘重复记录’);”,False)
如果结束
下一个

我认为最好是您管理数据插入和更改,这样就不会出现重复记录,而不是事后显示错误。上传实际上允许用户上传,因此,我不能限制他们,这就是我需要错误检查的原因
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