title: insert or update with single button in vb.net
body: <p>how to insert and update data in database(sql server) with single button in vb.net i tried but not get the result.
  here is my code.......</p>
  
  <pre><code>Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
      Try
          Dim reader As SqlDataReader
          Dim query As String
          Dim n As Integer
          n = 0
          sqlcom1 = New SqlCommand("select * from mydatabase.masters")
          sqlcom1.ExecuteReader()
          sqlcom = New SqlCommand("select * from mydatabase.masters")
          sqlcom.ExecuteReader()
          reader = sqlcom1.ExecuteReader
          reader = sqlcom.ExecuteReader
          sqlcom = New SqlCommand("Update masters SET EmpName=@EmpName, Age=@Age, Address=@Address where Empid=@Empid", conn)
          sqlcom.Parameters.Add("@EmpName", SqlDbType.VarChar).Value = TextBox4.Text
          sqlcom.Parameters.Add("@Age", SqlDbType.Int).Value = TextBox3.Text
          sqlcom.Parameters.Add("@Address", SqlDbType.VarChar).Value = TextBox2.Text
          sqlcom.Parameters.Add("@Empid", SqlDbType.Int).Value = TextBox1.Text
          sqlcom1 = New SqlCommand("insert into masters(Empid, EmpName, Age, Address) values(@Empid, @EmpName, @Age, @Address)", conn)
          sqlcom1.Parameters.AddWithValue("@Empid", TextBox1.Text)
          sqlcom1.Parameters.AddWithValue("@EmpName", TextBox4.Text)
          sqlcom1.Parameters.AddWithValue("@Age", TextBox3.Text)
          sqlcom1.Parameters.AddWithValue("@Address", TextBox2.Text)
          conn.Open()
          While reader.Read
              n = n + 1
          End While
          If table.Rows.Count = n Then
              sqlcom1.ExecuteNonQuery()
          ElseIf table.Rows.Count = n + 1 Then
              sqlcom.ExecuteNonQuery()
          End If
      Catch ex As Exception
          MessageBox.Show("error" + ex.Message)
      End Try
  End Sub
  </code></pre>