17
Answers

Preventing repeated data into the gridview

Vishnu Suresh

Vishnu Suresh

7y
2.3k
1
I have a .net program in which datas are to be inserted through gridview. If I am adding the same data twice then I need to show error message that data repeated. How can I do this. I have used one code but it shows error.
 
for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            TextBox txtSlno = (TextBox)GridView1.Rows[i].Cells[1].FindControl("txtSlno");
            if (txtSlno.Text != "" || txtSlno.Text != null)
            {
                if (txtSlno.Text == dt.Rows[i]["SlNo"].ToString())
                {
                    Label1.Text = "already exist";
                    break;
                }
                else
                {
                    Label1.Text = "";
                }
            }
        }
It shows error there is no row at position zero. 
Answers (17)