Vishnu Suresh

Vishnu Suresh

  • NA
  • 34
  • 12.5k

Preventing repeated data into the gridview

Oct 5 2015 4:05 AM
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)