title: Prevent duplicate value insert in database and in datagridview in vb.net
body: <p>I want to prevent duplicate roll no entered in database with following code but it is not helpful and also suggest that how to prevent this in gridview also.</p>
  
  <pre><code>   If txtRollNo.Text.Trim = "" Then
          ErrorProvider1.SetError(txtRollNo, "Roll Number is required")
          Errorcheck = False
        ElseIf varDuplicate = True Then
          MessageBox.Show("Duplicate values are not allowed")
        Else
         ErrorProvider1.SetError(txtRollNo, "")
    End If
  
  
      Private Function Duplicate() As Boolean
          Duplicate = True
          duplicatedata = "Select Count(RollNo) from KaiyumVbStudent where RollNO = &amp;txtRollNo.Text.Trim"
          con = New SqlConnection(constring)
          cmd = New SqlCommand(duplicatedata, con)
          con.Open()
          Dim mcount As Integer = cmd.ExecuteScalar
          If mcount &gt; 0 Then
              Duplicate = True
          Else
              Duplicate = False
          End If
      End Function
  </code></pre>